Top Description Fields Constructors Methods
org.springframework.util

public abstract Class ObjectUtils

extends Object
Class Inheritance
Imports
java.lang.reflect.Array, java.nio.charset.Charset, java.time.ZoneId, java.util.Arrays, .Collection, .Map, .Objects, .Optional, .StringJoiner, .TimeZone, org.springframework.lang.Nullable

Miscellaneous object utility methods.

Mainly for internal use within the framework.

Thanks to Alex Ruiz for contributing several enhancements to this class!

Authors
Juergen Hoeller, Keith Donald, Rod Johnson, Rob Harrop, Chris Beams, Sam Brannen
Since
19.03.2004
See Also
ClassUtils, CollectionUtils, StringUtils

Field Summary

Modifier and TypeField and Description
private static final String
private static final String
private static final String
private static final String
private static final String
private static final Object[]
private static final String
private static final String
private static final String
private static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static <A, O extends A> A[]

Returns:

the new array (of the same component type; never null)
addObjectToArray
(A[]
the array to append to (can be null)
array
,
O
the object to append
obj
)

Append the given object to the given array, returning a new array consisting of the input array contents plus the given object.

public static <A, O extends A> A[]

Returns:

the new array (of the same component type; never null)
addObjectToArray
(A[]
the array to add to (can be null)
array
,
O
the object to append
obj
,
int
the position at which to add the object
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.

private static boolean

Returns:

whether the given objects are equal
arrayEquals
(Object
first array to compare
o1
,
Object
second array to compare
o2
)

Compare the given arrays with Arrays.equals, performing an equality check based on the array elements rather than the array reference.

public static <
the concrete Enum type
E extends Enum<?>
>
E
caseInsensitiveValueOf(E[]
the array of all Enum constants in question, usually per Enum.values()
enumValues
,
String
the constant to get the enum value of
constant
)

Case insensitive alternative to Enum#valueOf(Class, String).

public static boolean

Returns:

whether the constant has been found in the given array
containsConstant
(Enum<?>[]
the enum values to check, typically obtained via MyEnum.values()
enumValues
,
String
the constant name to find (must not be null or empty string)
constant
)

Check whether the given array of enum constants contains a constant with the given name, ignoring case when determining a match.

public static boolean

Returns:

whether the constant has been found in the given array
containsConstant
(Enum<?>[]
the enum values to check, typically obtained via MyEnum.values()
enumValues
,
String
the constant name to find (must not be null or empty string)
constant
,
boolean
whether case is significant in determining a match
caseSensitive
)

Check whether the given array of enum constants contains a constant with the given name.

public static boolean

Returns:

whether the element has been found in the given array
containsElement
(Object[]
the array to check (may be null, in which case the return value will always be false)
array
,
Object
the element to check for
element
)

Check whether the given array contains the given element.

public static String

Returns:

a display String representation of obj
getDisplayString
(Object
the object to build a display String for
obj
)

Return a content-based String representation if obj is not null; otherwise returns an empty String.

public static String

Returns:

the object's identity code in hex notation
getIdentityHexString
(Object
the object
obj
)

Return a hex String form of an object's identity hash code.

public static String

Returns:

the object's identity as String representation, or an empty String if the object was null
identityToString
(Object
the object (may be null)
obj
)

Return a String representation of an object's overall identity.

public static boolean
isArray(Object
the object to check
obj
)

Determine whether the given object is an array: either an Object array or a primitive array.

public static boolean

Returns:

whether the throwable is a checked exception
isCheckedException
(Throwable
the throwable to check
ex
)

Return whether the given throwable is a checked exception: that is, neither a RuntimeException nor an Error.

public static boolean

Returns:

whether the given exception is compatible
isCompatibleWithThrowsClause
(Throwable
the exception to check
ex
,
Class<?>...
the exception types declared in the throws clause
declaredExceptions
)

Check whether the given exception is compatible with the specified exception types, as declared in a throws clause.

public static boolean
isEmpty(Object[]
the array to check
array
)

Determine whether the given array is empty: i.e. null or of zero length.

public static boolean

Returns:

true if the object is null or empty
isEmpty
(Object
the object to check
obj
)

Determine whether the given object is empty.

public static String

Returns:

the corresponding class name
nullSafeClassName
(Object
the object to introspect (may be null)
obj
)

Determine the class name for the given object.

public static String

Returns:

a concise string representation of the supplied object
nullSafeConciseToString
(Object
the object to build a string representation for
obj
)

Generate a null-safe, concise string representation of the supplied object as described below.

public static boolean

Returns:

whether the given objects are equal
nullSafeEquals
(Object
first Object to compare
o1
,
Object
second Object to compare
o2
)

Determine if the given objects are equal, returning true if both are null or false if only one is null.

public static int

Returns:

a hash value of the elements
nullSafeHash
(Object...
the elements to be hashed
elements
)

Return a hash code for the given elements, delegating to nullSafeHashCode(Object) for each element.

public static int
nullSafeHashCode(Object obj)

Return a hash code for the given object; typically the value of Object#hashCode()}.

public static int
nullSafeHashCode(Object[] array)
Deprecated since 6.1. as of 6.1 in favor of Arrays#hashCode(Object[])

Return a hash code based on the contents of the specified array.

public static int
nullSafeHashCode(boolean[] array)
Deprecated since 6.1. as of 6.1 in favor of Arrays#hashCode(boolean[])

Return a hash code based on the contents of the specified array.

public static int
nullSafeHashCode(byte[] array)
Deprecated since 6.1. as of 6.1 in favor of Arrays#hashCode(byte[])

Return a hash code based on the contents of the specified array.

public static int
nullSafeHashCode(char[] array)
Deprecated since 6.1. as of 6.1 in favor of Arrays#hashCode(char[])

Return a hash code based on the contents of the specified array.

public static int
nullSafeHashCode(double[] array)
Deprecated since 6.1. as of 6.1 in favor of Arrays#hashCode(double[])

Return a hash code based on the contents of the specified array.

public static int
nullSafeHashCode(float[] array)
Deprecated since 6.1. as of 6.1 in favor of Arrays#hashCode(float[])

Return a hash code based on the contents of the specified array.

public static int
nullSafeHashCode(int[] array)
Deprecated since 6.1. as of 6.1 in favor of Arrays#hashCode(int[])

Return a hash code based on the contents of the specified array.

public static int
nullSafeHashCode(long[] array)
Deprecated since 6.1. as of 6.1 in favor of Arrays#hashCode(long[])

Return a hash code based on the contents of the specified array.

public static int
nullSafeHashCode(short[] array)
Deprecated since 6.1. as of 6.1 in favor of Arrays#hashCode(short[])

Return a hash code based on the contents of the specified array.

public static String

Returns:

a String representation of obj
nullSafeToString
(Object
the object to build a String representation for
obj
)

Return a String representation of the specified Object.

public static String

Returns:

a String representation of array
nullSafeToString
(Object[]
the array to build a String representation for
array
)

Return a String representation of the contents of the specified array.

public static String

Returns:

a String representation of array
nullSafeToString
(boolean[]
the array to build a String representation for
array
)

Return a String representation of the contents of the specified array.

public static String

Returns:

a String representation of array
nullSafeToString
(byte[]
the array to build a String representation for
array
)

Return a String representation of the contents of the specified array.

public static String

Returns:

a String representation of array
nullSafeToString
(char[]
the array to build a String representation for
array
)

Return a String representation of the contents of the specified array.

public static String

Returns:

a String representation of array
nullSafeToString
(double[]
the array to build a String representation for
array
)

Return a String representation of the contents of the specified array.

public static String

Returns:

a String representation of array
nullSafeToString
(float[]
the array to build a String representation for
array
)

Return a String representation of the contents of the specified array.

public static String

Returns:

a String representation of array
nullSafeToString
(int[]
the array to build a String representation for
array
)

Return a String representation of the contents of the specified array.

public static String

Returns:

a String representation of array
nullSafeToString
(long[]
the array to build a String representation for
array
)

Return a String representation of the contents of the specified array.

public static String

Returns:

a String representation of array
nullSafeToString
(short[]
the array to build a String representation for
array
)

Return a String representation of the contents of the specified array.

public static Object[]

Returns:

the corresponding object array (never null)
toObjectArray
(Object
the (potentially primitive) array
source
)

Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects).

