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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <A,
O extends A>
A[]addObjectToArray
(A[] array, 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>
A[]addObjectToArray
(A[] array, 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 boolean
containsConstant
(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 boolean
containsConstant
(Enum<?>[] enumValues, String constant, boolean caseSensitive) Check whether the given array of enum constants contains a constant with the given name.static boolean
containsElement
(Object[] array, Object element) Check whether the given array contains the given element.static String
getDisplayString
(Object obj) Return a content-based String representation ifobj
is notnull
; otherwise returns an empty String.static String
Return a hex String form of an object's identity hash code.static String
identityToString
(Object obj) Return a String representation of an object's overall identity.static boolean
Determine whether the given object is an array: either an Object array or a primitive array.static boolean
Return whether the given throwable is a checked exception: that is, neither a RuntimeException nor an Error.static boolean
isCompatibleWithThrowsClause
(Throwable ex, Class<?>... declaredExceptions) Check whether the given exception is compatible with the specified exception types, as declared in a throws clause.static boolean
Determine whether the given object is empty.static boolean
Determine whether the given array is empty: i.e.static String
nullSafeClassName
(Object obj) Determine the class name for the given object.static String
Generate a null-safe, concise string representation of the supplied object as described below.static boolean
nullSafeEquals
(Object o1, Object o2) Determine if the given objects are equal, returningtrue
if both arenull
orfalse
if only one isnull
.static int
nullSafeHashCode
(boolean[] array) Return a hash code based on the contents of the specified array.static int
nullSafeHashCode
(byte[] array) Return a hash code based on the contents of the specified array.static int
nullSafeHashCode
(char[] array) Return a hash code based on the contents of the specified array.static int
nullSafeHashCode
(double[] array) Return a hash code based on the contents of the specified array.static int
nullSafeHashCode
(float[] array) Return a hash code based on the contents of the specified array.static int
nullSafeHashCode
(int[] array) Return a hash code based on the contents of the specified array.static int
nullSafeHashCode
(long[] array) Return a hash code based on the contents of the specified array.static int
nullSafeHashCode
(short[] array) Return a hash code based on the contents of the specified array.static int
nullSafeHashCode
(Object obj) Return as hash code for the given object; typically the value ofObject#hashCode()
}.static int
nullSafeHashCode
(Object[] array) Return a hash code based on the contents of the specified array.static String
nullSafeToString
(boolean[] array) Return a String representation of the contents of the specified array.static String
nullSafeToString
(byte[] array) Return a String representation of the contents of the specified array.static String
nullSafeToString
(char[] array) Return a String representation of the contents of the specified array.static String
nullSafeToString
(double[] array) Return a String representation of the contents of the specified array.static String
nullSafeToString
(float[] array) Return a String representation of the contents of the specified array.static String
nullSafeToString
(int[] array) Return a String representation of the contents of the specified array.static String
nullSafeToString
(long[] array) Return a String representation of the contents of the specified array.static String
nullSafeToString
(short[] array) Return a String representation of the contents of the specified array.static String
nullSafeToString
(Object obj) Return a String representation of the specified Object.static String
nullSafeToString
(Object[] array) Return a String representation of the contents of the specified array.static Object[]
toObjectArray
(Object source) Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects).static Object
unwrapOptional
(Object obj) Unwrap the given object which is potentially aOptional
.
-
Constructor Details
-
ObjectUtils
public ObjectUtils()
-
-
Method Details
-
isCheckedException
Return 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:
-
isCompatibleWithThrowsClause
public static boolean isCompatibleWithThrowsClause(Throwable ex, @Nullable Class<?>... declaredExceptions) Check whether the given exception is compatible with the specified exception types, as declared in a throws clause.- Parameters:
ex
- the exception to checkdeclaredExceptions
- the exception types declared in the throws clause- Returns:
- whether the given exception is compatible
-
isArray
Determine whether the given object is an array: either an Object array or a primitive array.- Parameters:
obj
- the object to check
-
isEmpty
Determine whether the given array is empty: i.e.null
or of zero length.- Parameters:
array
- the array to check- See Also:
-
isEmpty
Determine whether the given object is empty.This method supports the following object types.
Optional
: considered empty if notOptional.isPresent()
Array
: considered empty if its length is zeroCharSequence
: considered empty if its length is zeroCollection
: delegates toCollection.isEmpty()
Map
: delegates toMap.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:
true
if the object isnull
or empty- Since:
- 4.2
- See Also:
-
unwrapOptional
Unwrap the given object which is potentially aOptional
.- Parameters:
obj
- the candidate object- Returns:
- either the value held within the
Optional
,null
if theOptional
is empty, or simply the given object as-is - Since:
- 5.0
-
containsElement
Check whether the given array contains the given element.- Parameters:
array
- the array to check (may benull
, in which case the return value will always befalse
)element
- the element to check for- Returns:
- whether the element has been found in the given array
-
containsConstant
Check 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 viaMyEnum.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
-
containsConstant
public 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 viaMyEnum.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
-
caseInsensitiveValueOf
Case insensitive alternative toEnum.valueOf(Class, String)
.- Type Parameters:
E
- the concrete Enum type- Parameters:
enumValues
- the array of all Enum constants in question, usually perEnum.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. UsecontainsConstant(Enum[], String)
as a guard to avoid this exception.
-
addObjectToArray
Append 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 benull
)obj
- the object to append- Returns:
- the new array (of the same component type; never
null
)
-
addObjectToArray
public static <A,O extends A> A[] addObjectToArray(@Nullable A[] 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 benull
)obj
- the object to appendposition
- the position at which to add the object- Returns:
- the new array (of the same component type; never
null
) - Since:
- 6.0
-
toObjectArray
Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects).A
null
source value 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
Determine if the given objects are equal, returningtrue
if both arenull
orfalse
if 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 compareo2
- second Object to compare- Returns:
- whether the given objects are equal
- See Also:
-
nullSafeHashCode
Return as hash code for the given object; typically the value ofObject#hashCode()
}. If the object is an array, this method will delegate to any of thenullSafeHashCode
methods for arrays in this class. If the object isnull
, this method returns 0. -
nullSafeHashCode
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0. -
nullSafeHashCode
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0. -
nullSafeHashCode
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0. -
nullSafeHashCode
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0. -
nullSafeHashCode
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0. -
nullSafeHashCode
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0. -
nullSafeHashCode
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0. -
nullSafeHashCode
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0. -
nullSafeHashCode
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0. -
identityToString
Return a String representation of an object's overall identity.- Parameters:
obj
- the object (may benull
)- Returns:
- the object's identity as String representation,
or an empty String if the object was
null
-
getIdentityHexString
Return a hex String form of an object's identity hash code.- Parameters:
obj
- the object- Returns:
- the object's identity code in hex notation
-
getDisplayString
Return a content-based String representation ifobj
is notnull
; otherwise returns an empty String.Differs from
nullSafeToString(Object)
in that it returns an empty String rather than "null" for anull
value.- Parameters:
obj
- the object to build a display String for- Returns:
- a display String representation of
obj
- See Also:
-
nullSafeClassName
Determine the class name for the given object.Returns a
"null"
String ifobj
isnull
.- Parameters:
obj
- the object to introspect (may benull
)- Returns:
- the corresponding class name
-
nullSafeToString
Return a String representation of the specified Object.Builds a String representation of the contents in case of an array. Returns a
"null"
String ifobj
isnull
.- Parameters:
obj
- the object to build a String representation for- Returns:
- a String representation of
obj
- See Also:
-
nullSafeToString
Return 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 ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
Return 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 ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
Return 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 ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
Return 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 ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
Return 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 ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
Return 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 ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
Return 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 ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
Return 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 ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
Return 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 ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeConciseToString
Generate 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"
ifobj
isnull
"Optional.empty"
ifobj
is an emptyOptional
"Optional[<concise-string>]"
ifobj
is a non-emptyOptional
, where<concise-string>
is the result of invoking this method on the object contained in theOptional
"{}"
ifobj
is an empty array"{...}"
ifobj
is aMap
or a non-empty array"[...]"
ifobj
is aCollection
- Class name if
obj
is aClass
- Charset name if
obj
is aCharset
- TimeZone ID if
obj
is aTimeZone
- Zone ID if
obj
is aZoneId
- Potentially truncated string
if
obj
is aString
orCharSequence
- Potentially truncated string
if
obj
is 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:
-