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