Skip to content

Commit 14ab980

Browse files
committedJun 29, 2016
ObjectUtils.nullSafeEquals allows for JVM method inlining (through reducing its bytecode size)
Issue: SPR-14349 (cherry picked from commit 71df9ce)
1 parent a1c0987 commit 14ab980

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed
 

‎spring-core/src/main/java/org/springframework/util/ObjectUtils.java

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,8 @@
2121

2222
/**
2323
* Miscellaneous object utility methods.
24-
* Mainly for internal use within the framework.
24+
*
25+
* <p>Mainly for internal use within the framework.
2526
*
2627
* <p>Thanks to Alex Ruiz for contributing several enhancements to this class!
2728
*
@@ -31,6 +32,9 @@
3132
* @author Rob Harrop
3233
* @author Chris Beams
3334
* @since 19.03.2004
35+
* @see ClassUtils
36+
* @see CollectionUtils
37+
* @see StringUtils
3438
*/
3539
public abstract class ObjectUtils {
3640

@@ -226,14 +230,14 @@ public static Object[] toObjectArray(Object source) {
226230
//---------------------------------------------------------------------
227231

228232
/**
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}.
232235
* <p>Compares arrays with {@code Arrays.equals}, performing an equality
233236
* check based on the array elements rather than the array reference.
234237
* @param o1 first Object to compare
235238
* @param o2 second Object to compare
236239
* @return whether the given objects are equal
240+
* @see Object#equals(Object)
237241
* @see java.util.Arrays#equals
238242
*/
239243
public static boolean nullSafeEquals(Object o1, Object o2) {
@@ -247,33 +251,47 @@ public static boolean nullSafeEquals(Object o1, Object o2) {
247251
return true;
248252
}
249253
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);
277295
}
278296
return false;
279297
}
@@ -284,6 +302,7 @@ public static boolean nullSafeEquals(Object o1, Object o2) {
284302
* this method will delegate to any of the {@code nullSafeHashCode}
285303
* methods for arrays in this class. If the object is {@code null},
286304
* this method returns 0.
305+
* @see Object#hashCode()
287306
* @see #nullSafeHashCode(Object[])
288307
* @see #nullSafeHashCode(boolean[])
289308
* @see #nullSafeHashCode(byte[])

0 commit comments

Comments
 (0)
Please sign in to comment.