1 /* Copyright (c) 2008 Sascha Kohlmann
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU Affero General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 package net.sf.eos.util;
17
18 import java.util.Arrays;
19
20 import net.sf.eos.Nullable;
21
22 /**
23 * Utility class to support {@link Object#equals(Object)}
24 * and {@link Object#hashCode()} method implementation.
25 * @author Sascha Kohlmann
26 * @since 0.1.0
27 * @version 1.0.0
28 */
29 public final class EqualsAndHashUtil {
30
31 /** Magic hash multi value. */
32 private static final int HASH_MULTI = 37;
33
34 /** Magic hash shift value. */
35 private static final int HASH_SHIFT = 32;
36
37 /** Only {@code static} methods available. */
38 private EqualsAndHashUtil() {
39 super();
40 }
41
42 /**
43 * {@code boolean} equality check.
44 * @param own the first {@code boolean}
45 * @param other the second <{@code boolean}
46 * @return {@code true} if the {@code boolean}s are equal,
47 * <{@code false} otherwise
48 */
49 public static boolean isEqual(final boolean own, final boolean other) {
50 return own == other;
51 }
52
53 /**
54 * {@code char} equality check.
55 * @param own the first {@code char}
56 * @param other the second {@code char}
57 * @return {@code true} if the {@code char}s are equal,
58 * {@code false} otherwise
59 */
60 public static boolean isEqual(final char own, final char other) {
61 return own == other;
62 }
63
64 /**
65 * {@code byte} equality check.
66 * @param own the first {@code byte}
67 * @param other the second {@code byte}
68 * @return {@code true} if the {@code byte}s are equal,
69 * {@code false} otherwise
70 */
71 public static boolean isEqual(final byte own, final byte other) {
72 return own == other;
73 }
74
75 /**
76 * {@code short} equality check.
77 * @param own the first {@code short}
78 * @param other the second {@code short}
79 * @return {@code true} if the {@code short}s are equal,
80 * {@code false} otherwise
81 */
82 public static boolean isEqual(final short own, final short other) {
83 return own == other;
84 }
85
86 /**
87 * {@code int} equality check.
88 * @param own the first {@code int}
89 * @param other the second {@code int}
90 * @return {@code true} if the {@code int}s are equal,
91 * {@code false} otherwise
92 */
93 public static boolean isEqual(final int own, final int other) {
94 return own == other;
95 }
96
97 /**
98 * {@code long} equality check.
99 * @param own the first {@code long}
100 * @param other the second {@code long}
101 * @return {@code true} if the {@code long}s are equal,
102 * {@code false} otherwise
103 */
104 public static boolean isEqual(final long own, final long other) {
105 return own == other;
106 }
107
108 /**
109 * {@code float} equality check.
110 * @param own the first {@code float}
111 * @param other the second {@code float}
112 * @return {@code true} if the {@code float}s are equal,
113 * {@code false} otherwise
114 */
115 public static boolean isEqual(final float own, final float other) {
116 return Float.floatToIntBits(own) == Float.floatToIntBits(other);
117 }
118
119 /**
120 * {@code double} equality check.
121 * @param own the first {@code double}
122 * @param other the second {@code double}
123 * @return {@code true} if the {@code double}s are equal,
124 * {@code false} otherwise
125 */
126 public static boolean isEqual(final double own, final double other) {
127 return Double.doubleToLongBits(own) == Double.doubleToLongBits(other);
128 }
129
130 /**
131 * {@code Object} equality check.
132 * @param own the first {@code Object}
133 * @param other the second {@code Object}
134 * @return {@code true} if the {@code Object}s are equal,
135 * {@code false} otherwise
136 */
137 public static boolean isEqual(@Nullable final Object own,
138 @Nullable final Object other) {
139 return own == null ? other == null : own.equals(other);
140 }
141
142 /**
143 * {@code boolean} array equality check.
144 * @param own the first array of {@code boolean}s
145 * @param other the second array of {@code boolean}s
146 * @return {@code true} if the two {@code boolean} arrays are
147 * equal, {@code false} otherwise
148 */
149 public static boolean isEqual(@Nullable final boolean[] own,
150 @Nullable final boolean[] other) {
151 return Arrays.equals(own, other);
152 }
153
154 /**
155 * {@code char} array equality check.
156 * @param own the first array of {@code char}s
157 * @param other the second array of {@code char}s
158 * @return {@code true} if the two {@code char} arrays are
159 * equal, {@code false} otherwise
160 */
161 public static boolean isEqual(@Nullable final char[] own,
162 @Nullable final char[] other) {
163 return Arrays.equals(own, other);
164 }
165
166 /**
167 * {@code byte} array equality check.
168 * @param own the first array of {@code byte}s
169 * @param other the second array of {@code byte}s
170 * @return {@code true} if the two {@code byte} arrays are
171 * equal, {@code false} otherwise
172 */
173 public static boolean isEqual(@Nullable final byte[] own,
174 @Nullable final byte[] other) {
175 return Arrays.equals(own, other);
176 }
177
178 /**
179 * {@code short} array equality check.
180 * @param own the first array of {@code short}s
181 * @param other the second array of {@code short}s
182 * @return {@code true} if the two {@code short} arrays are
183 * equal, {@code false} otherwise
184 */
185 public static boolean isEqual(@Nullable final short[] own,
186 @Nullable final short[] other) {
187 return Arrays.equals(own, other);
188 }
189
190 /**
191 * {@code int} array equality check.
192 * @param own the first array of {@code int}s
193 * @param other the second array of {@code int}s
194 * @return {@code true} if the two {@code int} arrays are
195 * equal, {@code false} otherwise
196 */
197 public static boolean isEqual(@Nullable final int[] own,
198 @Nullable final int[] other) {
199 return Arrays.equals(own, other);
200 }
201
202 /**
203 * {@code long} array equality check.
204 * @param own the first array of {@code long}s
205 * @param other the second array of {@code long}s
206 * @return {@code true} if the two {@code long} arrays are
207 * equal, {@code false} otherwise
208 */
209 public static boolean isEqual(@Nullable final long[] own,
210 @Nullable final long[] other) {
211 return Arrays.equals(own, other);
212 }
213
214 /**
215 * {@code float} array equality check.
216 * @param own the first array of {@code float}s
217 * @param other the second array of {@code float}s
218 * @return {@code true} if the two {@code float} arrays are
219 * equal, {@code false} otherwise
220 */
221 public static boolean isEqual(@Nullable final float[] own,
222 @Nullable final float[] other) {
223 return Arrays.equals(own, other);
224 }
225
226 /**
227 * {@code double} array equality check.
228 * @param own the first array of {@code double}s
229 * @param other the second array of {@code double}s
230 * @return {@code true} if the two {@code double} arrays are
231 * equal, {@code false} otherwise
232 */
233 public static boolean isEqual(@Nullable final double[] own,
234 @Nullable final double[] other) {
235 return Arrays.equals(own, other);
236 }
237
238 /**
239 * {@code Object} array equality check.
240 * @param own the first array of {@code Object}s
241 * @param other the second array of {@code Object}s
242 * @return {@code true} if the two {@code Object} arrays are
243 * equal, {@code false} otherwise
244 */
245 public static boolean isEqual(@Nullable final Object[] own,
246 @Nullable final Object[] other) {
247 return Arrays.equals(own, other);
248 }
249
250 /**
251 * Creates a {@linkplain Object#hashCode() hash code} for the
252 * given {@code Object} according to the rules stated in "Effective
253 * Java" by Joshua Bloch.
254 * @param obj the object to compute the hash code for
255 * @return a hash code
256 */
257 public static int hash(@Nullable final Object obj) {
258 return obj == null ? 0 : obj.hashCode();
259 }
260
261 /**
262 * Creates a {@linkplain Object#hashCode() hash code} for the
263 * given {@code boolean} according to the rules stated in "Effective
264 * Java" by Joshua Bloch.
265 * @param bool the object to compute the hash code for
266 * @return a hash code
267 */
268 public static int hash(final boolean bool) {
269 return bool ? 0 : HASH_MULTI * 1;
270 }
271
272 /**
273 * Creates a {@linkplain Object#hashCode() hash code} for the
274 * given {@code byte} according to the rules stated in "Effective
275 * Java" by Joshua Bloch.
276 * @param b the object to compute the hash code for
277 * @return a hash code
278 */
279 public static int hash(final byte b) {
280 return HASH_MULTI * b;
281 }
282
283 /**
284 * Creates a {@linkplain Object#hashCode() hash code} for the
285 * given {@code char} according to the rules stated in "Effective
286 * Java" by Joshua Bloch.
287 * @param c the object to compute the hash code for
288 * @return a hash code
289 */
290 public static int hash(final char c) {
291 return HASH_MULTI * c;
292 }
293
294 /**
295 * Creates a {@linkplain Object#hashCode() hash code} for the
296 * given {@code short} according to the rules stated in "Effective
297 * Java" by Joshua Bloch.
298 * @param s the object to compute the hash code for
299 * @return a hash code
300 */
301 public static int hash(final short s) {
302 return HASH_MULTI * s;
303 }
304
305 /**
306 * Creates a {@linkplain Object#hashCode() hash code} for the
307 * given {@code int} according to the rules stated in "Effective
308 * Java" by Joshua Bloch.
309 * @param i the object to compute the hash code for
310 * @return a hash code
311 */
312 public static int hash(final int i) {
313 return HASH_MULTI * i;
314 }
315
316 /**
317 * Creates a {@linkplain Object#hashCode() hash code} for the
318 * given {@code long} according to the rules stated in "Effective
319 * Java" by Joshua Bloch.
320 * @param l the object to compute the hash code for
321 * @return a hash code
322 */
323 public static int hash(final long l) {
324 return (int) (HASH_MULTI * (l ^ (l >>> HASH_SHIFT)));
325 }
326
327 /**
328 * Creates a {@linkplain Object#hashCode() hash code} for the
329 * given {@code float} according to the rules stated in "Effective
330 * Java" by Joshua Bloch.
331 * @param f the object to compute the hash code for
332 * @return a hash code
333 */
334 public static int hash(final float f) {
335 final int i = Float.floatToIntBits(f);
336 return EqualsAndHashUtil.hash(i);
337 }
338
339 /**
340 * Creates a {@linkplain Object#hashCode() hash code} for the
341 * given {@code double} according to the rules stated in "Effective
342 * Java" by Joshua Bloch.
343 * @param d the object to compute the hash code for
344 * @return a hash code
345 */
346 public static int hash(final double d) {
347 final long l = Double.doubleToLongBits(d);
348 return EqualsAndHashUtil.hash(l);
349 }
350
351 /**
352 * Creates a {@linkplain Object#hashCode() hash code} for the
353 * given array of {@code Object}s according to the rules stated in
354 * "Effective Java" by Joshua Bloch.
355 * @param arr the array of {@code Object}s to compute the hash code for
356 * @return a hash code
357 */
358 public static int hash(@Nullable final Object[] arr) {
359 int hashCode = 0;
360 if (arr != null) {
361 for (int i = arr.length; i >= 0; i--) {
362 hashCode += EqualsAndHashUtil.hash(arr[i]);
363 }
364 }
365 return hashCode;
366 }
367
368 /**
369 * Creates a {@linkplain Object#hashCode() hash code} for the
370 * given array of {@code boolean}s according to the rules stated in
371 * "Effective Java" by Joshua Bloch.
372 * @param arr the array of {@code boolean}s to compute the hash code
373 * @return a hash code
374 */
375 public static int hash(@Nullable final boolean[] arr) {
376 int hashCode = 0;
377 if (arr != null) {
378 for (int i = arr.length; i >= 0; i--) {
379 hashCode += EqualsAndHashUtil.hash(arr[i]);
380 }
381 }
382 return hashCode;
383 }
384
385 /**
386 * Creates a {@linkplain Object#hashCode() hash code} for the
387 * given array of {@code byte}s according to the rules stated in
388 * "Effective Java" by Joshua Bloch.
389 * @param arr the array of {@code byte}s to compute the hash code for
390 * @return a hash code
391 */
392 public static int hash(@Nullable final byte[] arr) {
393 int hashCode = 0;
394 if (arr != null) {
395 for (int i = arr.length; i >= 0; i--) {
396 hashCode += EqualsAndHashUtil.hash(arr[i]);
397 }
398 }
399 return hashCode;
400 }
401
402 /**
403 * Creates a {@linkplain Object#hashCode() hash code} for the
404 * given array of {@code char}s according to the rules stated in
405 * "Effective Java" by Joshua Bloch.
406 * @param arr the array of {@code char}s to compute the hash code for
407 * @return a hash code
408 */
409 public static int hash(@Nullable final char[] arr) {
410 int hashCode = 0;
411 if (arr != null) {
412 for (int i = arr.length; i >= 0; i--) {
413 hashCode += EqualsAndHashUtil.hash(arr[i]);
414 }
415 }
416 return hashCode;
417 }
418
419 /**
420 * Creates a {@linkplain Object#hashCode() hash code} for the
421 * given array of {@code short}s according to the rules stated in
422 * "Effective Java" by Joshua Bloch.
423 * @param arr the array of {@code short}s to compute the hash code for
424 * @return a hash code
425 */
426 public static int hash(@Nullable final short[] arr) {
427 int hashCode = 0;
428 if (arr != null) {
429 for (int i = arr.length; i >= 0; i--) {
430 hashCode += EqualsAndHashUtil.hash(arr[i]);
431 }
432 }
433 return hashCode;
434 }
435
436 /**
437 * Creates a {@linkplain Object#hashCode() hash code} for the
438 * given array of {@code int}s according to the rules stated in
439 * "Effective Java" by Joshua Bloch.
440 * @param arr the array of {@code int}s to compute the hash code for
441 * @return a hash code
442 */
443 public static int hash(@Nullable final int[] arr) {
444 int hashCode = 0;
445 if (arr != null) {
446 for (int i = arr.length; i >= 0; i--) {
447 hashCode += EqualsAndHashUtil.hash(arr[i]);
448 }
449 }
450 return hashCode;
451 }
452
453 /**
454 * Creates a {@linkplain Object#hashCode() hash code} for the
455 * given array of {@code long}s according to the rules stated in
456 * "Effective Java" by Joshua Bloch.
457 * @param arr the array of {@code long}s to compute the hash code for
458 * @return a hash code
459 */
460 public static int hash(@Nullable final long[] arr) {
461 int hashCode = 0;
462 if (arr != null) {
463 for (int i = arr.length; i >= 0; i--) {
464 hashCode += EqualsAndHashUtil.hash(arr[i]);
465 }
466 }
467 return hashCode;
468 }
469
470 /**
471 * Creates a {@linkplain Object#hashCode() hash code} for the
472 * given array of {@code float}s according to the rules stated in
473 * "Effective Java" by Joshua Bloch.
474 * @param arr the array of {@code float}s to compute the hash code for
475 * @return a hash code
476 */
477 public static int hash(@Nullable final float[] arr) {
478 int hashCode = 0;
479 if (arr != null) {
480 for (int i = arr.length; i >= 0; i--) {
481 hashCode += EqualsAndHashUtil.hash(arr[i]);
482 }
483 }
484 return hashCode;
485 }
486
487 /**
488 * Creates a {@linkplain Object#hashCode() hash code} for the
489 * given array of {@code double}s according to the rules stated in
490 * "Effective Java" by Joshua Bloch.
491 * @param arr the array of {@code double}s to compute the hash code for
492 * @return a hash code
493 */
494 public static int hash(@Nullable final double[] arr) {
495 int hashCode = 0;
496 if (arr != null) {
497 for (int i = arr.length; i >= 0; i--) {
498 hashCode += EqualsAndHashUtil.hash(arr[i]);
499 }
500 }
501 return hashCode;
502 }
503 }