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.
#ThreadSafe#
| 限定符和类型 | 类和说明 |
|---|---|
static class |
ObjectUtils.Null
Class used as a null placeholder where
null
has another meaning. |
| 限定符和类型 | 字段和说明 |
|---|---|
static ObjectUtils.Null |
NULL
Singleton used as a
null placeholder where
null has another meaning. |
| 构造器和说明 |
|---|
ObjectUtils()
ObjectUtils instances should NOT be constructed in
standard programming. |
| 限定符和类型 | 方法和说明 |
|---|---|
static int |
checkInRange(int i,
int start,
int end,
String name)
Checks that the given argument is in range.
|
static long |
checkInRange(long l,
long start,
long end,
String name)
Checks that the given argument is in range.
|
static <T> T |
checkNotNull(T arg,
String text)
Checks that the given argument is not null.
|
static int |
checkPositive(int i,
String name)
Checks that the given argument is strictly positive.
|
static long |
checkPositive(long l,
String name)
Checks that the given argument is strictly positive.
|
static int |
checkPositiveOrZero(int i,
String name)
Checks that the given argument is positive or zero.
|
static long |
checkPositiveOrZero(long l,
String name)
Checks that the given argument is positive or zero.
|
static int |
compare(Comparable c1,
Comparable c2)
Null safe comparison of Comparables.
|
static int |
compare(Comparable c1,
Comparable c2,
boolean nullGreater)
Null safe comparison of Comparables.
|
static Object |
defaultIfNull(Object object,
Object defaultValue)
Returns a default value if the object passed is
null. |
static boolean |
equals(Object object1,
Object object2)
Compares two objects for equality, where either one or both
objects may be
null. |
static int |
hashCode(Object obj)
Gets the hash code of an object returning zero when the
object is
null. |
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 Object |
max(Comparable c1,
Comparable c2)
Null safe comparison of Comparables.
|
static Object |
min(Comparable c1,
Comparable c2)
Null safe comparison of Comparables.
|
static boolean |
notEqual(Object object1,
Object object2)
Compares two objects for inequality, where either one or both
objects may be
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 |
toString(Object obj)
Gets the
toString of an Object returning
an empty string ("") if null input. |
static String |
toString(Object obj,
String nullStr)
Gets the
toString of an Object returning
a specified text if null input. |
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 ObjectUtils()
ObjectUtils instances should NOT be constructed in
standard programming. Instead, the class should be used as
ObjectUtils.defaultIfNull("a","b");.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static Object defaultIfNull(Object object, Object 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
object - the Object to test, may be nulldefaultValue - the default value to return, may be nullobject if it is not null, defaultValue otherwisepublic static boolean equals(Object object1, Object object2)
Compares two objects for equality, where either one or both
objects may be null.
ObjectUtils.equals(null, null) = true
ObjectUtils.equals(null, "") = false
ObjectUtils.equals("", null) = false
ObjectUtils.equals("", "") = true
ObjectUtils.equals(Boolean.TRUE, null) = false
ObjectUtils.equals(Boolean.TRUE, "true") = false
ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE) = true
ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false
object1 - the first object, may be nullobject2 - the second object, may be nulltrue if the values of both objects are the samepublic static boolean notEqual(Object object1, Object object2)
Compares two objects for inequality, where either one or both
objects may be null.
ObjectUtils.notEqual(null, null) = false
ObjectUtils.notEqual(null, "") = true
ObjectUtils.notEqual("", null) = true
ObjectUtils.notEqual("", "") = false
ObjectUtils.notEqual(Boolean.TRUE, null) = true
ObjectUtils.notEqual(Boolean.TRUE, "true") = true
ObjectUtils.notEqual(Boolean.TRUE, Boolean.TRUE) = false
ObjectUtils.notEqual(Boolean.TRUE, Boolean.FALSE) = true
object1 - the first object, may be nullobject2 - the second object, may be nullfalse if the values of both objects are the samepublic static int hashCode(Object obj)
Gets the hash code of an object returning zero when the
object is null.
ObjectUtils.hashCode(null) = 0 ObjectUtils.hashCode(obj) = obj.hashCode()
obj - the object to obtain the hash code of, may be nullpublic 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(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 String toString(Object obj)
Gets the toString of an Object returning
an empty string ("") if null input.
ObjectUtils.toString(null) = ""
ObjectUtils.toString("") = ""
ObjectUtils.toString("bat") = "bat"
ObjectUtils.toString(Boolean.TRUE) = "true"
obj - the Object to toString, may be nullnull inputStringUtils.defaultString(String),
String.valueOf(Object)public static String toString(Object obj, String nullStr)
Gets the toString of an Object returning
a specified text if null input.
ObjectUtils.toString(null, null) = null
ObjectUtils.toString(null, "null") = "null"
ObjectUtils.toString("", "null") = ""
ObjectUtils.toString("bat", "null") = "bat"
ObjectUtils.toString(Boolean.TRUE, "null") = "true"
obj - the Object to toString, may be nullnullStr - the String to return if null input, may be nullnull inputStringUtils.defaultString(String, String),
String.valueOf(Object)public static Object min(Comparable c1, Comparable c2)
c1 - the first comparable, may be nullc2 - the second comparable, may be nullpublic static Object max(Comparable c1, Comparable c2)
c1 - the first comparable, may be nullc2 - the second comparable, may be nullpublic static int compare(Comparable c1, Comparable c2)
null is assumed to be less than a non-null value.c1 - the first comparable, may be nullc2 - the second comparable, may be nullpublic static int compare(Comparable c1, Comparable c2, boolean nullGreater)
c1 - 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 valueComparator.compare(Object, Object)public 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 <T> T checkNotNull(T arg,
String text)
NullPointerException.
Otherwise, returns the argument.public static int checkPositive(int i,
String name)
IllegalArgumentException.
Otherwise, returns the argument.public static long checkPositive(long l,
String name)
IllegalArgumentException.
Otherwise, returns the argument.public static int checkPositiveOrZero(int i,
String name)
IllegalArgumentException.
Otherwise, returns the argument.public static long checkPositiveOrZero(long l,
String name)
IllegalArgumentException.
Otherwise, returns the argument.public static int checkInRange(int i,
int start,
int end,
String name)
IllegalArgumentException.
Otherwise, returns the argument.public static long checkInRange(long l,
long start,
long end,
String name)
IllegalArgumentException.
Otherwise, returns the argument.Copyright © 2021–2022. All rights reserved.