1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
22
22
/**
23
23
* Miscellaneous object utility methods.
24
- * Mainly for internal use within the framework.
24
+ *
25
+ * <p>Mainly for internal use within the framework.
25
26
*
26
27
* <p>Thanks to Alex Ruiz for contributing several enhancements to this class!
27
28
*
31
32
* @author Rob Harrop
32
33
* @author Chris Beams
33
34
* @since 19.03.2004
35
+ * @see ClassUtils
36
+ * @see CollectionUtils
37
+ * @see StringUtils
34
38
*/
35
39
public abstract class ObjectUtils {
36
40
@@ -226,14 +230,14 @@ public static Object[] toObjectArray(Object source) {
226
230
//---------------------------------------------------------------------
227
231
228
232
/**
229
- * Determine if the given objects are equal, returning {@code true}
230
- * if both are {@code null} or {@code false} if only one is
231
- * {@code null}.
233
+ * Determine if the given objects are equal, returning {@code true} if
234
+ * both are {@code null} or {@code false} if only one is {@code null}.
232
235
* <p>Compares arrays with {@code Arrays.equals}, performing an equality
233
236
* check based on the array elements rather than the array reference.
234
237
* @param o1 first Object to compare
235
238
* @param o2 second Object to compare
236
239
* @return whether the given objects are equal
240
+ * @see Object#equals(Object)
237
241
* @see java.util.Arrays#equals
238
242
*/
239
243
public static boolean nullSafeEquals (Object o1 , Object o2 ) {
@@ -247,33 +251,47 @@ public static boolean nullSafeEquals(Object o1, Object o2) {
247
251
return true ;
248
252
}
249
253
if (o1 .getClass ().isArray () && o2 .getClass ().isArray ()) {
250
- if (o1 instanceof Object [] && o2 instanceof Object []) {
251
- return Arrays .equals ((Object []) o1 , (Object []) o2 );
252
- }
253
- if (o1 instanceof boolean [] && o2 instanceof boolean []) {
254
- return Arrays .equals ((boolean []) o1 , (boolean []) o2 );
255
- }
256
- if (o1 instanceof byte [] && o2 instanceof byte []) {
257
- return Arrays .equals ((byte []) o1 , (byte []) o2 );
258
- }
259
- if (o1 instanceof char [] && o2 instanceof char []) {
260
- return Arrays .equals ((char []) o1 , (char []) o2 );
261
- }
262
- if (o1 instanceof double [] && o2 instanceof double []) {
263
- return Arrays .equals ((double []) o1 , (double []) o2 );
264
- }
265
- if (o1 instanceof float [] && o2 instanceof float []) {
266
- return Arrays .equals ((float []) o1 , (float []) o2 );
267
- }
268
- if (o1 instanceof int [] && o2 instanceof int []) {
269
- return Arrays .equals ((int []) o1 , (int []) o2 );
270
- }
271
- if (o1 instanceof long [] && o2 instanceof long []) {
272
- return Arrays .equals ((long []) o1 , (long []) o2 );
273
- }
274
- if (o1 instanceof short [] && o2 instanceof short []) {
275
- return Arrays .equals ((short []) o1 , (short []) o2 );
276
- }
254
+ return arrayEquals (o1 , o2 );
255
+ }
256
+ return false ;
257
+ }
258
+
259
+ /**
260
+ * Compare the given arrays with {@code Arrays.equals}, performing an equality
261
+ * check based on the array elements rather than the array reference.
262
+ * @param o1 first array to compare
263
+ * @param o2 second array to compare
264
+ * @return whether the given objects are equal
265
+ * @see #nullSafeEquals(Object, Object)
266
+ * @see java.util.Arrays#equals
267
+ */
268
+ private static boolean arrayEquals (Object o1 , Object o2 ) {
269
+ if (o1 instanceof Object [] && o2 instanceof Object []) {
270
+ return Arrays .equals ((Object []) o1 , (Object []) o2 );
271
+ }
272
+ if (o1 instanceof boolean [] && o2 instanceof boolean []) {
273
+ return Arrays .equals ((boolean []) o1 , (boolean []) o2 );
274
+ }
275
+ if (o1 instanceof byte [] && o2 instanceof byte []) {
276
+ return Arrays .equals ((byte []) o1 , (byte []) o2 );
277
+ }
278
+ if (o1 instanceof char [] && o2 instanceof char []) {
279
+ return Arrays .equals ((char []) o1 , (char []) o2 );
280
+ }
281
+ if (o1 instanceof double [] && o2 instanceof double []) {
282
+ return Arrays .equals ((double []) o1 , (double []) o2 );
283
+ }
284
+ if (o1 instanceof float [] && o2 instanceof float []) {
285
+ return Arrays .equals ((float []) o1 , (float []) o2 );
286
+ }
287
+ if (o1 instanceof int [] && o2 instanceof int []) {
288
+ return Arrays .equals ((int []) o1 , (int []) o2 );
289
+ }
290
+ if (o1 instanceof long [] && o2 instanceof long []) {
291
+ return Arrays .equals ((long []) o1 , (long []) o2 );
292
+ }
293
+ if (o1 instanceof short [] && o2 instanceof short []) {
294
+ return Arrays .equals ((short []) o1 , (short []) o2 );
277
295
}
278
296
return false ;
279
297
}
@@ -284,6 +302,7 @@ public static boolean nullSafeEquals(Object o1, Object o2) {
284
302
* this method will delegate to any of the {@code nullSafeHashCode}
285
303
* methods for arrays in this class. If the object is {@code null},
286
304
* this method returns 0.
305
+ * @see Object#hashCode()
287
306
* @see #nullSafeHashCode(Object[])
288
307
* @see #nullSafeHashCode(boolean[])
289
308
* @see #nullSafeHashCode(byte[])
0 commit comments