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.core.partition.impl.btree;
21  
22  
23  import org.apache.directory.api.ldap.model.schema.comparators.SerializableComparator;
24  
25  
26  /**
27   * TupleComparator for index records.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  public class ForwardIndexComparator<K> implements TupleComparator<K, Long>
32  {
33      private static final long serialVersionUID = 3257283621751633459L;
34  
35      /** The key comparison to use */
36      private final SerializableComparator<K> keyComparator;
37  
38  
39      /**
40       * Creates an IndexComparator.
41       *
42       * @param keyComparator the table comparator to use for keys
43       */
44      public ForwardIndexComparator( SerializableComparator<K> keyComparator )
45      {
46          this.keyComparator = keyComparator;
47      }
48  
49  
50      /**
51       * Gets the comparator used to compare keys.
52       *
53       * @return the comparator for comparing keys.
54       */
55      public SerializableComparator<K> getKeyComparator()
56      {
57          return keyComparator;
58      }
59  
60  
61      /**
62       * Gets the binary comparator used to compare values which are the indices
63       * into the master table.
64       *
65       * @return the binary comparator for comparing values.
66       */
67      public SerializableComparator<Long> getValueComparator()
68      {
69          return LongComparator.INSTANCE;
70      }
71  
72  
73      /**
74       * Compares key Object to determine their sorting order returning a
75       * value = to, &lt; or &gt; than 0.
76       *
77       * @param key1 the first key to compare
78       * @param key2 the other key to compare to the first
79       * @return 0 if both are equal, a negative value less than 0 if the first
80       * is less than the second, or a poistive value if the first is greater than
81       * the second byte array.
82       */
83      public int compareKey( K key1, K key2 )
84      {
85          return getKeyComparator().compare( key1, key2 );
86      }
87  
88  
89      /**
90       * Comparse value Objects to determine their sorting order returning a
91       * value = to, &lt; or &gt; than 0.
92       *
93       * @param l1 the first Long value to compare
94       * @param l2 the other Long value to compare to the first
95       * @return 0 if both are equal, a negative value less than 0 if the first
96       * is less than the second, or a positive value if the first is greater than
97       * the second Object.
98       */
99      public int compareValue( Long l1, Long l2 )
100     {
101         return ( l1 < l2 ? -1 : ( l1.equals( l2 ) ? 0 : 1 ) );
102     }
103 }