Class ObjectUtils
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classClass used as a null placeholder wherenullhas another meaning. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ObjectUtils.NullSingleton used as anullplaceholder wherenullhas another meaning. -
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 booleanallNotNull(Object... values) Checks if all values in the array are notnulls.static booleananyNotNull(Object... values) Checks if any value in the given array is notnull.static <E extends Enum<?>>
EcaseInsensitiveValueOf(E[] enumValues, String constant) Case insensitive alternative toEnum.valueOf(Class, String).static <T> Tclone(T obj) Clone an object.static <T> TcloneIfPossible(T obj) Clone an object if possible.static <T extends Comparable<? super T>>
intcompare(T c1, T c2) Null safe comparison of Comparables.static <T extends Comparable<? super T>>
intcompare(T c1, T c2, boolean nullGreater) Null safe comparison of Comparables.static booleanCONST(boolean v) This method returns the provided value unchanged.static byteCONST(byte v) This method returns the provided value unchanged.static charCONST(char v) This method returns the provided value unchanged.static doubleCONST(double v) This method returns the provided value unchanged.static floatCONST(float v) This method returns the provided value unchanged.static intCONST(int v) This method returns the provided value unchanged.static longCONST(long v) This method returns the provided value unchanged.static shortCONST(short v) This method returns the provided value unchanged.static <T> TCONST(T v) This method returns the provided value unchanged.static byteCONST_BYTE(int v) This method returns the provided value unchanged.static shortCONST_SHORT(int v) This method returns the provided value unchanged.static booleancontainsConstant(Enum<?>[] enumValues, String constant) Check whether the given array of enum constants contains a constant with the given name, ignoring case when determining a match.static booleancontainsConstant(Enum<?>[] enumValues, String constant, boolean caseSensitive) Check whether the given array of enum constants contains a constant with the given name.static booleancontainsElement(Object[] array, Object element) Check whether the given array contains the given element.static <T> TdefaultIfNull(T object, T defaultValue) Returns a default value if the object passed isnull.static <T> TfirstNonNull(T... values) Returns the first value in the array which is notnull.static StringgetDisplayString(Object obj) Return a content-based String representation ifobjis notnull; otherwise returns an empty String.static StringReturn a hex String form of an object's identity hash code.static voididentityToString(Appendable appendable, Object object) Appends the toString that would be produced byObjectif a class did not override toString itself.static StringidentityToString(Object object) Gets the toString that would be produced byObjectif a class did not override toString itself.static voididentityToString(StringBuffer buffer, Object object) Appends the toString that would be produced byObjectif a class did not override toString itself.static voididentityToString(StringBuilder builder, Object object) Appends the toString that would be produced byObjectif a class did not override toString itself.static booleanDetermine whether the given object is an array: either an Object array or a primitive array.static booleanReturn whether the given throwable is a checked exception: that is, neither a RuntimeException nor an Error.static booleanisCompatibleWithThrowsClause(Throwable ex, Class<?>... declaredExceptions) Check whether the given exception is compatible with the specified exception types, as declared in a throws clause.static booleanChecks if an Object is empty or null.static booleanDetermine whether the given array is empty: i.e.static booleanisNotEmpty(Object object) Checks if an Object is not empty and not null.static <T extends Comparable<? super T>>
Tmax(T... values) Null safe comparison of Comparables.static <T extends Comparable<? super T>>
Tmin(T... values) Null safe comparison of Comparables.static StringnullSafeClassName(Object obj) Determine the class name for the given object.static booleannullSafeEquals(Object o1, Object o2) Determine if the given objects are equal, returningtrueif both arenullorfalseif only one isnull.static intnullSafeHashCode(boolean[] array) Return a hash code based on the contents of the specified array.static intnullSafeHashCode(byte[] array) Return a hash code based on the contents of the specified array.static intnullSafeHashCode(char[] array) Return a hash code based on the contents of the specified array.static intnullSafeHashCode(double[] array) Return a hash code based on the contents of the specified array.static intnullSafeHashCode(float[] array) Return a hash code based on the contents of the specified array.static intnullSafeHashCode(int[] array) Return a hash code based on the contents of the specified array.static intnullSafeHashCode(long[] array) Return a hash code based on the contents of the specified array.static intnullSafeHashCode(short[] array) Return a hash code based on the contents of the specified array.static intnullSafeHashCode(Object obj) Return as hash code for the given object; typically the value ofObject#hashCode()}.static intnullSafeHashCode(Object[] array) Return a hash code based on the contents of the specified array.static StringnullSafeToString(boolean[] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(byte[] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(char[] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(double[] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(float[] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(int[] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(long[] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(short[] array) Return a String representation of the contents of the specified array.static StringnullSafeToString(Object obj) Return a String representation of the specified Object.static StringnullSafeToString(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 ObjectunwrapOptional(Object obj) Unwrap the given object which is potentially aOptional.
-
Field Details
-
NULL
Singleton used as a
nullplaceholder wherenullhas another meaning.For example, in a
HashMaptheHashMap.get(Object)method returnsnullif theMapcontainsnullor if there is no matching key. TheNullplaceholder can be used to distinguish between these two cases.Another example is
Hashtable, wherenullcannot be stored.This instance is Serializable.
-
-
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
-
isCompatibleWithThrowsClause
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.nullor of zero length.- Parameters:
array- the array to check
-
unwrapOptional
Unwrap the given object which is potentially aOptional.- Parameters:
obj- the candidate object- Returns:
- either the value held within the
Optional,nullif theOptionalis empty, or simply the given object as-is
-
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
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
Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects).A
nullsource 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, returningtrueif both arenullorfalseif only one isnull.Compares arrays with
Arrays.equals, performing an equality check based on the array elements rather than the array reference.- Parameters:
o1- first Object to compareo2- second Object to compare- Returns:
- whether the given objects are equal
-
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 thenullSafeHashCodemethods 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. Ifarrayisnull, this method returns 0. -
nullSafeHashCode
public static int nullSafeHashCode(boolean[] array) Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0. -
nullSafeHashCode
public static int nullSafeHashCode(byte[] array) Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0. -
nullSafeHashCode
public static int nullSafeHashCode(char[] array) Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0. -
nullSafeHashCode
public static int nullSafeHashCode(double[] array) Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0. -
nullSafeHashCode
public static int nullSafeHashCode(float[] array) Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0. -
nullSafeHashCode
public static int nullSafeHashCode(int[] array) Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0. -
nullSafeHashCode
public static int nullSafeHashCode(long[] array) Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0. -
nullSafeHashCode
public static int nullSafeHashCode(short[] array) Return a hash code based on the contents of the specified array. Ifarrayisnull, this method returns 0. -
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 ifobjis notnull; otherwise returns an empty String.Differs from
nullSafeToString(Object)in that it returns an empty String rather than "null" for anullvalue.- Parameters:
obj- the object to build a display String for- Returns:
- a display String representation of
obj
-
nullSafeClassName
Determine the class name for the given object.Returns a
"null"String ifobjisnull.- 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 ifobjisnull.- Parameters:
obj- the object to build a String representation for- Returns:
- a String representation of
obj
-
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 ifarrayisnull.- 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 ifarrayisnull.- 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 ifarrayisnull.- 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 ifarrayisnull.- 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 ifarrayisnull.- 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 ifarrayisnull.- 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 ifarrayisnull.- 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 ifarrayisnull.- 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 ifarrayisnull.- Parameters:
array- the array to build a String representation for- Returns:
- a String representation of
array
-
isEmpty
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- Parameters:
object- theObjectto test, may benull- Returns:
trueif the object has a supported type and is empty or null,falseotherwise
-
isNotEmpty
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- Parameters:
object- theObjectto test, may benull- Returns:
trueif the object has an unsupported type or is not empty and not null,falseotherwise
-
defaultIfNull
public 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- Type Parameters:
T- the type of the object- Parameters:
object- theObjectto test, may benulldefaultValue- the default value to return, may benull- Returns:
objectif it is notnull, defaultValue otherwise
-
firstNonNull
Returns the first value in the array which is not
null. If all the values arenullor the array isnullor empty thennullis 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- Type Parameters:
T- the component type of the array- Parameters:
values- the values to test, may benullor empty- Returns:
- the first value from
valueswhich is notnull, ornullif there are no non-null values
-
anyNotNull
Checks if any value in the given array is notnull. If all the values arenullor the array isnullor empty thenfalseis returned. Otherwisetrueis 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
- Parameters:
values- the values to test, may benullor empty- Returns:
trueif there is at least one non-null value in the array,falseif all values in the array arenulls. If the array isnullor emptyfalseis also returned.
-
allNotNull
Checks if all values in the array are notnulls. If any value isnullor the array isnullthenfalseis returned. If all elements in array are notnullor the array is empty (contains no elements)trueis 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
- Parameters:
values- the values to test, may benullor empty- Returns:
falseif there is at least onenullvalue in the array or the array isnull,trueif all values in the array are notnulls or array contains no elements.
-
identityToString
Gets the toString that would be produced by
Objectif a class did not override toString itself.nullwill returnnull.ObjectUtils.identityToString(null) = null ObjectUtils.identityToString("") = "java.lang.String@1e23" ObjectUtils.identityToString(Boolean.TRUE) = "java.lang.Boolean@7fa"- Parameters:
object- the object to create a toString for, may benull- Returns:
- the default toString text, or
nullifnullpassed in
-
identityToString
Appends the toString that would be produced by
Objectif a class did not override toString itself.nullwill 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")- Parameters:
appendable- the appendable to append toobject- the object to create a toString for- Throws:
IOException- if an I/O error occurs
-
identityToString
Appends the toString that would be produced by
Objectif a class did not override toString itself.nullwill 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")- Parameters:
buffer- the buffer to append toobject- the object to create a toString for
-
identityToString
Appends the toString that would be produced by
Objectif a class did not override toString itself.nullwill 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")- Parameters:
builder- the builder to append toobject- the object to create a toString for
-
min
Null safe comparison of Comparables.
- Type Parameters:
T- type of the values processed by this method- Parameters:
values- the set of comparable values, may be null- Returns:
- If any objects are non-null and unequal, the lesser object.
- If all objects are non-null and equal, the first.
- If any of the comparables are null, the lesser of the non-null objects.
- If all the comparables are null, null is returned.
-
max
Null safe comparison of Comparables.
- Type Parameters:
T- type of the values processed by this method- Parameters:
values- the set of comparable values, may be null- Returns:
- If any objects are non-null and unequal, the greater object.
- If all objects are non-null and equal, the first.
- If any of the comparables are null, the greater of the non-null objects.
- If all the comparables are null, null is returned.
-
compare
Null safe comparison of Comparables.
nullis assumed to be less than a non-nullvalue.- Type Parameters:
T- type of the values processed by this method- Parameters:
c1- the first comparable, may be nullc2- the second comparable, may be null- Returns:
- a negative value if c1 < c2, zero if c1 = c2 and a positive value if c1 > c2
-
compare
Null safe comparison of Comparables.
- Type Parameters:
T- type of the values processed by this method- Parameters:
c1- the first comparable, may be nullc2- the second comparable, may be nullnullGreater- if truenullis considered greater than a non-nullvalue or if falsenullis considered less than a Non-nullvalue- Returns:
- a negative value if c1 < c2, zero if c1 = c2 and a positive value if c1 > c2
-
clone
public static <T> T clone(T obj) Clone an object.
- Type Parameters:
T- the type of the object- Parameters:
obj- the object to clone, null returns null- Returns:
- the clone if the object implements
Cloneableotherwisenull - Throws:
IllegalStateException- if the object is cloneable and the clone operation fails
-
cloneIfPossible
public 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 ofnullif 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.- Type Parameters:
T- the type of the object- Parameters:
obj- the object to clone, null returns null- Returns:
- the clone if the object implements
Cloneableotherwise the object itself - Throws:
IllegalStateException- if the object is cloneable and the clone operation fails
-
CONST
public static boolean CONST(boolean v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Parameters:
v- the boolean value to return- Returns:
- the boolean v, unchanged
-
CONST
public static byte CONST(byte v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Parameters:
v- the byte value to return- Returns:
- the byte v, unchanged
-
CONST_BYTE
public static byte CONST_BYTE(int v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Parameters:
v- the byte literal (as an int) value to return- Returns:
- the byte v, unchanged
- Throws:
IllegalArgumentException- if the value passed to v is larger than a byte, that is, smaller than -128 or larger than 127.
-
CONST
public static char CONST(char v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Parameters:
v- the char value to return- Returns:
- the char v, unchanged
-
CONST
public static short CONST(short v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Parameters:
v- the short value to return- Returns:
- the short v, unchanged
-
CONST_SHORT
public static short CONST_SHORT(int v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Parameters:
v- the short literal (as an int) value to return- Returns:
- the byte v, unchanged
- Throws:
IllegalArgumentException- if the value passed to v is larger than a short, that is, smaller than -32768 or larger than 32767.
-
CONST
public static int CONST(int v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Parameters:
v- the int value to return- Returns:
- the int v, unchanged
-
CONST
public static long CONST(long v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Parameters:
v- the long value to return- Returns:
- the long v, unchanged
-
CONST
public static float CONST(float v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Parameters:
v- the float value to return- Returns:
- the float v, unchanged
-
CONST
public static double CONST(double v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Parameters:
v- the double value to return- Returns:
- the double v, unchanged
-
CONST
public static <T> T CONST(T v) This method returns the provided value unchanged. This can prevent javac from inlining a constant field, e.g.,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.- Type Parameters:
T- the Object type- Parameters:
v- the genericized Object value to return (typically a String).- Returns:
- the genericized Object v, unchanged (typically a String).
-