public static Object

Returns:

either the value held within the Optional, null if the Optional is empty, or simply the given object as-is
unwrapOptional
(Object
the candidate object
obj
)

Unwrap the given object which is potentially a java.util.Optional.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ARRAY_ELEMENT_SEPARATORback to summary
private static final String ARRAY_ELEMENT_SEPARATOR
ARRAY_ENDback to summary
private static final String ARRAY_END
ARRAY_STARTback to summary
private static final String ARRAY_START
COLLECTIONback to summary
private static final String COLLECTION
EMPTY_ARRAYback to summary
private static final String EMPTY_ARRAY
EMPTY_OBJECT_ARRAYback to summary
private static final Object[] EMPTY_OBJECT_ARRAY
EMPTY_STRINGback to summary
private static final String EMPTY_STRING
MAPback to summary
private static final String MAP
NON_EMPTY_ARRAYback to summary
private static final String NON_EMPTY_ARRAY
NULL_STRINGback to summary
private static final String NULL_STRING

Constructor Detail

ObjectUtilsback to summary
public ObjectUtils()

Method Detail

addObjectToArrayback to summary
public static <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.

Parameters
array:A[]

the array to append to (can be null)

obj:O

the object to append

Returns:A[]

the new array (of the same component type; never null)

addObjectToArrayback to summary
public 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.

