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 ForgeRock AS
026 */
027package org.opends.server.backends.jeb;
028
029import com.sleepycat.je.DatabaseEntry;
030
031/**
032 * An integer identifier assigned to each entry in the JE backend.
033 * An entry ID is implemented by this class as a long.
034 * There are static methods to assign monotonically increasing entry IDs,
035 * starting from 1.
036 */
037public class EntryID implements Comparable<EntryID>
038{
039  /** The identifier integer value. */
040  private final long id;
041  /** The value in database format, created when necessary. */
042  private DatabaseEntry data;
043
044  /**
045   * Create a new entry ID object from a given long value.
046   * @param id The long value of the ID.
047   */
048  public EntryID(long id)
049  {
050    this.id = id;
051  }
052
053  /**
054   * Create a new entry ID object from a value in database format.
055   * @param databaseEntry The database value of the ID.
056   */
057  public EntryID(DatabaseEntry databaseEntry)
058  {
059    data = databaseEntry;
060    id = JebFormat.entryIDFromDatabase(data.getData());
061  }
062
063  /**
064   * Get the value of the entry ID as a long.
065   * @return The entry ID.
066   */
067  public long longValue()
068  {
069    return id;
070  }
071
072  /**
073   * Get the value of the ID in database format.
074   * @return The value of the ID in database format.
075   */
076  public DatabaseEntry getDatabaseEntry()
077  {
078    if (data == null)
079    {
080      data = new DatabaseEntry();
081      data.setData(JebFormat.entryIDToDatabase(id));
082    }
083    return data;
084  }
085
086  /**
087   * Compares this object with the specified object for order.  Returns a
088   * negative integer, zero, or a positive integer as this object is less
089   * than, equal to, or greater than the specified object.<p>
090   * <p/>
091   *
092   * @param that the Object to be compared.
093   * @return a negative integer, zero, or a positive integer as this object
094   *         is less than, equal to, or greater than the specified object.
095   * @throws ClassCastException if the specified object's type prevents it
096   *                            from being compared to this Object.
097   */
098  @Override
099  public int compareTo(EntryID that) throws ClassCastException
100  {
101    final long result = this.id - that.id;
102    if (result < 0)
103    {
104      return -1;
105    }
106    else if (result > 0)
107    {
108      return 1;
109    }
110    return 0;
111  }
112
113  /**
114   * Indicates whether some other object is "equal to" this one.
115   *
116   * @param   that   the reference object with which to compare.
117   * @return  <code>true</code> if this object is the same as the obj
118   *          argument; <code>false</code> otherwise.
119   * @see     #hashCode()
120   * @see     java.util.Hashtable
121   */
122  @Override
123  public boolean equals(Object that)
124  {
125    if (this == that)
126    {
127      return true;
128    }
129    if (!(that instanceof EntryID))
130    {
131      return false;
132    }
133    return this.id == ((EntryID) that).id;
134  }
135
136  /**
137   * Returns a hash code value for the object. This method is
138   * supported for the benefit of hashtables such as those provided by
139   * <code>java.util.Hashtable</code>.
140   *
141   * @return  a hash code value for this object.
142   * @see     java.lang.Object#equals(java.lang.Object)
143   * @see     java.util.Hashtable
144   */
145  @Override
146  public int hashCode()
147  {
148    return (int) id;
149  }
150
151  /**
152   * Get a string representation of this object.
153   * @return A string representation of this object.
154   */
155  @Override
156  public String toString()
157  {
158    return Long.toString(id);
159  }
160}