001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.types;
018
019import org.forgerock.opendj.ldap.DN;
020
021/**
022 * This class defines a Directory Server cache entry, which is simply
023 * used to store an entry with its associated backend and entry ID.
024 */
025@org.opends.server.types.PublicAPI(
026     stability=org.opends.server.types.StabilityLevel.VOLATILE,
027     mayInstantiate=true,
028     mayExtend=false,
029     mayInvoke=true,
030     notes="This should only be used within a backend")
031public final class CacheEntry
032{
033  /** ID of the backend with which this cache entry is associated. */
034  private final String backendID;
035
036  /** The entry itself. */
037  private final Entry entry;
038
039  /** The entry ID for the entry within the backend. */
040  private final long entryID;
041
042  /**
043   * Creates a new cache entry with the provided information.
044   *
045   * @param  entry    The entry for this cache entry.
046   * @param  backendID  ID of the backend for this cache entry.
047   * @param  entryID  The entry ID for this cache entry.
048   */
049  public CacheEntry(Entry entry, String backendID, long entryID)
050  {
051    this.entry   = entry;
052    this.backendID = backendID;
053    this.entryID = entryID;
054  }
055
056  /**
057   * Retrieves the entry for this cache entry.
058   *
059   * @return  The entry for this cache entry.
060   */
061  public Entry getEntry()
062  {
063    return entry;
064  }
065
066  /**
067   * Retrieves the backend ID for this cache entry.
068   *
069   * @return  ID of the backend for this cache entry.
070   */
071  public String getBackendID()
072  {
073    return backendID;
074  }
075
076  /**
077   * Retrieves the entry ID for this cache entry.
078   *
079   * @return  The entry ID for this cache entry.
080   */
081  public long getEntryID()
082  {
083    return entryID;
084  }
085
086  /**
087   * Retrieves the DN for this cache entry.
088   *
089   * @return  The DN for this cache entry.
090   */
091  public DN getDN()
092  {
093    return entry.getName();
094  }
095
096  /**
097   * Retrieves the hash code for this cache entry.  It will be the
098   * integer representation of the entry ID.
099   *
100   * @return  The hash code for this cache entry.
101   */
102  @Override
103  public int hashCode()
104  {
105    return (int) entryID;
106  }
107
108  /**
109   * Indicates whether this cache entry is equal to the provided \
110   * object.  They will be considered equal if the provided object is
111   * a cache entry with the same entry and entry ID.
112   *
113   * @param  o  The object for which to make the determination.
114   *
115   * @return  <CODE>true</CODE> if the provided object is equal to
116   *          this cache entry, or <CODE>false</CODE> if not.
117   */
118  @Override
119  public boolean equals(Object o)
120  {
121    if (o == null)
122    {
123      return false;
124    }
125
126    if (o == this)
127    {
128      return true;
129    }
130
131    if (! (o instanceof CacheEntry))
132    {
133      return false;
134    }
135
136    CacheEntry e = (CacheEntry) o;
137    return e.entryID == entryID && e.entry.equals(entry);
138  }
139}
140