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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.backends.jeb;
028    
029    import 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     */
037    public class EntryID implements Comparable<EntryID>
038    {
039      /**
040       * The identifier integer value.
041       */
042      private final Long id;
043    
044      /**
045       * The value in database format, created when necessary.
046       */
047      private DatabaseEntry data = null;
048    
049      /**
050       * Create a new entry ID object from a given long value.
051       * @param id The long value of the ID.
052       */
053      public EntryID(long id)
054      {
055        this.id = id;
056      }
057    
058      /**
059       * Create a new entry ID object from a given Long value.
060       * @param id the Long value of the ID.
061       */
062      public EntryID(Long id)
063      {
064        this.id = id;
065      }
066    
067      /**
068       * Create a new entry ID object from a value in database format.
069       * @param databaseEntry The database value of the ID.
070       */
071      public EntryID(DatabaseEntry databaseEntry)
072      {
073        data = databaseEntry;
074        id = JebFormat.entryIDFromDatabase(data.getData());
075      }
076    
077      /**
078       * Get the value of the entry ID as a long.
079       * @return The entry ID.
080       */
081      public long longValue()
082      {
083        return id;
084      }
085    
086      /**
087       * Get the value of the ID in database format.
088       * @return The value of the ID in database format.
089       */
090      public DatabaseEntry getDatabaseEntry()
091      {
092        if (data == null)
093        {
094          data = new DatabaseEntry();
095          data.setData(JebFormat.entryIDToDatabase(id));
096        }
097        return data;
098      }
099    
100      /**
101       * Compares this object with the specified object for order.  Returns a
102       * negative integer, zero, or a positive integer as this object is less
103       * than, equal to, or greater than the specified object.<p>
104       * <p/>
105       *
106       * @param that the Object to be compared.
107       * @return a negative integer, zero, or a positive integer as this object
108       *         is less than, equal to, or greater than the specified object.
109       * @throws ClassCastException if the specified object's type prevents it
110       *                            from being compared to this Object.
111       */
112      public int compareTo(EntryID that) throws ClassCastException
113      {
114        return this.id.compareTo(that.id);
115      }
116    
117      /**
118       * Indicates whether some other object is "equal to" this one.
119       *
120       * @param   that   the reference object with which to compare.
121       * @return  <code>true</code> if this object is the same as the obj
122       *          argument; <code>false</code> otherwise.
123       * @see     #hashCode()
124       * @see     java.util.Hashtable
125       */
126      @Override public boolean equals(Object that)
127      {
128        if (that == null)
129        {
130          return false;
131        }
132        if (this == that)
133        {
134          return true;
135        }
136        if (!(that instanceof EntryID))
137        {
138          return false;
139        }
140        return this.id.equals(((EntryID)that).id);
141      }
142    
143      /**
144       * Returns a hash code value for the object. This method is
145       * supported for the benefit of hashtables such as those provided by
146       * <code>java.util.Hashtable</code>.
147       *
148       * @return  a hash code value for this object.
149       * @see     java.lang.Object#equals(java.lang.Object)
150       * @see     java.util.Hashtable
151       */
152      @Override public int hashCode()
153      {
154        return (int)id.longValue();
155      }
156    
157      /**
158       * Get a string representation of this object.
159       * @return A string representation of this object.
160       */
161      public String toString()
162      {
163        return id.toString();
164      }
165    }