1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.db;
18
19
20 import org.apache.ldap.common.util.BigIntegerComparator;
21 import org.apache.ldap.server.schema.SerializableComparator;
22
23
24 /***
25 * TupleComparator for index records.
26 *
27 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
28 * @version $Rev: 157708 $
29 */
30 public class IndexComparator implements TupleComparator
31 {
32 private static final long serialVersionUID = 3257283621751633459L;
33
34 private static final SerializableComparator BIG_INTEGER_COMPARATOR =
35 new SerializableComparator( "1.2.6.1.4.1.18060.1.1.1.2.2" )
36 {
37 private static final long serialVersionUID = 3690478030414165816L;
38
39 public int compare( Object o1, Object o2 )
40 {
41 return BigIntegerComparator.INSTANCE.compare( o1, o2 );
42 }
43 };
44 /*** Whether or not the key/value is swapped */
45 private final boolean isForwardMap;
46 /*** The key comparison to use */
47 private final SerializableComparator keyComp;
48
49
50 /***
51 * Creates an IndexComparator.
52 *
53 * @param keyComp the table comparator to use for keys
54 * @param isForwardMap whether or not the comparator should swap the
55 * key value pair while conducting comparisons.
56 */
57 public IndexComparator( SerializableComparator keyComp, boolean isForwardMap )
58 {
59 this.keyComp = keyComp;
60 this.isForwardMap = isForwardMap;
61 }
62
63
64 /***
65 * Gets the comparator used to compare keys. May be null in which
66 * case the compareKey method will throw an UnsupportedOperationException.
67 *
68 * @return the comparator for comparing keys.
69 */
70 public SerializableComparator getKeyComparator()
71 {
72 if ( isForwardMap )
73 {
74 return keyComp;
75 }
76
77 return BIG_INTEGER_COMPARATOR;
78 }
79
80
81 /***
82 * Gets the binary comparator used to compare valuess. May be null in which
83 * case the compareValue method will throw an UnsupportedOperationException.
84 *
85 * @return the binary comparator for comparing values.
86 */
87 public SerializableComparator getValueComparator()
88 {
89 if ( isForwardMap )
90 {
91 return BIG_INTEGER_COMPARATOR;
92 }
93
94 return keyComp;
95 }
96
97
98 /***
99 * Compares key Object to determine their sorting order returning a
100 * value = to, < or > than 0.
101 *
102 * @param key1 the first key to compare
103 * @param key2 the other key to compare to the first
104 * @return 0 if both are equal, a negative value less than 0 if the first
105 * is less than the second, or a postive value if the first is greater than
106 * the second byte array.
107 */
108 public int compareKey( Object key1, Object key2 )
109 {
110 return getKeyComparator().compare( key1, key2 );
111 }
112
113
114 /***
115 * Comparse value Objects to determine their sorting order returning a
116 * value = to, < or > than 0.
117 *
118 * @param value1 the first value to compare
119 * @param value2 the other value to compare to the first
120 * @return 0 if both are equal, a negative value less than 0 if the first
121 * is less than the second, or a postive value if the first is greater than
122 * the second Object.
123 */
124 public int compareValue( Object value1, Object value2 )
125 {
126 return getValueComparator().compare( value1, value2 );
127 }
128 }