Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TempUtils |
|
| 6.75;6.75 |
1 | package org.jtheque.primary.utils; | |
2 | ||
3 | import org.jtheque.utils.Constants; | |
4 | import org.jtheque.utils.bean.EqualsUtils; | |
5 | ||
6 | /* | |
7 | * This file is part of JTheque. | |
8 | * | |
9 | * JTheque is free software: you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation, either version 3 of the License. | |
12 | * | |
13 | * JTheque is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | /** | |
23 | * A temporary utility class. Its methods will be moved to JTheque Utils on the last | |
24 | * version of this last. | |
25 | * | |
26 | * @author Baptiste Wicht | |
27 | */ | |
28 | public final class TempUtils { | |
29 | private static final int NUMBER_BIT_LENGTH = 32; | |
30 | ||
31 | /** | |
32 | * Utility class, not instanciable. | |
33 | */ | |
34 | private TempUtils(){ | |
35 | 0 | super(); |
36 | 0 | } |
37 | ||
38 | /** | |
39 | * Return the hash code of an object, using the properties in the given list. | |
40 | * | |
41 | * @param properties The properties to use to generate the hash code. | |
42 | * | |
43 | * @return The hash code of the bean . If there is no properties, the hash code will be 17. | |
44 | */ | |
45 | public static int hashCodeDirect(Object... properties){ | |
46 | 0 | int result = Constants.HASH_CODE_START; |
47 | ||
48 | 0 | for (Object property : properties){ |
49 | 0 | result = computeValue(result, property); |
50 | } | |
51 | ||
52 | 0 | return result; |
53 | } | |
54 | ||
55 | /** | |
56 | * Compute the value with the current result. | |
57 | * | |
58 | * @param result The current hash code result. | |
59 | * @param value The value to compute to the result. | |
60 | * | |
61 | * @return The result computed with the value. | |
62 | */ | |
63 | private static int computeValue(int result, Object value){ | |
64 | 0 | if (value instanceof Double){ |
65 | 0 | Long temp = Double.doubleToLongBits((Double) value); |
66 | 0 | return Constants.HASH_CODE_PRIME * result + (int) (temp ^ temp >>> NUMBER_BIT_LENGTH); |
67 | 0 | } else if (value instanceof Long){ |
68 | 0 | Long temp = (Long) value; |
69 | 0 | return Constants.HASH_CODE_PRIME * result + (int) (temp ^ temp >>> NUMBER_BIT_LENGTH); |
70 | 0 | } else if (value instanceof Boolean){ |
71 | 0 | return Constants.HASH_CODE_PRIME * result + ((Boolean) value ? 0 : 1); |
72 | 0 | } else if (value instanceof Float){ |
73 | 0 | return Constants.HASH_CODE_PRIME * result + Float.floatToIntBits((Float) value); |
74 | 0 | } else if (value instanceof Number){ |
75 | 0 | return Constants.HASH_CODE_PRIME * result + ((Number) value).intValue(); |
76 | } else { | |
77 | 0 | return Constants.HASH_CODE_PRIME * result + (value == null ? 0 : value.hashCode()); |
78 | } | |
79 | } | |
80 | ||
81 | /** | |
82 | * Test if the two objects are equals. | |
83 | * | |
84 | * @param bean The bean to test. | |
85 | * @param other The other bean to test for equality with the first one. | |
86 | * @param properties The properties to compare one by one. The properties n is compared to the property | |
87 | * n + (properties.length / 2). This arry must be pair. | |
88 | * | |
89 | * @return A boolean indicating if the two objects are equals or not. | |
90 | */ | |
91 | public static boolean areEqualsDirect(Object bean, Object other, Object... properties){ | |
92 | 0 | if (bean == other){ |
93 | 0 | return true; |
94 | } | |
95 | ||
96 | 0 | if (EqualsUtils.areObjectIncompatible(bean, other)){ |
97 | 0 | return false; |
98 | } | |
99 | ||
100 | 0 | int numberOfProperties = properties.length / 2; |
101 | ||
102 | 0 | for (int i = 0; i < numberOfProperties; i++){ |
103 | 0 | Object propertyBean = properties[i]; |
104 | 0 | Object propertyOther = properties[i + numberOfProperties]; |
105 | ||
106 | 0 | if (propertyBean == null){ |
107 | 0 | if (propertyOther != null){ |
108 | 0 | return false; |
109 | } | |
110 | 0 | } else if (!propertyBean.equals(propertyOther)){ |
111 | 0 | return false; |
112 | } | |
113 | } | |
114 | ||
115 | 0 | return true; |
116 | } | |
117 | } |