Parameters
array:A[]

the array to add to (can be null)

obj:O

the object to append

position:int

the position at which to add the object

Returns:A[]

the new array (of the same component type; never null)

Since
6.0
arrayEqualsback to summary
private static boolean arrayEquals(Object o1, Object o2)

Compare the given arrays with Arrays.equals, performing an equality check based on the array elements rather than the array reference.

Parameters
o1:Object

first array to compare

o2:Object

second array to compare

Returns:boolean

whether the given objects are equal

See Also
nullSafeEquals(Object, Object), java.util.Arrays#equals
caseInsensitiveValueOfback to summary
public static <E extends Enum<?>> E caseInsensitiveValueOf(E[] enumValues, String constant)

Case insensitive alternative to Enum#valueOf(Class, String).

Parameters
<E>
the concrete Enum type
enumValues:E[]

the array of all Enum constants in question, usually per Enum.values()

constant:String

the constant to get the enum value of

Exceptions
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.
containsConstantback to summary
public 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.

Parameters
enumValues:Enum<?>[]

the enum values to check, typically obtained via MyEnum.values()

constant:String

the constant name to find (must not be null or empty string)

Returns:boolean

whether the constant has been found in the given array

containsConstantback to summary
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:Enum<?>[]

the enum values to check, typically obtained via MyEnum.values()

constant:String

the constant name to find (must not be null or empty string)

caseSensitive:boolean

whether case is significant in determining a match

Returns:boolean

whether the constant has been found in the given array

containsElementback to summary
public static boolean containsElement(Object[] array, Object element)

Check whether the given array contains the given element.

Parameters
array:Object[]

the array to check (may be null, in which case the return value will always be false)

element:Object

the element to check for

Returns:boolean

whether the element has been found in the given array

getDisplayStringback to summary
public static String getDisplayString(Object obj)

Return a content-based String representation if obj is not null; otherwise returns an empty String.

Differs from nullSafeToString(Object) in that it returns an empty String rather than "null" for a null value.

Parameters
obj:Object

the object to build a display String for

Returns:String

a display String representation of obj

See Also
nullSafeToString(Object)
getIdentityHexStringback to summary
public static String getIdentityHexString(Object obj)

Return a hex String form of an object's identity hash code.

Parameters
obj:Object

the object

Returns:String

the object's identity code in hex notation

identityToStringback to summary
public static String identityToString(Object obj)

Return a String representation of an object's overall identity.

Parameters
obj:Object

the object (may be null)

Returns:String

the object's identity as String representation, or an empty String if the object was null

isArrayback to summary
public static boolean isArray(Object obj)

Determine whether the given object is an array: either an Object array or a primitive array.

Parameters
obj:Object

the object to check

isCheckedExceptionback to summary
public static boolean isCheckedException(Throwable ex)

Return whether the given throwable is a checked exception: that is, neither a RuntimeException nor an Error.

Parameters
ex:Throwable

the throwable to check

Returns:boolean

whether the throwable is a checked exception

See Also
java.lang.Exception, java.lang.RuntimeException, java.lang.Error
isCompatibleWithThrowsClauseback to summary
public 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.

