KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > db > IndexComparator


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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: 169198 $
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 JavaDoc o1, Object JavaDoc 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 JavaDoc key1, Object JavaDoc 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 JavaDoc value1, Object JavaDoc value2 )
125     {
126         return getValueComparator().compare( value1, value2 );
127     }
128 }
129
Popular Tags