org.apache.commons.lang

Class ObjectUtils


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.

Since:
1.0

Nested Class Summary

static class
ObjectUtils.Null
Class used as a null placeholder where null has another meaning.

Field Summary

static ObjectUtils.Null
NULL
Singleton used as a null placeholder where null has another meaning.

Constructor Summary

ObjectUtils()
ObjectUtils instances should NOT be constructed in standard programming.

Method Summary

static StringBuffer
appendIdentityToString(StringBuffer buffer, Object object)
Deprecated. The design of this method is bad - see LANG-360.
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<T> c1, Comparable<T> c2)
Null safe comparison of Comparables.
static Object
min(Comparable<T> c1, Comparable<T> c2)
Null safe comparison of Comparables.
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.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

NULL

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.

Usages and Demos :

View More Examples of NULL
   1: 
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.logging.Log;
   4:         ...
   5:         if (value==null) {
   6:             return (Class) subclassesByDiscriminatorValue.get(ObjectUtils.NULL);
   7:         }
   8:         ...
   9:             if (discriminatorValue==null) {
  10:                 subclassesByDiscriminatorValue.put(ObjectUtils.NULL, mappedClass);
  11:             }
  12:         ...
  13:                 if ( "null".equals( sc.getDiscriminatorValue() ) ) {
  14:                     subclassesByDiscriminatorValue.put( ObjectUtils.NULL, sc.getPersistentClass() );

View Full Code Here

Constructor Details

ObjectUtils

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.

Method Details

appendIdentityToString

public static StringBuffer appendIdentityToString(StringBuffer buffer,
                                                  Object object)

Deprecated. The design of this method is bad - see LANG-360. Instead, use identityToString(StringBuffer, 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:
buffer - the buffer to append to, may be null
object - the object to create a toString for, may be null
Returns:
the default toString text, or null if null passed in
Since:
2.0
Usages and Demos :

View More Examples of appendIdentityToString(StringBuffer buffer,Object object)
   1: import org.apache.commons.lang.ClassUtils;
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.SystemUtils;
   4:         ...
   5:     protected void appendCyclicObject(StringBuffer buffer, String fieldName, Object value) {
   6:        ObjectUtils.appendIdentityToString(buffer, value);
   7:     }
   8: 
   9:     protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {

View Full Code Here

defaultIfNull

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
 
Parameters:
object - the Object to test, may be null
defaultValue - the default value to return, may be null
Returns:
object if it is not null, defaultValue otherwise
Usages and Demos :

View More Examples of defaultIfNull(Object object,Object defaultValue)
   1: import org.apache.commons.collections.IteratorUtils;
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.StringUtils;
   4:         ...
   5:                 try {
   6:                     value = ObjectUtils.defaultIfNull(PropertyUtils.getProperty(bean,this.getProperty()),StringUtils.EMPTY);
   7:                 }
   8:         ...
   9:                 try {
  10:                     label = ObjectUtils.defaultIfNull(PropertyUtils.getProperty(bean,this.getLabelProperty()),value);
  11:         ...
  12:                     label = ObjectUtils.defaultIfNull(label,StringUtils.EMPTY);

View Full Code Here
   1: import org.apache.commons.lang.BooleanUtils;
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.StringUtils;
   4:         ...
   5:             for (int i = 0; i < valuesArray.length; i++) {
   6:                 values[i] = ObjectUtils.defaultIfNull(valuesArray[i],StringUtils.EMPTY).toString();
   7:             }
   8:         ...
   9:         else {
  10:             values = new String[] { ObjectUtils.defaultIfNull(value,StringUtils.EMPTY).toString() };
  11:         }

View Full Code Here

equals

public 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
 
Parameters:
object1 - the first object, may be null
object2 - the second object, may be null
Returns:
true if the values of both objects are the same
Usages and Demos :

View More Examples of equals(Object object1,Object object2)
   1: 
   2: import org.apache.commons.lang.ObjectUtils;
   3: 
   4:         ...
   5:     public boolean equals(Object x, Object y) {
   6:         return ObjectUtils.equals(x, y);
   7:     }
   8:             
   9:     public String toXML(Object value) {

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ObjectUtils;
   3: 
   4:         ...
   5:     public boolean equals(Object x, Object y) {
   6:         return ObjectUtils.equals(x, y);
   7:     }
   8:     
   9:     public String toXML(Object value) {

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ObjectUtils;
   3: 
   4:         ...
   5:     public boolean equals(Object x, Object y) {
   6:         return ObjectUtils.equals(x, y);
   7:     }
   8:     public String toXML(Object value) {
   9:         return (String) value;

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ObjectUtils;
   3: 
   4:         ...
   5:     public boolean equals(Object x, Object y) {
   6:         return ObjectUtils.equals(x, y);
   7:     }
   8:     public String toXML(Object value) {
   9:         return (String) value;

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ObjectUtils;
   3: 
   4:         ...
   5:     public boolean equals(Object x, Object y) throws HibernateException {
   6:         return ObjectUtils.equals(x, y);
   7:     }
   8: 
   9:     public String getName() {

View Full Code Here

hashCode

public 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()
 
Parameters:
obj - the object to obtain the hash code of, may be null
Returns:
the hash code of the object, or zero if null
Since:
2.1

identityToString

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"
 
Parameters:
object - the object to create a toString for, may be null
Returns:
the default toString text, or null if null passed in
Usages and Demos :

View More Examples of identityToString(Object object)
   1: 
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.builder.HashCodeBuilder;
   4:         ...
   5:         }
   6:         return ObjectUtils.identityToString(this) + ".id=" + getId();
   7:     }
   8: }

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.builder.HashCodeBuilder;
   4:         ...
   5:         }
   6:         return ObjectUtils.identityToString(this) + ".id=" + getId();
   7:     }
   8: }

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.builder.ToStringStyle;
   4:         ...
   5:         }
   6:         return ObjectUtils.identityToString(this);
   7:     }
   8: 

View Full Code Here

identityToString

public 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")
 
Parameters:
buffer - the buffer to append to
object - the object to create a toString for
Since:
2.4

max

public static Object max(Comparable<T> c1,
                         Comparable<T> c2)
Null safe comparison of Comparables.
Parameters:
c1 - the first comparable, may be null
c2 - the second comparable, may be null
Returns:
  • If both objects are non-null and unequal, the greater object.
  • If both objects are non-null and equal, c1.
  • If one of the comparables is null, the non-null object.
  • If both the comparables are null, null is returned.

min

public static Object min(Comparable<T> c1,
                         Comparable<T> c2)
Null safe comparison of Comparables.
Parameters:
c1 - the first comparable, may be null
c2 - the second comparable, may be null
Returns:
  • If both objects are non-null and unequal, the lesser object.
  • If both objects are non-null and equal, c1.
  • If one of the comparables is null, the non-null object.
  • If both the comparables are null, null is returned.

toString

public 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"
 
Parameters:
obj - the Object to toString, may be null
Returns:
the passed in Object's toString, or nullStr if null input
Since:
2.0
Usages and Demos :

View More Examples of toString(Object obj)
   1: import org.apache.commons.collections.Transformer;
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.StringUtils;
   4:         ...
   5:                     Boolean.valueOf(
   6:                         ObjectUtils.toString(this.findTaggedValue(UMLProfile.TAGGEDVALUE_PRESENTATION_IS_TABLE)))
   7:                            .booleanValue();
   8:                 if (!isTable)
   9:                 {

View Full Code Here
   1: import org.andromda.core.configuration.Configuration;
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.maven.artifact.Artifact;
   4:         ...
   5:                     properties,
   6:                     ObjectUtils.toString(properties.get(property)));
   7:             properties.put(
   8:                 property,
   9:                 value);

View Full Code Here
   1: import org.andromda.utils.StringUtilsHelper;
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.StringUtils;
   4:         ...
   5:     {
   6:         return ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.VIEW_POPULATOR_PATTERN)).replaceAll(
   7:             "\\{0\\}",
   8:         ...
   9:         final Object formKeyValue = this.findTaggedValue(JSFProfile.TAGGEDVALUE_ACTION_FORM_KEY);
  10:         return formKeyValue == null ? ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.ACTION_FORM_KEY))
  11:                                     : String.valueOf(formKeyValue);
  12:         ...
  13:     {
  14:         return ObjectUtils.toString(this.findTaggedValue(JSFProfile.TAGGEDVALUE_VIEW_TYPE)).equalsIgnoreCase(

View Full Code Here
   1: import org.andromda.utils.StringUtilsHelper;
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.StringUtils;
   4:         ...
   5:     {
   6:         return ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.VIEW_POPULATOR_PATTERN)).replaceAll(
   7:             "\\{0\\}",
   8:         ...
   9:         final Object formKeyValue = this.findTaggedValue(JSFProfile.TAGGEDVALUE_ACTION_FORM_KEY);
  10:         return formKeyValue == null ? ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.ACTION_FORM_KEY))
  11:                                     : String.valueOf(formKeyValue);
  12:         ...
  13:     {
  14:         return ObjectUtils.toString(this.findTaggedValue(JSFProfile.TAGGEDVALUE_VIEW_TYPE)).equalsIgnoreCase(

View Full Code Here
   1: import org.andromda.utils.StringUtilsHelper;
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.StringUtils;
   4:         ...
   5:         final String normalizeMessages =
   6:             ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.NORMALIZE_MESSAGES));
   7:         return Boolean.valueOf(normalizeMessages).booleanValue();
   8:         ...
   9:             StringUtils.replace(
  10:                 ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.BACKING_LIST_PATTERN)),
  11:                 "{0}",
  12:         ...
  13:             StringUtils.replace(
  14:                 ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.BACKING_VALUE_PATTERN)),

