Class ObjectUtils
- java.lang.Object
-
- com.igormaznitsa.jcp.utils.antpathmatcher.ObjectUtils
-
abstract class ObjectUtils extends java.lang.Object
Miscellaneous object utility methods.Mainly for internal use within the framework.
Thanks to Alex Ruiz for contributing several enhancements to this class!
- Since:
- 19.03.2004
- See Also:
ClassUtils
,CollectionUtils
,StringUtils
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
ARRAY_ELEMENT_SEPARATOR
private static java.lang.String
ARRAY_END
private static java.lang.String
ARRAY_START
private static java.lang.String
EMPTY_ARRAY
private static java.lang.String
EMPTY_STRING
private static int
INITIAL_HASH
private static int
MULTIPLIER
private static java.lang.String
NULL_STRING
-
Constructor Summary
Constructors Constructor Description ObjectUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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.private static boolean
arrayEquals(java.lang.Object o1, java.lang.Object o2)
Compare the given arrays withArrays.equals
, performing an equality check based on the array elements rather than the array reference.static <E extends java.lang.Enum<?>>
EcaseInsensitiveValueOf(E[] enumValues, java.lang.String constant)
Case insensitive alternative toEnum.valueOf(Class, String)
.static boolean
containsConstant(java.lang.Enum<?>[] enumValues, java.lang.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(java.lang.Enum<?>[] enumValues, java.lang.String constant, boolean caseSensitive)
Check whether the given array of enum constants contains a constant with the given name.static boolean
containsElement(java.lang.Object[] array, java.lang.Object element)
Check whether the given array contains the given element.static java.lang.String
getDisplayString(java.lang.Object obj)
Return a content-based String representation ifobj
is notnull
; otherwise returns an empty String.static java.lang.String
getIdentityHexString(java.lang.Object obj)
Return a hex String form of an object's identity hash code.static int
hashCode(boolean bool)
Return the same value asBoolean.hashCode()
}.static int
hashCode(double dbl)
Return the same value asDouble.hashCode()
}.static int
hashCode(float flt)
Return the same value asFloat.hashCode()
}.static int
hashCode(long lng)
Return the same value asLong.hashCode()
}.static java.lang.String
identityToString(java.lang.Object obj)
Return a String representation of an object's overall identity.static boolean
isArray(java.lang.Object obj)
Determine whether the given object is an array: either an Object array or a primitive array.static boolean
isCheckedException(java.lang.Throwable ex)
Return whether the given throwable is a checked exception: that is, neither a RuntimeException nor an Error.static boolean
isCompatibleWithThrowsClause(java.lang.Throwable ex, java.lang.Class<?>... declaredExceptions)
Check whether the given exception is compatible with the specified exception types, as declared in a throws clause.static boolean
isEmpty(java.lang.Object[] array)
Determine whether the given array is empty: i.e.static java.lang.String
nullSafeClassName(java.lang.Object obj)
Determine the class name for the given object.static boolean
nullSafeEquals(java.lang.Object o1, java.lang.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(java.lang.Object obj)
Return as hash code for the given object; typically the value ofObject#hashCode()
}.static int
nullSafeHashCode(java.lang.Object[] array)
Return a hash code based on the contents of the specified array.static java.lang.String
nullSafeToString(boolean[] array)
Return a String representation of the contents of the specified array.static java.lang.String
nullSafeToString(byte[] array)
Return a String representation of the contents of the specified array.static java.lang.String
nullSafeToString(char[] array)
Return a String representation of the contents of the specified array.static java.lang.String
nullSafeToString(double[] array)
Return a String representation of the contents of the specified array.static java.lang.String
nullSafeToString(float[] array)
Return a String representation of the contents of the specified array.static java.lang.String
nullSafeToString(int[] array)
Return a String representation of the contents of the specified array.static java.lang.String
nullSafeToString(long[] array)
Return a String representation of the contents of the specified array.static java.lang.String
nullSafeToString(short[] array)
Return a String representation of the contents of the specified array.static java.lang.String
nullSafeToString(java.lang.Object obj)
Return a String representation of the specified Object.static java.lang.String
nullSafeToString(java.lang.Object[] array)
Return a String representation of the contents of the specified array.static java.lang.Object[]
toObjectArray(java.lang.Object source)
Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects).
-
-
-
Field Detail
-
INITIAL_HASH
private static final int INITIAL_HASH
- See Also:
- Constant Field Values
-
MULTIPLIER
private static final int MULTIPLIER
- See Also:
- Constant Field Values
-
EMPTY_STRING
private static final java.lang.String EMPTY_STRING
- See Also:
- Constant Field Values
-
NULL_STRING
private static final java.lang.String NULL_STRING
- See Also:
- Constant Field Values
-
ARRAY_START
private static final java.lang.String ARRAY_START
- See Also:
- Constant Field Values
-
ARRAY_END
private static final java.lang.String ARRAY_END
- See Also:
- Constant Field Values
-
EMPTY_ARRAY
private static final java.lang.String EMPTY_ARRAY
- See Also:
- Constant Field Values
-
ARRAY_ELEMENT_SEPARATOR
private static final java.lang.String ARRAY_ELEMENT_SEPARATOR
- See Also:
- Constant Field Values
-
-
Method Detail
-
isCheckedException
public static boolean isCheckedException(java.lang.Throwable ex)
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:
Exception
,RuntimeException
,Error
-
isCompatibleWithThrowsClause
public static boolean isCompatibleWithThrowsClause(java.lang.Throwable ex, java.lang.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
public static boolean isArray(java.lang.Object obj)
Determine whether the given object is an array: either an Object array or a primitive array.- Parameters:
obj
- the object to check
-
isEmpty
public static boolean isEmpty(java.lang.Object[] array)
Determine whether the given array is empty: i.e.null
or of zero length.- Parameters:
array
- the array to check- See Also:
#isEmpty(Object)
-
containsElement
public static boolean containsElement(java.lang.Object[] array, java.lang.Object element)
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
public static boolean containsConstant(java.lang.Enum<?>[] enumValues, java.lang.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
- the enum values to check, typically the product of a call to 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
-
containsConstant
public static boolean containsConstant(java.lang.Enum<?>[] enumValues, java.lang.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 the product of a call to 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
-
caseInsensitiveValueOf
public static <E extends java.lang.Enum<?>> E caseInsensitiveValueOf(E[] enumValues, java.lang.String constant)
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 per Enum.values()constant
- the constant to get the enum value of- Throws:
java.lang.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
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
- the array to append to (can benull
)obj
- the object to append- Returns:
- the new array (of the same component type; never
null
)
-
toObjectArray
public static java.lang.Object[] toObjectArray(java.lang.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
- the (potentially primitive) array- Returns:
- the corresponding object array (never
null
) - Throws:
java.lang.IllegalArgumentException
- if the parameter is not an array
-
nullSafeEquals
public static boolean nullSafeEquals(java.lang.Object o1, java.lang.Object o2)
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:
Object.equals(Object)
,Arrays.equals(long[], long[])
-
arrayEquals
private static boolean arrayEquals(java.lang.Object o1, java.lang.Object o2)
Compare the given arrays withArrays.equals
, performing an equality check based on the array elements rather than the array reference.- Parameters:
o1
- first array to compareo2
- second array to compare- Returns:
- whether the given objects are equal
- See Also:
nullSafeEquals(Object, Object)
,Arrays.equals(long[], long[])
-
nullSafeHashCode
public static int nullSafeHashCode(java.lang.Object obj)
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
public static int nullSafeHashCode(java.lang.Object[] array)
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0.
-
nullSafeHashCode
public static int nullSafeHashCode(boolean[] array)
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0.
-
nullSafeHashCode
public static int nullSafeHashCode(byte[] array)
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0.
-
nullSafeHashCode
public static int nullSafeHashCode(char[] array)
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0.
-
nullSafeHashCode
public static int nullSafeHashCode(double[] array)
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0.
-
nullSafeHashCode
public static int nullSafeHashCode(float[] array)
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0.
-
nullSafeHashCode
public static int nullSafeHashCode(int[] array)
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0.
-
nullSafeHashCode
public static int nullSafeHashCode(long[] array)
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0.
-
nullSafeHashCode
public static int nullSafeHashCode(short[] array)
Return a hash code based on the contents of the specified array. Ifarray
isnull
, this method returns 0.
-
hashCode
public static int hashCode(boolean bool)
Return the same value asBoolean.hashCode()
}.- See Also:
Boolean.hashCode()
-
hashCode
public static int hashCode(double dbl)
Return the same value asDouble.hashCode()
}.- See Also:
Double.hashCode()
-
hashCode
public static int hashCode(float flt)
Return the same value asFloat.hashCode()
}.- See Also:
Float.hashCode()
-
hashCode
public static int hashCode(long lng)
Return the same value asLong.hashCode()
}.- See Also:
Long.hashCode()
-
identityToString
public static java.lang.String identityToString(java.lang.Object obj)
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
public static java.lang.String getIdentityHexString(java.lang.Object obj)
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
public static java.lang.String getDisplayString(java.lang.Object obj)
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:
nullSafeToString(Object)
-
nullSafeClassName
public static java.lang.String nullSafeClassName(java.lang.Object obj)
Determine the class name for the given object.Returns
"null"
ifobj
isnull
.- Parameters:
obj
- the object to introspect (may benull
)- Returns:
- the corresponding class name
-
nullSafeToString
public static java.lang.String nullSafeToString(java.lang.Object obj)
Return a String representation of the specified Object.Builds a String representation of the contents in case of an array. Returns
"null"
ifobj
isnull
.- Parameters:
obj
- the object to build a String representation for- Returns:
- a String representation of
obj
-
nullSafeToString
public static java.lang.String nullSafeToString(java.lang.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"null"
ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
public static java.lang.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"null"
ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
public static java.lang.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"null"
ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
public static java.lang.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"null"
ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
public static java.lang.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"null"
ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
public static java.lang.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"null"
ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
public static java.lang.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"null"
ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
public static java.lang.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"null"
ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
nullSafeToString
public static java.lang.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"null"
ifarray
isnull
.- Parameters:
array
- the array to build a String representation for- Returns:
- a String representation of
array
-
-