Parameters
ex:Throwable

the exception to check

declaredExceptions:Class<?>[]

the exception types declared in the throws clause

Returns:boolean

whether the given exception is compatible

isEmptyback to summary
public static boolean isEmpty(Object[] array)

Determine whether the given array is empty: i.e. null or of zero length.

Parameters
array:Object[]

the array to check

See Also
isEmpty(Object)
isEmptyback to summary
public static boolean isEmpty(Object obj)

Determine whether the given object is empty.

This method supports the following object types.

If the given object is non-null and not one of the aforementioned supported types, this method returns false.

Parameters
obj:Object

the object to check

Returns:boolean

true if the object is null or empty

Since
4.2
See Also
Optional#isPresent(), ObjectUtils#isEmpty(Object[]), StringUtils#hasLength(CharSequence), CollectionUtils#isEmpty(java.util.Collection), CollectionUtils#isEmpty(java.util.Map)
nullSafeClassNameback to summary
public static String nullSafeClassName(Object obj)

Determine the class name for the given object.

Returns a "null" String if obj is null.

Parameters
obj:Object

the object to introspect (may be null)

Returns:String

the corresponding class name

nullSafeConciseToStringback to summary
public static String nullSafeConciseToString(Object obj)

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" if obj is null
  • "Optional.empty" if obj is an empty Optional
  • "Optional[<concise-string>]" if obj is a non-empty Optional, where <concise-string> is the result of invoking this method on the object contained in the Optional
  • "{}" if obj is an empty array
  • "{...}" if obj is a Map or a non-empty array
  • "[...]" if obj is a Collection
  • Class name if obj is a Class
  • Charset name if obj is a Charset
  • TimeZone ID if obj is a TimeZone
  • Zone ID if obj is a ZoneId
  • Potentially truncated string if obj is a String or CharSequence
  • Potentially truncated string if obj is a simple value type whose toString() 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:Object

the object to build a string representation for

Returns:String

a concise string representation of the supplied object

Since
5.3.27
See Also
nullSafeToString(Object), StringUtils#truncate(CharSequence), ClassUtils#isSimpleValueType(Class)
nullSafeEqualsback to summary
public static boolean nullSafeEquals(Object o1, Object o2)

Determine if the given objects are equal, returning true if both are null or false if only one is null.

Compares arrays with Arrays.equals, performing an equality check based on the array elements rather than the array reference.

Parameters
o1:Object

first Object to compare

o2:Object

second Object to compare

Returns:boolean

whether the given objects are equal

See Also
Object#equals(Object), java.util.Arrays#equals
nullSafeHashback to summary
public static int nullSafeHash(Object... elements)

Return a hash code for the given elements, delegating to nullSafeHashCode(Object) for each element. Contrary to Objects#hash(Object...), this method can handle an element that is an array.

Parameters
elements:Object[]

the elements to be hashed

Returns:int

a hash value of the elements

Since
6.1
nullSafeHashCodeback to summary
public static int nullSafeHashCode(Object obj)

Return a hash code for the given object; typically the value of Object#hashCode()}. If the object is an array, this method will delegate to any of the Arrays.hashCode methods. If the object is null, this method returns 0.

See Also
Object#hashCode(), Arrays
nullSafeHashCodeback to summary
public static int nullSafeHashCode(Object[] array)

Deprecated

since 6.1.

as of 6.1 in favor of Arrays#hashCode(Object[])

Return a hash code based on the contents of the specified array. If array is null, this method returns 0.

Annotations
@Deprecated
since:6.1
nullSafeHashCodeback to summary
public static int nullSafeHashCode(boolean[] array)

Deprecated

since 6.1.

as of 6.1 in favor of Arrays#hashCode(boolean[])

Return a hash code based on the contents of the specified array. If array is null, this method returns 0.

Annotations
@Deprecated
since:6.1
nullSafeHashCodeback to summary
public static int nullSafeHashCode(byte[] array)

Deprecated

since 6.1.

as of 6.1 in favor of Arrays#hashCode(byte[])

Return a hash code based on the contents of the specified array. If array is null, this method returns 0.

Annotations
@Deprecated
since:6.1
nullSafeHashCodeback to summary
public static int nullSafeHashCode(char[] array)

