public class ObjectUtils extends Object
Operations on Object.
This class tries to handle null input gracefully.
An exception will generally not be thrown for a null input.
Each method documents its behaviour in more detail.
| Modifier and Type | Class and Description |
|---|---|
static class |
ObjectUtils.Null
Class used as a null placeholder where
null
has another meaning. |
| Modifier and Type | Field and Description |
|---|---|
static ObjectUtils.Null |
NULL
Singleton used as a
null placeholder where
null has another meaning. |
| Constructor and Description |
|---|
ObjectUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <A,O extends 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 boolean |
allNotNull(Object... values)
Checks if all values in the array are not
nulls. |
static boolean |
anyNotNull(Object... values)
Checks if any value in the given array is not
null. |
static <E extends Enum<?>> |
caseInsensitiveValueOf(E[] enumValues,
String constant)
Case insensitive alternative to
Enum.valueOf(Class, String). |
static <T> T |
clone(T obj)
Clone an object.
|
static <T> T |
cloneIfPossible(T obj)
Clone an object if possible.
|
static <T extends Comparable<? super T>> |
compare(T c1,
T c2)
Null safe comparison of Comparables.
|
static <T extends Comparable<? super T>> |
compare(T c1,
T c2,
boolean nullGreater)
Null safe comparison of Comparables.
|
static byte |
CONST_BYTE(int v)
This method returns the provided value unchanged.
|
static short |
CONST_SHORT(int v)
This method returns the provided value unchanged.
|
static boolean |
CONST(boolean v)
This method returns the provided value unchanged.
|
static byte |
CONST(byte v)
This method returns the provided value unchanged.
|
static char |
CONST(char v)
This method returns the provided value unchanged.
|
static double |
CONST(double v)
This method returns the provided value unchanged.
|
static float |
CONST(float v)
This method returns the provided value unchanged.
|
static int |
CONST(int v)
This method returns the provided value unchanged.
|
static long |
CONST(long v)
This method returns the provided value unchanged.
|
static short |
CONST(short v)
This method returns the provided value unchanged.
|
static <T> T |
CONST(T v)
This method returns the provided value unchanged.
|
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 <T> T |
defaultIfNull(T object,
T defaultValue)
Returns a default value if the object passed is
null. |
static <T> T |
firstNonNull(T... values)
Returns the first value in the array which is not
null. |
static String |
getDisplayString(Object obj)
Return a content-based String representation if
obj is
not null; otherwise returns an empty String. |
static String |
getIdentityHexString(Object obj)
Return a hex String form of an object's identity hash code.
|
static void |
identityToString(Appendable appendable,
Object object)
Appends the toString that would be produced by
Object
if a class did not override toString itself. |
static String |
identityToString(Object object)
Gets the toString that would be produced by
Object
if a class did not override toString itself. |
static void |
identityToString(StringBuffer buffer,
Object object)
Appends the toString that would be produced by
Object
if a class did not override toString itself. |
static void |
identityToString(StringBuilder builder,
Object object)
Appends the toString that would be produced by
Object
if a class did not override toString itself. |
static boolean |
isArray(Object obj)
Determine whether the given object is an array:
either an Object array or a primitive array.
|
static boolean |
isCheckedException(Throwable ex)
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 |
isEmpty(Object object)
Checks if an Object is empty or null.
|
static boolean |
isEmpty(Object[] array)
Determine whether the given array is empty:
i.e.
|
static boolean |
isNotEmpty(Object object)
Checks if an Object is not empty and not null.
|
static <T extends Comparable<? super T>> |
max(T... values)
Null safe comparison of Comparables.
|
static <T extends Comparable<? super T>> |
min(T... values)
Null safe comparison of Comparables.
|
static String |
nullSafeClassName(Object obj)
Determine the class name for the given object.
|
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. |
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(Object obj)
Return as hash code for the given object; typically the value of
Object#hashCode()}. |
static int |
nullSafeHashCode(Object[] 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 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(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 String |
nullSafeToString(short[] 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 a
Optional. |
public static final ObjectUtils.Null NULL
Singleton used as a null placeholder where
null has another meaning.
For example, in a HashMap the
HashMap.get(Object) method returns
null if the Map contains null or if there
is no matching key. The Null placeholder can be used to
distinguish between these two cases.
Another example is Hashtable, where null
cannot be stored.
This instance is Serializable.
public static boolean isCheckedException(Throwable ex)
ex - the throwable to checkpublic static boolean isCompatibleWithThrowsClause(Throwable ex, Class<?>... declaredExceptions)
ex - the exception to checkdeclaredExceptions - the exception types declared in the throws clausepublic static boolean isArray(Object obj)
obj - the object to checkpublic static boolean isEmpty(Object[] array)
null or of zero length.array - the array to checkpublic static Object unwrapOptional(Object obj)
Optional.obj - the candidate objectOptional, null
if the Optional is empty, or simply the given object as-ispublic static boolean containsElement(Object[] array, Object element)
array - the array to check (may be null,
in which case the return value will always be false)element - the element to check forpublic static boolean containsConstant(Enum<?>[] enumValues, String constant)
enumValues - the enum values to check, typically obtained via MyEnum.values()constant - the constant name to find (must not be null or empty string)public static boolean containsConstant(Enum<?>[] enumValues, String constant, boolean caseSensitive)
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 matchpublic static <E extends Enum<?>> E caseInsensitiveValueOf(E[] enumValues, String constant)
Enum.valueOf(Class, String).E - the concrete Enum typeenumValues - the array of all Enum constants in question, usually per Enum.values()constant - the constant to get the enum value ofIllegalArgumentException - 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.public static <A,O extends A> A[] addObjectToArray(A[] array,
O obj)
array - the array to append to (can be null)obj - the object to appendnull)public static Object[] toObjectArray(Object source)
A null source value will be converted to an
empty Object array.
source - the (potentially primitive) arraynull)IllegalArgumentException - if the parameter is not an arraypublic static boolean nullSafeEquals(Object o1, Object o2)
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.
o1 - first Object to compareo2 - second Object to comparepublic static int nullSafeHashCode(Object obj)
Object#hashCode()}. If the object is an array,
this method will delegate to any of the nullSafeHashCode
methods for arrays in this class. If the object is null,
this method returns 0.public static int nullSafeHashCode(Object[] array)
array is null, this method returns 0.public static int nullSafeHashCode(boolean[] array)
array is null, this method returns 0.public static int nullSafeHashCode(byte[] array)
array is null, this method returns 0.public static int nullSafeHashCode(char[] array)
array is null, this method returns 0.public static int nullSafeHashCode(double[] array)
array is null, this method returns 0.public static int nullSafeHashCode(float[] array)
array is null, this method returns 0.public static int nullSafeHashCode(int[] array)
array is null, this method returns 0.public static int nullSafeHashCode(long[] array)
array is null, this method returns 0.public static int nullSafeHashCode(short[] array)
array is null, this method returns 0.public static String getIdentityHexString(Object obj)
obj - the objectpublic static String getDisplayString(Object obj)
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.
obj - the object to build a display String forobjpublic static String nullSafeClassName(Object obj)
Returns a "null" String if obj is null.
obj - the object to introspect (may be null)public static String nullSafeToString(Object obj)
Builds a String representation of the contents in case of an array.
Returns a "null" String if obj is null.
obj - the object to build a String representation forobjpublic static String nullSafeToString(Object[] 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.
array - the array to build a String representation forarraypublic static String nullSafeToString(boolean[] 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.
array - the array to build a String representation forarraypublic static String nullSafeToString(byte[] 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.
array - the array to build a String representation forarraypublic static String nullSafeToString(char[] 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.
array - the array to build a String representation forarraypublic static String nullSafeToString(double[] 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.
array - the array to build a String representation forarraypublic static String nullSafeToString(float[] 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.
array - the array to build a String representation forarraypublic static String nullSafeToString(int[] 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.
array - the array to build a String representation forarraypublic static String nullSafeToString(long[] 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.
array - the array to build a String representation forarraypublic static String nullSafeToString(short[] 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.
array - the array to build a String representation forarraypublic static boolean isEmpty(Object object)
Checks if an Object is empty or null.
The following types are supported:
CharSequence: Considered empty if its length is zero.Array: Considered empty if its length is zero.Collection: Considered empty if it has zero elements.Map: Considered empty if it has zero key-value mappings.
ObjectUtils.isEmpty(null) = true
ObjectUtils.isEmpty("") = true
ObjectUtils.isEmpty("ab") = false
ObjectUtils.isEmpty(new int[]{}) = true
ObjectUtils.isEmpty(new int[]{1,2,3}) = false
ObjectUtils.isEmpty(1234) = false
object - the Object to test, may be nulltrue if the object has a supported type and is empty or null, false otherwisepublic static boolean isNotEmpty(Object object)
Checks if an Object is not empty and not null.
The following types are supported:
CharSequence: Considered empty if its length is zero.Array: Considered empty if its length is zero.Collection: Considered empty if it has zero elements.Map: Considered empty if it has zero key-value mappings.
ObjectUtils.isNotEmpty(null) = false
ObjectUtils.isNotEmpty("") = false
ObjectUtils.isNotEmpty("ab") = true
ObjectUtils.isNotEmpty(new int[]{}) = false
ObjectUtils.isNotEmpty(new int[]{1,2,3}) = true
ObjectUtils.isNotEmpty(1234) = true
object - the Object to test, may be nulltrue if the object has an unsupported type or is not empty
and not null, false otherwisepublic static <T> T defaultIfNull(T object,
T defaultValue)
Returns a default value if the object passed is null.
ObjectUtils.defaultIfNull(null, null) = null
ObjectUtils.defaultIfNull(null, "") = ""
ObjectUtils.defaultIfNull(null, "zz") = "zz"
ObjectUtils.defaultIfNull("abc", *) = "abc"
ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
T - the type of the objectobject - the Object to test, may be nulldefaultValue - the default value to return, may be nullobject if it is not null, defaultValue otherwise@SafeVarargs public static <T> T firstNonNull(T... values)
Returns the first value in the array which is not null.
If all the values are null or the array is null
or empty then null is returned.
ObjectUtils.firstNonNull(null, null) = null
ObjectUtils.firstNonNull(null, "") = ""
ObjectUtils.firstNonNull(null, null, "") = ""
ObjectUtils.firstNonNull(null, "zz") = "zz"
ObjectUtils.firstNonNull("abc", *) = "abc"
ObjectUtils.firstNonNull(null, "xyz", *) = "xyz"
ObjectUtils.firstNonNull(Boolean.TRUE, *) = Boolean.TRUE
ObjectUtils.firstNonNull() = null
T - the component type of the arrayvalues - the values to test, may be null or emptyvalues which is not null,
or null if there are no non-null valuespublic static boolean anyNotNull(Object... values)
null.
If all the values are null or the array is null
or empty then false is returned. Otherwise true is returned.
ObjectUtils.anyNotNull(*) = true ObjectUtils.anyNotNull(*, null) = true ObjectUtils.anyNotNull(null, *) = true ObjectUtils.anyNotNull(null, null, *, *) = true ObjectUtils.anyNotNull(null) = false ObjectUtils.anyNotNull(null, null) = false
values - the values to test, may be null or emptytrue if there is at least one non-null value in the array,
false if all values in the array are nulls.
If the array is null or empty false is also returned.public static boolean allNotNull(Object... values)
nulls.
If any value is null or the array is null then
false is returned. If all elements in array are not
null or the array is empty (contains no elements) true
is returned.
ObjectUtils.allNotNull(*) = true ObjectUtils.allNotNull(*, *) = true ObjectUtils.allNotNull(null) = false ObjectUtils.allNotNull(null, null) = false ObjectUtils.allNotNull(null, *) = false ObjectUtils.allNotNull(*, null) = false ObjectUtils.allNotNull(*, *, null, *) = false
values - the values to test, may be null or emptyfalse if there is at least one null value in the array or the array is null,
true if all values in the array are not nulls or array contains no elements.public static String identityToString(Object object)
Gets the toString that would be produced by Object
if a class did not override toString itself. null
will return null.
ObjectUtils.identityToString(null) = null
ObjectUtils.identityToString("") = "java.lang.String@1e23"
ObjectUtils.identityToString(Boolean.TRUE) = "java.lang.Boolean@7fa"
object - the object to create a toString for, may be
nullnull if
null passed inpublic static void identityToString(Appendable appendable, Object object) throws IOException
Appends the toString that would be produced by Object
if a class did not override toString itself. null
will throw a NullPointerException for either of the two parameters.
ObjectUtils.identityToString(appendable, "") = appendable.append("java.lang.String@1e23"
ObjectUtils.identityToString(appendable, Boolean.TRUE) = appendable.append("java.lang.Boolean@7fa"
ObjectUtils.identityToString(appendable, Boolean.TRUE) = appendable.append("java.lang.Boolean@7fa")
appendable - the appendable to append toobject - the object to create a toString forIOException - if an I/O error occurspublic static void identityToString(StringBuffer buffer, Object object)
Appends the toString that would be produced by Object
if a class did not override toString itself. null
will throw a NullPointerException for either of the two parameters.
ObjectUtils.identityToString(buf, "") = buf.append("java.lang.String@1e23"
ObjectUtils.identityToString(buf, Boolean.TRUE) = buf.append("java.lang.Boolean@7fa"
ObjectUtils.identityToString(buf, Boolean.TRUE) = buf.append("java.lang.Boolean@7fa")
buffer - the buffer to append toobject - the object to create a toString forpublic static void identityToString(StringBuilder builder, Object object)
Appends the toString that would be produced by Object
if a class did not override toString itself. null
will throw a NullPointerException for either of the two parameters.
ObjectUtils.identityToString(builder, "") = builder.append("java.lang.String@1e23"
ObjectUtils.identityToString(builder, Boolean.TRUE) = builder.append("java.lang.Boolean@7fa"
ObjectUtils.identityToString(builder, Boolean.TRUE) = builder.append("java.lang.Boolean@7fa")
builder - the builder to append toobject - the object to create a toString for@SafeVarargs public static <T extends Comparable<? super T>> T min(T... values)
Null safe comparison of Comparables.
T - type of the values processed by this methodvalues - the set of comparable values, may be null@SafeVarargs public static <T extends Comparable<? super T>> T max(T... values)
Null safe comparison of Comparables.
T - type of the values processed by this methodvalues - the set of comparable values, may be nullpublic static <T extends Comparable<? super T>> int compare(T c1, T c2)
Null safe comparison of Comparables.
null is assumed to be less than a non-null value.
T - type of the values processed by this methodc1 - the first comparable, may be nullc2 - the second comparable, may be nullpublic static <T extends Comparable<? super T>> int compare(T c1, T c2, boolean nullGreater)
Null safe comparison of Comparables.
T - type of the values processed by this methodc1 - the first comparable, may be nullc2 - the second comparable, may be nullnullGreater - if true null is considered greater
than a non-null value or if false null is
considered less than a Non-null valuepublic static <T> T clone(T obj)
Clone an object.
T - the type of the objectobj - the object to clone, null returns nullCloneable otherwise nullIllegalStateException - if the object is cloneable and the clone operation failspublic static <T> T cloneIfPossible(T obj)
Clone an object if possible.
This method is similar to clone(Object), but will return the provided
instance as the return value instead of null if the instance
is not cloneable. This is more convenient if the caller uses different
implementations (e.g. of a service) and some of the implementations do not allow concurrent
processing or have state. In such cases the implementation can simply provide a proper
clone implementation and the caller's code does not have to change.
T - the type of the objectobj - the object to clone, null returns nullCloneable otherwise the object itselfIllegalStateException - if the object is cloneable and the clone operation failspublic static boolean CONST(boolean v)
public final static boolean MAGIC_FLAG = ObjectUtils.CONST(true);
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
v - the boolean value to returnpublic static byte CONST(byte v)
public final static byte MAGIC_BYTE = ObjectUtils.CONST((byte) 127);
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
v - the byte value to returnpublic static byte CONST_BYTE(int v)
public final static byte MAGIC_BYTE = ObjectUtils.CONST_BYTE(127);
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
v - the byte literal (as an int) value to returnIllegalArgumentException - if the value passed to v
is larger than a byte, that is, smaller than -128 or
larger than 127.public static char CONST(char v)
public final static char MAGIC_CHAR = ObjectUtils.CONST('a');
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
v - the char value to returnpublic static short CONST(short v)
public final static short MAGIC_SHORT = ObjectUtils.CONST((short) 123);
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
v - the short value to returnpublic static short CONST_SHORT(int v)
public final static short MAGIC_SHORT = ObjectUtils.CONST_SHORT(127);
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
v - the short literal (as an int) value to returnIllegalArgumentException - if the value passed to v
is larger than a short, that is, smaller than -32768 or
larger than 32767.public static int CONST(int v)
public final static int MAGIC_INT = ObjectUtils.CONST(123);
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
v - the int value to returnpublic static long CONST(long v)
public final static long MAGIC_LONG = ObjectUtils.CONST(123L);
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
v - the long value to returnpublic static float CONST(float v)
public final static float MAGIC_FLOAT = ObjectUtils.CONST(1.0f);
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
v - the float value to returnpublic static double CONST(double v)
public final static double MAGIC_DOUBLE = ObjectUtils.CONST(1.0);
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
v - the double value to returnpublic static <T> T CONST(T v)
public final static String MAGIC_STRING = ObjectUtils.CONST("abc");
This way any jars that refer to this field do not have to recompile themselves if the field's value changes at some future date.
T - the Object typev - the genericized Object value to return (typically a String).Copyright © 2020. All rights reserved.