View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.xdbm;
21  
22  
23  import org.apache.directory.api.ldap.model.cursor.Tuple;
24  import org.apache.directory.api.ldap.model.entry.Entry;
25  
26  
27  /**
28   * An index id value pair based on a Tuple which can optionally reference the
29   * indexed Entry if one has already been loaded.
30   * 
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @param <K> The key stored in the Tuple, associated key for the object
33   * @param <I> The ID of the object
34   */
35  public class IndexEntry<K, I>
36  {
37      /** The referenced Entry if loaded from the store */
38      private Entry entry;
39  
40      /** The underlying Tuple */
41      private final Tuple<K, I> tuple = new Tuple<>();
42  
43  
44      /**
45       * Creates a ForwardIndexEntry instance
46       */
47      public IndexEntry()
48      {
49          super();
50      }
51  
52  
53      /**
54       * Sets the key value tuple represented by this ForwardIndexEntry, after having 
55       * reset the IndexEntry content (the Entry will now be null)
56       *
57       * @param tuple the tuple for the ForwardIndexEntry
58       */
59      public void setTuple( Tuple<K, I> tuple )
60      {
61          // Clear the entry
62          entry = null;
63  
64          // And inject the tuple key and value 
65          this.tuple.setKey( tuple.getKey() );
66          this.tuple.setValue( tuple.getValue() );
67      }
68  
69  
70      /**
71       * {@inheritDoc}
72       */
73      public I getId()
74      {
75          return tuple.getValue();
76      }
77  
78  
79      /**
80       * {@inheritDoc}
81       */
82      public K getKey()
83      {
84          return tuple.getKey();
85      }
86  
87  
88      /**
89       * {@inheritDoc}
90       */
91      public void setId( I id )
92      {
93          tuple.setValue( id );
94      }
95  
96  
97      /**
98       * {@inheritDoc}
99       */
100     public void setKey( K value )
101     {
102         tuple.setKey( value );
103     }
104 
105 
106     /**
107      * {@inheritDoc}
108      */
109     public Entry getEntry()
110     {
111         return entry;
112     }
113 
114 
115     /**
116      * {@inheritDoc}
117      */
118     public void setEntry( Entry entry )
119     {
120         this.entry = entry;
121     }
122 
123 
124     /**
125      * {@inheritDoc}
126      */
127     public Tuple<K, I> getTuple()
128     {
129         return tuple;
130     }
131 
132 
133     /**
134      * {@inheritDoc}
135      */
136     public void clear()
137     {
138         entry = null;
139         tuple.setKey( null );
140         tuple.setValue( null );
141     }
142 
143 
144     /**
145      * {@inheritDoc}
146      */
147     public void copy( IndexEntry<K, I> entry )
148     {
149         this.entry = entry.getEntry();
150         tuple.setKey( entry.getKey() );
151         tuple.setValue( entry.getId() );
152     }
153 
154 
155     public int hashCode()
156     {
157         if ( getId() == null )
158         {
159             return 0;
160         }
161 
162         return getId().hashCode();
163     }
164 
165 
166     public boolean equals( Object that )
167     {
168         if ( that == this )
169         {
170             return true;
171         }
172 
173         if ( !( that instanceof IndexEntry ) )
174         {
175             return false;
176         }
177 
178         @SuppressWarnings("unchecked")
179         IndexEntry<K, I> thatIndexEntry = ( IndexEntry<K, I> ) that;
180 
181         if ( thatIndexEntry.getId() == null )
182         {
183             return getId() == null;
184         }
185 
186         return thatIndexEntry.getId().equals( getId() );
187     }
188 
189 
190     /**
191      * {@inheritDoc}
192      */
193     public String toString()
194     {
195         StringBuilder buf = new StringBuilder();
196         buf.append( "IndexEntry[ " );
197         buf.append( tuple.getKey() );
198         buf.append( ", " );
199         buf.append( tuple.getValue() );
200         buf.append( " ]" );
201 
202         return buf.toString();
203     }
204 }