Deprecated

since 6.1.

as of 6.1 in favor of Arrays#hashCode(char[])

Return a hash code based on the contents of the specified array. If array is null, this method returns 0.

Annotations
@Deprecated
since:6.1
nullSafeHashCodeback to summary
public static int nullSafeHashCode(double[] array)

Deprecated

since 6.1.

as of 6.1 in favor of Arrays#hashCode(double[])

Return a hash code based on the contents of the specified array. If array is null, this method returns 0.

Annotations
@Deprecated
since:6.1
nullSafeHashCodeback to summary
public static int nullSafeHashCode(float[] array)

Deprecated

since 6.1.

as of 6.1 in favor of Arrays#hashCode(float[])

Return a hash code based on the contents of the specified array. If array is null, this method returns 0.

Annotations
@Deprecated
since:6.1
nullSafeHashCodeback to summary
public static int nullSafeHashCode(int[] array)

Deprecated

since 6.1.

as of 6.1 in favor of Arrays#hashCode(int[])

Return a hash code based on the contents of the specified array. If array is null, this method returns 0.

Annotations
@Deprecated
since:6.1
nullSafeHashCodeback to summary
public static int nullSafeHashCode(long[] array)

Deprecated

since 6.1.

as of 6.1 in favor of Arrays#hashCode(long[])

Return a hash code based on the contents of the specified array. If array is null, this method returns 0.

Annotations
@Deprecated
since:6.1
nullSafeHashCodeback to summary
public static int nullSafeHashCode(short[] array)

Deprecated

since 6.1.

as of 6.1 in favor of Arrays#hashCode(short[])

Return a hash code based on the contents of the specified array. If array is null, this method returns 0.

Annotations
@Deprecated
since:6.1
nullSafeToStringback to summary
public static String nullSafeToString(Object obj)

Return a String representation of the specified Object.

Builds a String representation of the contents in case of an array. Returns a "null" String if obj is null.

Parameters
obj:Object

the object to build a String representation for

Returns:String

a String representation of obj

See Also
nullSafeConciseToString(Object)
nullSafeToStringback to summary
public static String nullSafeToString(Object[] array)

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 if array is null.

Parameters
array:Object[]

the array to build a String representation for

Returns:String

a String representation of array

nullSafeToStringback to summary
public static String nullSafeToString(boolean[] array)

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 if array is null.

Parameters
array:boolean[]

the array to build a String representation for

Returns:String

a String representation of array

nullSafeToStringback to summary
public static String nullSafeToString(byte[] array)

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 if array is null.

Parameters
array:byte[]

the array to build a String representation for

Returns:String

a String representation of array

nullSafeToStringback to summary
public static String nullSafeToString(char[] array)

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 if array is null.

Parameters
array:char[]

the array to build a String representation for

Returns:String

a String representation of array

nullSafeToStringback to summary
public static String nullSafeToString(double[] array)

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 if array is null.

Parameters
array:double[]

the array to build a String representation for

Returns:String

a String representation of array

nullSafeToStringback to summary
public static String nullSafeToString(float[] array)

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 if array is null.

Parameters
array:float[]

the array to build a String representation for

Returns:String

a String representation of array

nullSafeToStringback to summary
public static String nullSafeToString(int[] array)

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 if array is null.

Parameters
array:int[]

the array to build a String representation for

Returns:String

a String representation of array

nullSafeToStringback to summary
public static String nullSafeToString(long[] array)

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 if array is null.

Parameters
array:long[]

the array to build a String representation for

Returns:String

a String representation of array

nullSafeToStringback to summary
public static String nullSafeToString(short[] array)

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 if array is null.

Parameters
array:short[]

the array to build a String representation for

Returns:String

a String representation of array

toObjectArrayback to summary
public 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).

A null source value will be converted to an empty Object array.

Parameters
source:Object

the (potentially primitive) array

Returns:Object[]

the corresponding object array (never null)

Exceptions
IllegalArgumentException:
if the parameter is not an array
unwrapOptionalback to summary
public static Object unwrapOptional(Object obj)

Unwrap the given object which is potentially a java.util.Optional.

Parameters
obj:Object

the candidate object

Returns:Object

either the value held within the Optional, null if the Optional is empty, or simply the given object as-is

Annotations
@Nullable
Since
5.0