some util classes stolen from webfunds
[cacert-birdshack.git] / Business Logic / trunk / src / org / cacert / birdshack / util / Equals.java
1 /*
2 * Was webfunds.util.Example, contributed.
3 */
4 package org.cacert.birdshack.util;
5
6 import java.util.HashMap;
7
8 /**
9 * A set of equality classes, for use in equals() methods.
10 *
11 * Similar role to org.apache.commons.lang.builder.EqualsBuilder ?
12 */
13 public class Equals
14 {
15 private Equals() {}
16
17 /**
18 * Only set this if you are running tests.
19 * Causes diags on "false" results.
20 */
21 public static void setNoisy() { _noisy = true; }
22 public static void resetNoisy() { _noisy = false; }
23 private static boolean _noisy = false;
24
25 /**
26 * @return True if they are the same object
27 * True if both null, false if only one is null.
28 */
29 public static boolean sameClass(Object one, Object two)
30 {
31 if (one == null && two == null)
32 return true;
33 if (one == null)
34 return noisy("Obj!=Obj 1st is <null>, 2nd: ", two);
35 if (two == null)
36 return noisy("Obj!=Obj 2nd is <null>, 1st: ", one);
37
38 // both != null
39 String classone = one.getClass().getName();
40 String classtwo = two.getClass().getName();
41 if( !classone.equals(classtwo) )
42 {
43 return noisy("Class!=Class " + classone + "!=" + classtwo);
44 }
45
46 return true;
47 }
48
49 /**
50 * @return true if the byte arrays are equal, false if not
51 */
52 public static boolean equals(byte[] b1, byte[] b2)
53 {
54 if (b1 == null && b2 == null)
55 return true ;
56 if (b1 == null)
57 return noisy("byte[] 1st is <null>");
58 if (b2 == null)
59 return noisy("byte[] 2nd is <null>");
60 int len1 = b1.length;
61 int len2 = b2.length;
62 if (len1 != len2)
63 return noisy("byte[] len "+len1+" (1st) != "+len2+" (2nd)");
64
65 // check if the data components are equivalent
66 String s1 = new String(b1);
67 String s2 = new String(b2);
68
69 if (! s1.equals(s2))
70 {
71 return noisy("byte[] contents differ (len " + len1 + ")" +
72 ONE + "\n" +
73 Hex.printable(b1) + "\n" +
74 TWO + "\n" +
75 Hex.printable(b2) + "\n" +
76 END);
77 }
78
79 return true ;
80 }
81
82 /**
83 * Equals for parent objects (handles null case).
84 * Works as long as you know at least one is the right Class
85 * (for example, was already type-checked).
86 * @param one, two are objects
87 * @return true if both null, false if one null,
88 * false if different class, else returns one.equals(two)
89 */
90 public static boolean equals(Object one, Object two)
91 {
92 if (one == null && two == null)
93 return true;
94 if (!Equals.sameClass(one, two)) // catches one == null
95 return noisy("Obj,Obj Not Equals(classes)!");
96
97 if (!one.equals(two))
98 return noisy("Obj,Obj Not Equals!", one, two);
99
100 return true;
101 }
102
103 /**
104 * Equals (list) for parent objects (handles null case).
105 *
106 * @param one, two are lists of Id
107 * @return true if both null, false if one null,
108 * else returns AND(one[].equals(two[]))
109 */
110 public static boolean equals(Object one[], Object two[])
111 {
112 if (one == null && two == null)
113 return true;
114 if (one == null)
115 return noisy("Obj[] 1st is <null> ( 2nd" + two.length + ")");
116 if (one == null)
117 return noisy("Obj[] 2nd is <null> ( 1st" + one.length + ")");
118 int len1 = one.length;
119 int len2 = two.length;
120 if (len1 != len2)
121 return noisy("Obj[] len "+len1+" (1st) != "+len2+" (2nd)");
122
123 for (int i = 0; i < len1; i++)
124 {
125 if (!equals(one[i], two[i]))
126 return noisy("Obj[" + i + "] not Equals!"); // dumped already
127 }
128
129 return true;
130 }
131
132
133 /**
134 * Equals (list) for HashMaps of objects (handles null case).
135 *
136 * @param one, two are HashMaps
137 * @return true if both null, false if one null,
138 * else returns AND(one[].equals(two[]))
139 */
140 public static boolean equalsHashMap(HashMap one, HashMap two)
141 {
142 if (one == null && two == null)
143 return true;
144 if (one == null)
145 return noisy("HashMap first map is <Null>");
146 if (two == null)
147 return noisy("HashMap second map is <Null>");
148
149 java.util.Set x = one.keySet();
150 int len = x.size();
151 java.util.Set y = two.keySet();
152 if (len != y.size())
153 return noisy("HashMap sizes "+len+" (1st) != "+y.size()+" (2nd)");
154
155 java.util.Iterator keys = x.iterator();
156 while (keys.hasNext())
157 {
158 Object key = keys.next();
159 Object oneObj = one.get(key);
160 Object twoObj = two.get(key);
161 /*
162 * With HashMaps, have to check special case of
163 * value == null versus key not present.
164 */
165 if (oneObj == null && twoObj == null)
166 {
167 if (one.containsKey(key) != two.containsKey(key))
168 return noisy("HashMap has unmatched ref-><null>: " + key);
169 }
170 else if (oneObj == null)
171 {
172 return noisy("HashMap ref: " + key + " 1st null ", twoObj);
173 }
174 else if (twoObj == null)
175 {
176 return noisy("HashMap ref: " + key + " 2st null ", oneObj);
177 }
178 else
179 {
180 if (!oneObj.equals(twoObj))
181 {
182 return noisy("HashMap ref: " + key, oneObj, twoObj);
183 }
184 }
185 }
186
187 return true;
188 }
189
190
191 /**
192 * Display a diag on the screen if noise is turned on.
193 */
194 private static boolean noisy(String s)
195 {
196 if (_noisy)
197 {
198 System.err.println(s);
199 }
200 return false;
201 }
202
203 /**
204 * Display a diag on the screen if noise is turned on.
205 */
206 private static boolean noisy(String s, Object o)
207 {
208 if (_noisy)
209 {
210 System.err.println(s);
211 dump(o);
212 }
213 return false;
214 }
215
216 /**
217 * Display a diag on the screen if noise is turned on.
218 */
219 private static void dump(Object o)
220 {
221 if (o == null)
222 {
223 System.err.println("Object: <null>");
224 return ;
225 }
226 System.err.println("Object: " + o.getClass().getName());
227 System.err.println(o.toString());
228 }
229
230 private static final String
231 ONE = "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ",
232 TWO = "2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ",
233 END = "^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ";
234
235 /**
236 * Display a diag on the screen if noise is turned on.
237 */
238 private static boolean noisy(String s, Object one, Object two)
239 {
240 if (_noisy)
241 {
242 System.err.println(s + "\n");
243 System.err.println(ONE);
244 dump(one);
245 System.err.println(TWO);
246 dump(two);
247 System.err.println(END);
248 }
249 return false;
250 }
251 }