Class ObjectUtils
Mainly for internal use within the framework.
Thanks to Alex Ruiz for contributing several enhancements to this class!
- Since:
- 19.03.2004
- Author:
- Juergen Hoeller, Keith Donald, Rod Johnson, Rob Harrop, Chris Beams, Sam Brannen
- See Also:
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic <A, O extends A>
 A[]addObjectToArray(A @Nullable [] array, @Nullable O obj) Append the given object to the given array, returning a new array consisting of the input array contents plus the given object.static <A, O extends A>
 @Nullable A[]addObjectToArray(A @Nullable [] array, @Nullable O obj, int position) Add the given object to the given array at the specified position, returning a new array consisting of the input array contents plus the given object.static <E extends Enum<?>>
 EcaseInsensitiveValueOf(E[] enumValues, String constant) Case insensitive alternative toEnum.valueOf(Class, String).static booleancontainsConstant(Enum<?>[] enumValues, String constant) Check whether the given array of enum constants contains a constant with the given name, ignoring case when determining a match.static booleancontainsConstant(Enum<?>[] enumValues, String constant, boolean caseSensitive) Check whether the given array of enum constants contains a constant with the given name.static booleanCheck whether the given array contains the given element.static StringReturn a content-based String representation ifobjis notnull; otherwise returns an empty String.static StringReturn a hex String form of an object's identity hash code.static StringReturn a String representation of an object's overall identity.static booleanDetermine whether the given object is an array: either an Object array or a primitive array.static booleanReturn whether the given throwable is a checked exception: that is, neither a RuntimeException nor an Error.static booleanisCompatibleWithThrowsClause(Throwable ex, Class<?> @Nullable ... declaredExceptions) Check whether the given exception is compatible with the specified exception types, as declared in athrowsclause.static booleanDetermine whether the given object is empty.static booleanDetermine whether the given array is empty: i.e.static StringDetermine the class name for the given object.static StringGenerate a null-safe, concise string representation of the supplied object as described below.static booleannullSafeEquals(@Nullable Object o1, @Nullable Object o2) Determine if the given objects are equal, returningtrueif both arenullorfalseif only one isnull.static intnullSafeHash(@Nullable Object @Nullable ... elements) Return a hash code for the given elements, delegating tonullSafeHashCode(Object)for each element.static intnullSafeHashCode(boolean @Nullable [] array) Deprecated.static intnullSafeHashCode(byte @Nullable [] array) Deprecated.as of 6.1 in favor ofArrays.hashCode(byte[])static intnullSafeHashCode(char @Nullable [] array) Deprecated.as of 6.1 in favor ofArrays.hashCode(char[])static intnullSafeHashCode(double @Nullable [] array) Deprecated.as of 6.1 in favor ofArrays.hashCode(double[])static intnullSafeHashCode(float @Nullable [] array) Deprecated.as of 6.1 in favor ofArrays.hashCode(float[])static intnullSafeHashCode(int @Nullable [] array) Deprecated.as of 6.1 in favor ofArrays.hashCode(int[])static intnullSafeHashCode(long @Nullable [] array) Deprecated.as of 6.1 in favor ofArrays.hashCode(long[])static intnullSafeHashCode(short @Nullable [] array) Deprecated.as of 6.1 in favor ofArrays.hashCode(short[])static intReturn a hash code for the given object, typically the value ofObject.hashCode().static intnullSafeHashCode(Object @Nullable [] array) Deprecated.as of 6.1 in favor ofArrays.hashCode(Object[])static StringnullSafeToString(boolean @Nullable [] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(byte @Nullable [] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(char @Nullable [] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(double @Nullable [] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(float @Nullable [] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(int @Nullable [] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(long @Nullable [] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(short @Nullable [] array) Return a String representation of the contents of the specified array.static StringReturn a String representation of the specified Object.static StringnullSafeToString(@Nullable Object @Nullable [] array) Return a String representation of the contents of the specified array.static Object[]toObjectArray(@Nullable Object source) Convert the given array (which may be a primitive array) to an object array (if necessary, to an array of primitive wrapper objects).Unwrap the given object which is potentially aOptional.
- 
Constructor Details- 
ObjectUtilspublic ObjectUtils()
 
- 
- 
Method Details- 
isCheckedExceptionReturn whether the given throwable is a checked exception: that is, neither a RuntimeException nor an Error.- Parameters:
- ex- the throwable to check
- Returns:
- whether the throwable is a checked exception
- See Also:
 
- 
isCompatibleWithThrowsClausepublic static boolean isCompatibleWithThrowsClause(Throwable ex, Class<?> @Nullable ... declaredExceptions) Check whether the given exception is compatible with the specified exception types, as declared in athrowsclause.- Parameters:
- ex- the exception to check
- declaredExceptions- the exception types declared in the throws clause
- Returns:
- whether the given exception is compatible
 
- 
isArray
- 
isEmpty
- 
isEmptyDetermine whether the given object is empty.This method supports the following object types. - Optional: considered empty if not- Optional.isPresent()
- Array: considered empty if its length is zero
- CharSequence: considered empty if its length is zero
- Collection: delegates to- Collection.isEmpty()
- Map: delegates to- Map.isEmpty()
 If the given object is non-null and not one of the aforementioned supported types, this method returns false.- Parameters:
- obj- the object to check
- Returns:
- trueif the object is- nullor empty
- Since:
- 4.2
- See Also:
 
- 
unwrapOptionalUnwrap the given object which is potentially aOptional.- Parameters:
- obj- the candidate object
- Returns:
- either the value held within the Optional,nullif theOptionalis empty, or simply the given object as-is
- Since:
- 5.0
 
- 
containsElement@Contract("null, _ -> false") public static boolean containsElement(@Nullable Object @Nullable [] array, @Nullable Object element) Check whether the given array contains the given element.- Parameters:
- array- the array to check (may be- null, in which case the return value will always be- false)
- element- the element to check for
- Returns:
- whether the element has been found in the given array
 
- 
containsConstantCheck whether the given array of enum constants contains a constant with the given name, ignoring case when determining a match.- Parameters:
- enumValues- the enum values to check, typically obtained via- MyEnum.values()
- constant- the constant name to find (must not be null or empty string)
- Returns:
- whether the constant has been found in the given array
 
- 
containsConstantpublic static boolean containsConstant(Enum<?>[] enumValues, String constant, boolean caseSensitive) Check whether the given array of enum constants contains a constant with the given name.- Parameters:
- enumValues- the enum values to check, typically obtained via- MyEnum.values()
- constant- the constant name to find (must not be null or empty string)
- caseSensitive- whether case is significant in determining a match
- Returns:
- whether the constant has been found in the given array
 
- 
caseInsensitiveValueOfCase insensitive alternative toEnum.valueOf(Class, String).- Type Parameters:
- E- the concrete Enum type
- Parameters:
- enumValues- the array of all Enum constants in question, usually per- Enum.values()
- constant- the constant to get the enum value of
- Throws:
- IllegalArgumentException- if the given constant is not found in the given array of enum values. Use- containsConstant(Enum[], String)as a guard to avoid this exception.
 
- 
addObjectToArrayAppend the given object to the given array, returning a new array consisting of the input array contents plus the given object.- Parameters:
- array- the array to append to (can be- null)
- obj- the object to append
- Returns:
- the new array (of the same component type; never null)
 
- 
addObjectToArraypublic static <A, O extends A> @Nullable A[] addObjectToArray(A @Nullable [] array, @Nullable O obj, int position) Add the given object to the given array at the specified position, returning a new array consisting of the input array contents plus the given object.- Parameters:
- array- the array to add to (can be- null)
- obj- the object to append
- position- the position at which to add the object
- Returns:
- the new array (of the same component type; never null)
- Since:
- 6.0
 
- 
toObjectArrayConvert the given array (which may be a primitive array) to an object array (if necessary, to an array of primitive wrapper objects).A nullsource value or empty primitive array will be converted to an empty Object array.- Parameters:
- source- the (potentially primitive) array
- Returns:
- the corresponding object array (never null)
- Throws:
- IllegalArgumentException- if the parameter is not an array
 
- 
nullSafeEquals@Contract("null, null -> true; null, _ -> false; _, null -> false") public static boolean nullSafeEquals(@Nullable Object o1, @Nullable Object o2) Determine if the given objects are equal, returningtrueif both arenullorfalseif only one isnull.Compares arrays with Arrays.equals, performing an equality check based on the array elements rather than the array reference.- Parameters:
- o1- first Object to compare
- o2- second Object to compare
- Returns:
- whether the given objects are equal
- See Also:
 
- 
nullSafeHashReturn a hash code for the given elements, delegating tonullSafeHashCode(Object)for each element. Contrary toObjects.hash(Object...), this method can handle an element that is an array.- Parameters:
- elements- the elements to be hashed
- Returns:
- a hash value of the elements
- Since:
- 6.1
 
- 
nullSafeHashCodeReturn a hash code for the given object, typically the value ofObject.hashCode(). If the object is an array, this method will delegate to one of theArrays.hashCodemethods. If the object isnull, this method returns0.- See Also:
 
- 
nullSafeHashCodeDeprecated.as of 6.1 in favor ofArrays.hashCode(Object[])Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0.
- 
nullSafeHashCodeDeprecated.as of 6.1 in favor ofArrays.hashCode(boolean[])Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0.
- 
nullSafeHashCodeDeprecated.as of 6.1 in favor ofArrays.hashCode(byte[])Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0.
- 
nullSafeHashCodeDeprecated.as of 6.1 in favor ofArrays.hashCode(char[])Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0.
- 
nullSafeHashCodeDeprecated.as of 6.1 in favor ofArrays.hashCode(double[])Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0.
- 
nullSafeHashCodeDeprecated.as of 6.1 in favor ofArrays.hashCode(float[])Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0.
- 
nullSafeHashCodeDeprecated.as of 6.1 in favor ofArrays.hashCode(int[])Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0.
- 
nullSafeHashCodeDeprecated.as of 6.1 in favor ofArrays.hashCode(long[])Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0.
- 
nullSafeHashCodeDeprecated.as of 6.1 in favor ofArrays.hashCode(short[])Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0.
- 
identityToString
- 
getIdentityHexString
- 
getDisplayStringReturn a content-based String representation ifobjis notnull; otherwise returns an empty String.Differs from nullSafeToString(Object)in that it returns an empty String rather than "null" for anullvalue.- Parameters:
- obj- the object to build a display String for
- Returns:
- a display String representation of obj
- See Also:
 
- 
nullSafeClassName
- 
nullSafeToStringReturn a String representation of the specified Object.Builds a String representation of the contents in case of an array. Returns a "null"String ifobjisnull.- Parameters:
- obj- the object to build a String representation for
- Returns:
- a String representation of obj
- See Also:
 
- 
nullSafeToStringReturn a String representation of the contents of the specified array.The String representation consists of a list of the array's elements, enclosed in curly braces ( "{}"). Adjacent elements are separated by the characters", "(a comma followed by a space). Returns a"null"String ifarrayisnull.- Parameters:
- array- the array to build a String representation for
- Returns:
- a String representation of array
 
- 
nullSafeToStringReturn a String representation of the contents of the specified array.The String representation consists of a list of the array's elements, enclosed in curly braces ( "{}"). Adjacent elements are separated by the characters", "(a comma followed by a space). Returns a"null"String ifarrayisnull.- Parameters:
- array- the array to build a String representation for
- Returns:
- a String representation of array
 
- 
nullSafeToStringReturn a String representation of the contents of the specified array.The String representation consists of a list of the array's elements, enclosed in curly braces ( "{}"). Adjacent elements are separated by the characters", "(a comma followed by a space). Returns a"null"String ifarrayisnull.- Parameters:
- array- the array to build a String representation for
- Returns:
- a String representation of array
 
- 
nullSafeToStringReturn a String representation of the contents of the specified array.The String representation consists of a list of the array's elements, enclosed in curly braces ( "{}"). Adjacent elements are separated by the characters", "(a comma followed by a space). Returns a"null"String ifarrayisnull.- Parameters:
- array- the array to build a String representation for
- Returns:
- a String representation of array
 
- 
nullSafeToStringReturn a String representation of the contents of the specified array.The String representation consists of a list of the array's elements, enclosed in curly braces ( "{}"). Adjacent elements are separated by the characters", "(a comma followed by a space). Returns a"null"String ifarrayisnull.- Parameters:
- array- the array to build a String representation for
- Returns:
- a String representation of array
 
- 
nullSafeToStringReturn a String representation of the contents of the specified array.The String representation consists of a list of the array's elements, enclosed in curly braces ( "{}"). Adjacent elements are separated by the characters", "(a comma followed by a space). Returns a"null"String ifarrayisnull.- Parameters:
- array- the array to build a String representation for
- Returns:
- a String representation of array
 
- 
nullSafeToStringReturn a String representation of the contents of the specified array.The String representation consists of a list of the array's elements, enclosed in curly braces ( "{}"). Adjacent elements are separated by the characters", "(a comma followed by a space). Returns a"null"String ifarrayisnull.- Parameters:
- array- the array to build a String representation for
- Returns:
- a String representation of array
 
- 
nullSafeToStringReturn a String representation of the contents of the specified array.The String representation consists of a list of the array's elements, enclosed in curly braces ( "{}"). Adjacent elements are separated by the characters", "(a comma followed by a space). Returns a"null"String ifarrayisnull.- Parameters:
- array- the array to build a String representation for
- Returns:
- a String representation of array
 
- 
nullSafeToStringReturn a String representation of the contents of the specified array.The String representation consists of a list of the array's elements, enclosed in curly braces ( "{}"). Adjacent elements are separated by the characters", "(a comma followed by a space). Returns a"null"String ifarrayisnull.- Parameters:
- array- the array to build a String representation for
- Returns:
- a String representation of array
 
- 
nullSafeConciseToStringGenerate a null-safe, concise string representation of the supplied object as described below.Favor this method over nullSafeToString(Object)when you need the length of the generated string to be limited.Returns: - "null"if- objis- null
- "Optional.empty"if- objis an empty- Optional
- "Optional[<concise-string>]"if- objis a non-empty- Optional, where- <concise-string>is the result of invoking this method on the object contained in the- Optional
- "{}"if- objis an empty array
- "{...}"if- objis a- Mapor a non-empty array
- "[...]"if- objis a- Collection
- Class name if objis aClass
- Charset name if objis aCharset
- TimeZone ID if objis aTimeZone
- Zone ID if objis aZoneId
- Potentially truncated string
 if objis aStringorCharSequence
- Potentially truncated string
 if objis a simple value type whosetoString()method returns a non-null value
- Otherwise, a string representation of the object's type name concatenated
 with "@"and a hex string form of the object's identity hash code
 In the context of this method, a simple value type is any of the following: primitive wrapper (excluding Void),Enum,Number,Date,Temporal,File,Path,URI,URL,InetAddress,Currency,Locale,UUID,Pattern.- Parameters:
- obj- the object to build a string representation for
- Returns:
- a concise string representation of the supplied object
- Since:
- 5.3.27
- See Also:
 
 
- 
Arrays.hashCode(boolean[])