org.omnaest.utils.structure.element
Class ObjectUtils

java.lang.Object
  extended by org.omnaest.utils.structure.element.ObjectUtils

public class ObjectUtils
extends Object

Helper which offers methods allowing to deal with arbitrary Objects

Author:
Omnaest

Nested Class Summary
protected static class ObjectUtils.PrimitiveTypeArrayAnalysisResult
          Result of the analysis for primitive arrays
 
Constructor Summary
ObjectUtils()
           
 
Method Summary
static
<C> C
castArrayTo(Class<C> wrapperType, Class<?> elementType, Object array)
          Casts a given Array to a wrapper Class type and its elements to the element Class type using castTo(Class, Object).
static
<C> C
castArrayToMap(Class<? extends C> mapType, Class<?> keyType, Class<?> valueType, Object array)
          Casts a given Array to a Map class.
static
<C> C
castTo(Class<C> type, Object object)
          Casts a given Object to a given Class type.
static
<O> O
defaultIfNull(O object, Factory<O> defaultObjectFactory)
          Returns the object parameter if it is not null, otherwise the object created by the given Factory .
static
<O> O
defaultIfNull(O object, O defaultObject)
          Similar to defaultIfNull(Object, Object, Object...)
static
<O> O
defaultIfNull(O object, O defaultObject, O... defaultObjects)
          Returns the object parameter if it is not null, otherwise the defaultObject.
static boolean isAnyTypeAssignableFromInstance(Iterable<Class<?>> targetTypes, Object object)
          Returns true if the given object instance is assignable to at least one of the given types.
static boolean isAnyTypeAssignableFromType(Iterable<Class<?>> targetTypes, Class<?> sourceType)
          Returns true if the given source type instance is assignable to at least one of the given target types.
static boolean isArray(Object object)
          Returns true if the given Object is not null and an Array type.
static boolean isAssignableFromInstance(Class<?> targetType, Object object)
          Returns true if the given target Class is Class.isAssignableFrom(Class) from the given Object.
static boolean isCollection(Object object)
          Returns true if the given Object is not null and a Collection derived type
static boolean isIterable(Object object)
          Returns true if the given Object is not null and a Iterable derived type
static boolean isList(Object object)
          Returns true if the given Object is not null and a List derived type
static boolean isMap(Object object)
          Returns true if the given Object is not null and a Map derived type
static boolean isPrimitiveOrPrimitiveWrapperType(Class<?> type)
          Returns true if isPrimitiveType(Class) or isPrimitiveWrapperType(Class) returns true
static boolean isPrimitiveType(Class<?> type)
          Returns true if type is not null and Class.isPrimitive() is true for the given type
static boolean isPrimitiveWrapperType(Class<?> type)
          Returns true if the given Class type is a wrapper type of a primitive.
static boolean isSortedMap(Object object)
          Returns true if the given Object is not null and a SortedMap derived type
static boolean isSortedSet(Object object)
          Returns true if the given Object is not null and a SortedSet derived type
static boolean isString(Object object)
           isString( null ) = false isString( "" ) = true isString( "xyz" ) = true isString( 123 ) = false
static Class<?> objectTypeFor(Class<?> type)
          Returns the Object type for the given type.
static Class<?> primitiveWrapperTypeFor(Class<?> primitiveType)
          Returns the auto boxing type for a primitive Class type.
static String toStringAsNestedHierarchy(Object object)
          Generates a hierarchy representation of the object graph for the given Object.
static String toStringAsSimpleNestedHierarchy(Object object)
          Similar to toStringAsNestedHierarchy(Object) but with reduced information
static ObjectTreeNavigator treeNavigator(Object object)
          Returns a ObjectTreeNavigator for the given Object
static Class<?> typeOf(Object instance)
          Returns the Class of the given instance or null if null is given as instance
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectUtils

public ObjectUtils()
Method Detail

castArrayToMap

public static <C> C castArrayToMap(Class<? extends C> mapType,
                                   Class<?> keyType,
                                   Class<?> valueType,
                                   Object array)
Casts a given Array to a Map class. This is done by toggling between key and value for every index position of the given array.

Parameters:
mapType -
keyType -
valueType -
array -
Returns:

castArrayTo

public static <C> C castArrayTo(Class<C> wrapperType,
                                Class<?> elementType,
                                Object array)
Casts a given Array to a wrapper Class type and its elements to the element Class type using castTo(Class, Object).

Parameters:
wrapperType -
elementType -
array -
Returns:

castTo

public static <C> C castTo(Class<C> type,
                           Object object)
Casts a given Object to a given Class type. If the given type is a primitive the valueOf(...) method is used.

Parameters:
type -
object -
Returns:

isPrimitiveWrapperType

public static boolean isPrimitiveWrapperType(Class<?> type)
Returns true if the given Class type is a wrapper type of a primitive.

Parameters:
type -
Returns:

isPrimitiveType

public static boolean isPrimitiveType(Class<?> type)
Returns true if type is not null and Class.isPrimitive() is true for the given type

Parameters:
type -
Returns:

isPrimitiveOrPrimitiveWrapperType

public static boolean isPrimitiveOrPrimitiveWrapperType(Class<?> type)
Returns true if isPrimitiveType(Class) or isPrimitiveWrapperType(Class) returns true

Parameters:
type -
Returns:

isString

public static boolean isString(Object object)
 isString( null ) = false
 isString( "" ) = true
 isString( "xyz" ) = true
 isString( 123 ) = false
 

Parameters:
object -
Returns:

isMap

public static boolean isMap(Object object)
Returns true if the given Object is not null and a Map derived type