View Full Code Here

toString

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"
 
Parameters:
obj - the Object to toString, may be null
nullStr - the String to return if null input, may be null
Returns:
the passed in Object's toString, or nullStr if null input
Since:
2.0
Usages and Demos :

View More Examples of toString(Object obj,String nullStr)
   1: import javax.portlet.RenderResponse;
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.logging.Log;
   4:         ...
   5:             Object attrNm = attrIter.nextElement();
   6:             Object attrVal = request.getAttribute(ObjectUtils.toString(attrNm));
   7:             String attrValDesc = ( attrVal instanceof String ) ? (String)attrVal : (( attrVal == null ) ? "null" : attrVal.getClass().getName() );
   8:         ...
   9:             log.info( "   key=" + ObjectUtils.toString(attrNm,"null") + " value=" + attrValDesc);
  10:         }
  11:         ...
  12:             Object attrNm = attrIter.nextElement();
  13:             Object attrVal = request.getPortletSession().getAttribute(ObjectUtils.toString(attrNm));

View Full Code Here
   1: import org.apache.commons.collections.map.PredicatedMap;
   2: import org.apache.commons.lang.ObjectUtils;
   3: import org.apache.commons.lang.builder.EqualsBuilder;
   4:         ...
   5:     {
   6:         String value = ObjectUtils.toString(objValue, null);
   7: 
   8:         if (name != null && name.startsWith(PARAM_PREFIX))
   9:         {

View Full Code Here