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 ReverseIndexComparator<V> implements TupleComparator<Long, V>
32  {
33      private static final long serialVersionUID = 3257283621751633459L;
34  
35      /** The value comparison to use - keys are Longs */
36      private final SerializableComparator<V> valueComparator;
37  
38  
39      /**
40       * Creates an IndexComparator.
41       *
42       * @param valueComparator the table comparator to use for values
43       */
44      public ReverseIndexComparator( SerializableComparator<V> valueComparator )
45      {
46          this.valueComparator = valueComparator;
47      }
48  
49  
50      /**
51       * Gets the comparator used to compare keys.
52       *
53       * @return the comparator for comparing keys.
54       */
55      public SerializableComparator<Long> getKeyComparator()
56      {
57          return LongComparator.INSTANCE;
58      }
59  
60  
61      /**
62       * Gets the binary comparator used to compare values which are the values
63       * of attributes.
64       *
65       * @return the binary comparator for comparing values.
66       */
67      public SerializableComparator<V> getValueComparator()
68      {
69          return valueComparator;
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 l1 the first long key to compare
78       * @param l2 the other long 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 positive value if the first is greater than
81       * the second byte array.
82       */
83      public int compareKey( Long l1, Long l2 )
84      {
85          return ( l1 < l2 ? -1 : ( l1.equals( l2 ) ? 0 : 1 ) );
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 v1 the first value to compare
94       * @param v2 the other 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( V v1, V v2 )
100     {
101         return valueComparator.compare( v1, v2 );
102     }
103 }