Debian Codegraph @

jdbm1/src/main/java/jdbm/helper/ObjectBAComparator.java

raw
1 /**
2 * JDBM LICENSE v1.00
3 *
4 * Redistribution and use of this software and associated documentation
5 * ("Software"), with or without modification, are permitted provided
6 * that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain copyright
9 * statements and notices. Redistributions must also contain a
10 * copy of this document.
11 *
12 * 2. Redistributions in binary form must reproduce the
13 * above copyright notice, this list of conditions and the
14 * following disclaimer in the documentation and/or other
15 * materials provided with the distribution.
16 *
17 * 3. The name "JDBM" must not be used to endorse or promote
18 * products derived from this Software without prior written
19 * permission of Cees de Groot. For written permission,
20 * please contact [email protected].
21 *
22 * 4. Products derived from this Software may not be called "JDBM"
23 * nor may "JDBM" appear in their names without prior written
24 * permission of Cees de Groot.
25 *
26 * 5. Due credit should be given to the JDBM Project
27 * (http://jdbm.sourceforge.net/).
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
31 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Copyright 2001 (C) Alex Boisvert. All Rights Reserved.
43 * Contributions are Copyright (C) 2001 by their associated contributors.
44 *
45 */
46
47 package jdbm.helper;
48
49
50 import java.io.IOException;
51 import java.io.Serializable;
52 import java.util.Comparator;
53
54 import jdbm.I18n;
55
56
57 /**
58 * Comparator for objects which have been serialized into byte arrays.
59 * In effect, it wraps another Comparator which compares object and provides
60 * transparent deserialization from byte array to object.
61 *
62 * @author <a href="mailto:[email protected]">Alex Boisvert</a>
63 */
64 public final class ObjectBAComparator
65 implements Comparator, Serializable
66 {
67
68 /**
69 * Version id for serialization.
70 */
71 final static long serialVersionUID = 1L;
72
73 /**
74 * Wrapped comparator.
75 */
76 private Comparator _comparator;
77
78
79 /**
80 * Construct an ObjectByteArrayComparator which wraps an Object Comparator.
81 *
82 * @param comparator Object comparator.
83 */
84 public ObjectBAComparator( Comparator comparator )
85 {
86 if ( comparator == null )
87 {
88 throw new IllegalArgumentException( I18n.err( I18n.ERR_518 ) );
89 }
90
91 _comparator = comparator;
92 }
93
94
95 /**
96 * Compare two objects.
97 *
98 * @param obj1 First object
99 * @param obj2 Second object
100 * @return 1 if obj1 > obj2, 0 if obj1 == obj2, -1 if obj1 < obj2
101 */
102 public int compare( Object obj1, Object obj2 )
103 {
104 if ( obj1 == null )
105 {
106 throw new IllegalArgumentException( I18n.err( I18n.ERR_525 ) );
107 }
108
109 if ( obj2 == null )
110 {
111 throw new IllegalArgumentException( I18n.err( I18n.ERR_526 ) );
112 }
113
114 try
115 {
116 obj1 = Serialization.deserialize( ( byte[] ) obj1 );
117 obj2 = Serialization.deserialize( ( byte[] ) obj2 );
118
119 return _comparator.compare( obj1, obj2 );
120 }
121 catch ( IOException except )
122 {
123 throw new WrappedRuntimeException( except );
124 }
125 catch ( ClassNotFoundException except )
126 {
127 throw new WrappedRuntimeException( except );
128 }
129 }
130
131
132 /**
133 * Compare two byte arrays.
134 */
135 public static int compareByteArray( byte[] thisKey, byte[] otherKey )
136 {
137 int len = Math.min( thisKey.length, otherKey.length );
138
139 // compare the byte arrays
140 for ( int i = 0; i < len; i++ )
141 {
142 if ( thisKey[i] >= 0 )
143 {
144 if ( otherKey[i] >= 0 )
145 {
146 // both positive
147 if ( thisKey[i] < otherKey[i] )
148 {
149 return -1;
150 }
151 else if ( thisKey[i] > otherKey[i] )
152 {
153 return 1;
154 }
155 }
156 else
157 {
158 // otherKey is negative => greater (because MSB is 1)
159 return -1;
160 }
161 }
162 else
163 {
164 if ( otherKey[i] >= 0 )
165 {
166 // thisKey is negative => greater (because MSB is 1)
167 return 1;
168 }
169 else
170 {
171 // both negative
172 if ( thisKey[i] < otherKey[i] )
173 {
174 return -1;
175 }
176 else if ( thisKey[i] > otherKey[i] )
177 {
178 return 1;
179 }
180 }
181 }
182 }
183 if ( thisKey.length == otherKey.length )
184 {
185 return 0;
186 }
187 if ( thisKey.length < otherKey.length )
188 {
189 return -1;
190 }
191 return 1;
192 }
193
194 }