|
This class tries to handle null input gracefully. An exception will generally not be thrown for a null input. Each method documents its behavior in more detail. Nested Class Summarystatic class ObjectUtils.Null Field Detailpublic static final ObjectUtils.Null NULL Singleton used as a null placeholder where null has another meaning. For example, in a HashMap the HashMap.get(java.lang.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. Constructor Detailpublic 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. Method Detail
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 Parameters: objectthe Object to test, may be null defaultValuethe default value to return, may be null Returns: object if it is not null, defaultValue otherwise public static boolean equals(java.lang.Object object1, java.lang.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 Parameters: object1the first object, may be null object2the second object, may be null Returns: true if the values of both objects are the same public static java.lang.String identityToString(java.lang.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" Parameters: objectthe object to create a toString for, may be null Returns: the default toString text, or null if null passed in public static java.lang.StringBuffer appendIdentityToString(java.lang.StringBuffer buffer, java.lang.Object object) Appends the toString that would be produced by Object if a class did not override toString itself. null will return null. ObjectUtils.appendIdentityToString(*, null) = null ObjectUtils.appendIdentityToString(null, "") = "java.lang.String@1e23" ObjectUtils.appendIdentityToString(null, Boolean.TRUE) = "java.lang.Boolean@7fa" ObjectUtils.appendIdentityToString(buf, Boolean.TRUE) = buf.append("java.lang.Boolean@7fa") Parameters: bufferthe buffer to append to, may be null objectthe object to create a toString for, may be null Returns: the default toString text, or null if null passed in public static java.lang.String toString(java.lang.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" Parameters: objthe Object to toString, may be null Returns: the passed in Object's toString, or nullStr if null input See Also: StringUtils.defaultString(String), String.valueOf(Object) public static java.lang.String toString(java.lang.Object obj, java.lang.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" Parameters: objthe Object to toString, may be null nullStrthe String to return if null input, may be null Returns: the passed in Object's toString, or nullStr if null input See Also: StringUtils.defaultString(String,String), String.valueOf(Object) |
|