Parameters:
object -
Returns:

isSortedMap

public static boolean isSortedMap(Object object)
Returns true if the given Object is not null and a SortedMap derived type

Parameters:
object -
Returns:

isSortedSet

public static boolean isSortedSet(Object object)
Returns true if the given Object is not null and a SortedSet derived type

Parameters:
object -
Returns:

isList

public static boolean isList(Object object)
Returns true if the given Object is not null and a List derived type

Parameters:
object -
Returns:

isCollection

public static boolean isCollection(Object object)
Returns true if the given Object is not null and a Collection derived type

Parameters:
object -
Returns:

isIterable

public static boolean isIterable(Object object)
Returns true if the given Object is not null and a Iterable derived type

Parameters:
object -
Returns:

isAssignableFromInstance

public static boolean isAssignableFromInstance(Class<?> targetType,
                                               Object object)
Returns true if the given target Class is Class.isAssignableFrom(Class) from the given Object. In other words is the assignment "TargetType target = (TargetType) object;" valid

Parameters:
targetType -
object -
Returns:

isAnyTypeAssignableFromType

public static boolean isAnyTypeAssignableFromType(Iterable<Class<?>> targetTypes,
                                                  Class<?> sourceType)
Returns true if the given source type instance is assignable to at least one of the given target types.

Parameters:
targetTypes -
sourceType -
Returns:

isAnyTypeAssignableFromInstance

public static boolean isAnyTypeAssignableFromInstance(Iterable<Class<?>> targetTypes,
                                                      Object object)
Returns true if the given object instance is assignable to at least one of the given types.

Parameters:
targetTypes -
object -
Returns:

isArray

public static boolean isArray(Object object)
Returns true if the given Object is not null and an Array type.

Parameters:
object -
Returns:
See Also:
Class.isArray()

objectTypeFor

public static Class<?> objectTypeFor(Class<?> type)
Returns the Object type for the given type.

 objectTypeFor(null) = null
 objectTypeFor(double) = Double
 objectTypeFor(String) = String
 objectTypeFor(Object) = Object
 

Parameters:
type -
Returns:
See Also:
primitiveWrapperTypeFor(Class), isPrimitiveOrPrimitiveWrapperType(Class)

primitiveWrapperTypeFor

public static Class<?> primitiveWrapperTypeFor(Class<?> primitiveType)
Returns the auto boxing type for a primitive Class type.

 wrapperTypeForPrimitiveType( null ) = null
 wrapperTypeForPrimitiveType( boolean ) = Boolean
 wrapperTypeForPrimitiveType( double ) = Double
 wrapperTypeForPrimitiveType( primitiveType ) = wrapperType
 wrapperTypeForPrimitiveType( Double ) = Double
 wrapperTypeForPrimitiveType( wrapperType ) = wrapperType
 wrapperTypeForPrimitiveType( String ) = null
 

Parameters:
primitiveType -
Returns:
See Also:
objectTypeFor(Class)

defaultIfNull

public static <O> O defaultIfNull(O object,
                                  O defaultObject)
Similar to defaultIfNull(Object, Object, Object...)

Parameters:
object -
defaultObject -
Returns:

defaultIfNull

public static <O> O defaultIfNull(O object,
                                  O defaultObject,
                                  O... defaultObjects)
Returns the object parameter if it is not null, otherwise the defaultObject.

Examples:
 ObjectUtils.defaultObject( x, * ) = x;
 ObjectUtils.defaultObject( null, default ) = default;
 ObjectUtils.defaultObject( null, null, null, default ) = default;
 ObjectUtils.defaultObject( null, default, null, null ) = default;
 ObjectUtils.defaultObject( null, default1, null, default2 ) = default1;
 ObjectUtils.defaultObject( null, null ) = null;
 

Parameters:
object -
defaultObject -
defaultObjects -
Returns:

defaultIfNull

public static <O> O defaultIfNull(O object,
                                  Factory<O> defaultObjectFactory)
Returns the object parameter if it is not null, otherwise the object created by the given Factory .

Examples:
 ObjectUtils.defaultObject( x, * ) = x;
 ObjectUtils.defaultObject( null, factory ) = factory.newInstance();
 ObjectUtils.defaultObject( null, null ) = null;
 

Parameters:
object -
defaultObjectFactory -
Returns:

toStringAsNestedHierarchy

public static String toStringAsNestedHierarchy(Object object)
Generates a hierarchy representation of the object graph for the given Object.

E.g.:
 |--[ org.omnaest.utils.structure.hierarchy.tree.adapter.ObjectToTreeNodeAdapterTest$TestClass@5801319c ]
    |--[ fieldDouble = 1.234 ]
    |--[ fieldString = value1 ]
    |--[ fieldString2 = value2 ]
    |--[ testClassSub = org.omnaest.utils.structure.hierarchy.tree.adapter.ObjectToTreeNodeAdapterTest$TestClassSub@790bc49d ]
       |--[ fieldString = value3 ]
 

Parameters:
object -
Returns:

toStringAsSimpleNestedHierarchy

public static String toStringAsSimpleNestedHierarchy(Object object)
Similar to toStringAsNestedHierarchy(Object) but with reduced information

Parameters:
object -
Returns:

treeNavigator

public static ObjectTreeNavigator treeNavigator(Object object)
Returns a ObjectTreeNavigator for the given Object

Parameters:
object -
Returns:

typeOf

public static Class<?> typeOf(Object instance)
Returns the Class of the given instance or null if null is given as instance

Parameters:
instance -
Returns:


Copyright © 2013. All Rights Reserved.