org.apache.camel.util
Class ObjectHelper

java.lang.Object
  extended by org.apache.camel.util.ObjectHelper

public final class ObjectHelper
extends Object

A number of useful helper methods for working with Objects

Version:
$Revision: 17646 $

Method Summary
static String after(String text, String after)
           
static String asString(Object[] objects)
          Turns the given object arrays into a meaningful string
static String before(String text, String before)
           
static String between(String text, String after, String before)
           
static String capitalize(String text)
           
static
<T> T
cast(Class<T> toType, Object value)
          Converts the given value to the required type or throw a meaningful exception
static String classCanonicalName(Object value)
          Returns the canonical type name of the given value
static String className(Object value)
          Returns the type name of the given value
static void close(Closeable closeable, String name, org.apache.commons.logging.Log log)
          Closes the given resource if it is available, logging any closing exceptions to the given log
static int compare(Object a, Object b)
          A helper method for performing an ordered comparison on the objects handling nulls and objects which do not handle sorting gracefully
static int compare(Object a, Object b, boolean ignoreCase)
          A helper method for performing an ordered comparison on the objects handling nulls and objects which do not handle sorting gracefully
static boolean contains(Object collectionOrArray, Object value)
          Returns true if the collection contains the specified value
static Class<?> convertPrimitiveTypeToWrapperType(Class<?> type)
          Converts primitive types such as int to its wrapper type like Integer
static Iterator<Throwable> createExceptionIterator(Throwable exception)
          Creates an iterator to walk the exception from the bottom up (the last caused by going upwards to the root exception).
static Iterator<Object> createIterator(Object value)
          Creates an iterator over the value if the value is a collection, an Object[] or a primitive type array; otherwise to simplify the caller's code, we just create a singleton collection iterator over a single value

Will default use comma for String separating String values.

static Iterator<Object> createIterator(Object value, String delimiter)
          Creates an iterator over the value if the value is a collection, an Object[] or a primitive type array; otherwise to simplify the caller's code, we just create a singleton collection iterator over a single value
static boolean equal(Object a, Object b)
          A helper method for comparing objects for equality while handling nulls
static boolean equalByteArray(byte[] a, byte[] b)
          A helper method for comparing byte arrays for equality while handling nulls
static boolean evaluateValuePredicate(Object value)
          Evaluate the value as a predicate which attempts to convert the value to a boolean otherwise true is returned if the value is not null
static List<Method> findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType)
          Returns a list of methods which are annotated with the given annotation
static List<Method> findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType, boolean checkMetaAnnotations)
          Returns a list of methods which are annotated with the given annotation
static String getDefaultCharacterSet()
          Helper method to return the default character set name
static
<T> T
getException(Class<T> type, Throwable exception)
          Retrieves the given exception type from the exception.
static String getIdentityHashCode(Object object)
           
static String getPropertyName(Method method)
          Returns the Java Bean property name of the given method, if it is a setter
static Scanner getScanner(Exchange exchange, Object value)
          Creates a Scanner for scanning the given value.
static boolean getSystemProperty(String name, Boolean defaultValue)
          A helper method to access a boolean system property, catching any security exceptions
static String getSystemProperty(String name, String defaultValue)
          A helper method to access a system property, catching any security exceptions
static boolean hasAnnotation(AnnotatedElement elem, Class<? extends Annotation> annotationType, boolean checkMetaAnnotations)
          Checks if a Class or Method are annotated with the given annotation
static boolean hasAnnotation(Annotation[] annotations, Class<?> type)
          Returns true if the given collection of annotations matches the given type
static Object invokeMethod(Method method, Object instance, Object... parameters)
          A helper method to invoke a method via reflection and wrap any exceptions as RuntimeCamelException instances
static boolean isAssignableFrom(Class<?> a, Class<?> b)
          Returns true if a class is assignable from another class like the Class.isAssignableFrom(Class) method but which also includes coercion between primitive types to deal with Java 5 primitive type wrapping
static boolean isEmpty(Object value)
          Tests whether the value is null or an empty string.
static boolean isEqualToAny(Object object, Object... values)
          Returns true if the given object is equal to any of the expected value
static boolean isJavaIdentifier(String name)
          Returns true if the given name is a valid java identifier
static boolean isNotEmpty(Object value)
          Tests whether the value is not null or an empty string.
static boolean isOverridingMethod(Method source, Method target)
          Tests whether the target method overrides the source method.
static Class<?> loadClass(String name)
          Attempts to load the given class name using the thread context class loader or the class loader used to load this class
static Class<?> loadClass(String name, ClassLoader loader)
          Attempts to load the given class name using the thread context class loader or the given class loader
static Class<?> loadClass(String name, ClassLoader loader, boolean needToWarn)
          Attempts to load the given class name using the thread context class loader or the given class loader
static InputStream loadResourceAsStream(String name)
          Attempts to load the given resource as a stream using the thread context class loader or the class loader used to load this class
static URL loadResourceAsURL(String name)
          Attempts to load the given resource as a stream using the thread context class loader or the class loader used to load this class
static boolean matches(List<Object> list)
          Returns the predicate matching boolean on a List result set where if the first element is a boolean its value is used otherwise this method returns true if the collection is not empty
static String name(Class<?> type)
          Returns the type name of the given type or null if the type variable is null
static
<T> T
newInstance(Class<?> actualType, Class<T> expectedType)
          A helper method to create a new instance of a type using the default constructor arguments.
static
<T> T
newInstance(Class<T> type)
          A helper method to create a new instance of a type using the default constructor arguments.
static String normalizeClassName(String name)
          Cleans the string to pure java identifier so we can use it for loading class names.
static void notEmpty(String value, String name)
          Asserts whether the string is not empty.
static void notEmpty(String value, String name, Object on)
          Asserts whether the string is not empty.
static void notNull(Object value, String name)
          Asserts whether the value is not null
static void notNull(Object value, String name, Object on)
          Asserts whether the value is not null
static String removeStartingCharacters(String text, char ch)
          Removes any starting characters on the given text which match the given character
static String[] splitOnCharacter(String value, String needle, int count)
           
static Boolean toBoolean(Object value)
           
static Object type(Object bean)
          Returns the type of the given object or null if the value is null
static int typeCoerceCompare(TypeConverter converter, Object leftValue, Object rightValue)
          A helper method for comparing objects ordering in which it uses type coerce to coerce types between the left and right values.
static boolean typeCoerceEquals(TypeConverter converter, Object leftValue, Object rightValue)
          A helper method for comparing objects for equality in which it uses type coerce to coerce types between the left and right values.
static boolean typeCoerceNotEquals(TypeConverter converter, Object leftValue, Object rightValue)
          A helper method for comparing objects for equality in which it uses type coerce to coerce types between the left and right values.
static CamelExecutionException wrapCamelExecutionException(Exchange exchange, Throwable e)
          Wraps the caused exception in a CamelExecutionException if its not already such an exception.
static RuntimeCamelException wrapRuntimeCamelException(Throwable e)
          Wraps the caused exception in a RuntimeCamelException if its not already such an exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

typeCoerceEquals

public static boolean typeCoerceEquals(TypeConverter converter,
                                       Object leftValue,
                                       Object rightValue)
A helper method for comparing objects for equality in which it uses type coerce to coerce types between the left and right values. This allows you to equal test eg String and Integer as Camel will be able to coerce the types


typeCoerceNotEquals

public static boolean typeCoerceNotEquals(TypeConverter converter,
                                          Object leftValue,
                                          Object rightValue)
A helper method for comparing objects for equality in which it uses type coerce to coerce types between the left and right values. This allows you to equal test eg String and Integer as Camel will be able to coerce the types


typeCoerceCompare

public static int typeCoerceCompare(TypeConverter converter,
                                    Object leftValue,
                                    Object rightValue)
A helper method for comparing objects ordering in which it uses type coerce to coerce types between the left and right values. This allows you to equal test eg String and Integer as Camel will be able to coerce the types


equal

public static boolean equal(Object a,
                            Object b)
A helper method for comparing objects for equality while handling nulls


equalByteArray

public static boolean equalByteArray(byte[] a,
                                     byte[] b)
A helper method for comparing byte arrays for equality while handling nulls


isEqualToAny

public static boolean isEqualToAny(Object object,
                                   Object... values)
Returns true if the given object is equal to any of the expected value


compare

public static int compare(Object a,
                          Object b)
A helper method for performing an ordered comparison on the objects handling nulls and objects which do not handle sorting gracefully


compare

public static int compare(Object a,
                          Object b,
                          boolean ignoreCase)
A helper method for performing an ordered comparison on the objects handling nulls and objects which do not handle sorting gracefully

Parameters:
a - the first object
b - the second object
ignoreCase - ignore case for string comparison

toBoolean

public static Boolean toBoolean(Object value)

notNull

public static void notNull(Object value,
                           String name)
Asserts whether the value is not null

Parameters:
value - the value to test
name - the key that resolved the value
Throws:
IllegalArgumentException - is thrown if assertion fails

notNull

public static void notNull(Object value,
                           String name,
                           Object on)
Asserts whether the value is not null

Parameters:
value - the value to test
on - additional description to indicate where this problem occured (appended as toString())
name - the key that resolved the value
Throws:
IllegalArgumentException - is thrown if assertion fails

notEmpty

public static void notEmpty(String value,
                            String name)
Asserts whether the string is not empty.

Parameters:
value - the string to test
name - the key that resolved the value
Throws:
IllegalArgumentException - is thrown if assertion fails

notEmpty

public static void notEmpty(String value,
                            String name,
                            Object on)
Asserts whether the string is not empty.

Parameters:
value - the string to test
on - additional description to indicate where this problem occured (appended as toString())
name - the key that resolved the value
Throws:
IllegalArgumentException - is thrown if assertion fails

isEmpty

public static boolean isEmpty(Object value)
Tests whether the value is null or an empty string.

Parameters:
value - the value, if its a String it will be tested for text length as well
Returns:
true if empty

isNotEmpty

public static boolean isNotEmpty(Object value)
Tests whether the value is not null or an empty string.

Parameters:
value - the value, if its a String it will be tested for text length as well
Returns:
true if not empty

splitOnCharacter

public static String[] splitOnCharacter(String value,
                                        String needle,
                                        int count)

removeStartingCharacters

public static String removeStartingCharacters(String text,
                                              char ch)
Removes any starting characters on the given text which match the given character

Parameters:
text - the string
ch - the initial characters to remove
Returns:
either the original string or the new substring

capitalize

public static String capitalize(String text)

after

public static String after(String text,
                           String after)

before

public static String before(String text,
                            String before)

between

public static String between(String text,
                             String after,
                             String before)

contains

public static boolean contains(Object collectionOrArray,
                               Object value)
Returns true if the collection contains the specified value


createIterator

public static Iterator<Object> createIterator(Object value)
Creates an iterator over the value if the value is a collection, an Object[] or a primitive type array; otherwise to simplify the caller's code, we just create a singleton collection iterator over a single value

Will default use comma for String separating String values.

Parameters:
value - the value
Returns:
the iterator

createIterator

public static Iterator<Object> createIterator(Object value,
                                              String delimiter)
Creates an iterator over the value if the value is a collection, an Object[] or a primitive type array; otherwise to simplify the caller's code, we just create a singleton collection iterator over a single value

Parameters:
value - the value
delimiter - delimiter for separating String values
Returns:
the iterator

matches

public static boolean matches(List<Object> list)
Returns the predicate matching boolean on a List result set where if the first element is a boolean its value is used otherwise this method returns true if the collection is not empty

Returns:
true if the first element is a boolean and its value is true or if the list is non empty

getSystemProperty

public static String getSystemProperty(String name,
                                       String defaultValue)
A helper method to access a system property, catching any security exceptions

Parameters:
name - the name of the system property required
defaultValue - the default value to use if the property is not available or a security exception prevents access
Returns:
the system property value or the default value if the property is not available or security does not allow its access

getSystemProperty

public static boolean getSystemProperty(String name,
                                        Boolean defaultValue)
A helper method to access a boolean system property, catching any security exceptions

Parameters:
name - the name of the system property required
defaultValue - the default value to use if the property is not available or a security exception prevents access
Returns:
the boolean representation of the system property value or the default value if the property is not available or security does not allow its access

name

public static String name(Class<?> type)
Returns the type name of the given type or null if the type variable is null


className

public static String className(Object value)
Returns the type name of the given value


classCanonicalName

public static String classCanonicalName(Object value)
Returns the canonical type name of the given value


loadClass

public static Class<?> loadClass(String name)
Attempts to load the given class name using the thread context class loader or the class loader used to load this class

Parameters:
name - the name of the class to load
Returns:
the class or null if it could not be loaded

loadClass

public static Class<?> loadClass(String name,
                                 ClassLoader loader)
Attempts to load the given class name using the thread context class loader or the given class loader

Parameters:
name - the name of the class to load
loader - the class loader to use after the thread context class loader
Returns:
the class or null if it could not be loaded

loadClass

public static Class<?> loadClass(String name,
                                 ClassLoader loader,
                                 boolean needToWarn)
Attempts to load the given class name using the thread context class loader or the given class loader

Parameters:
name - the name of the class to load
loader - the class loader to use after the thread context class loader
needToWarn - if it is true will use log a warning message for not loading the class
Returns:
the class or null if it could not be loaded

loadResourceAsStream

public static InputStream loadResourceAsStream(String name)
Attempts to load the given resource as a stream using the thread context class loader or the class loader used to load this class

Parameters:
name - the name of the resource to load
Returns:
the stream or null if it could not be loaded

loadResourceAsURL

public static URL loadResourceAsURL(String name)
Attempts to load the given resource as a stream using the thread context class loader or the class loader used to load this class

Parameters:
name - the name of the resource to load
Returns:
the stream or null if it could not be loaded

invokeMethod

public static Object invokeMethod(Method method,
                                  Object instance,
                                  Object... parameters)
A helper method to invoke a method via reflection and wrap any exceptions as RuntimeCamelException instances

Parameters:
method - the method to invoke
instance - the object instance (or null for static methods)
parameters - the parameters to the method
Returns:
the result of the method invocation

isOverridingMethod

public static boolean isOverridingMethod(Method source,
                                         Method target)
Tests whether the target method overrides the source method.

Tests whether they have the same name, return type, and parameter list.

Parameters:
source - the source method
target - the target method
Returns:
true if it override, false otherwise

findMethodsWithAnnotation

public static List<Method> findMethodsWithAnnotation(Class<?> type,
                                                     Class<? extends Annotation> annotationType)
Returns a list of methods which are annotated with the given annotation

Parameters:
type - the type to reflect on
annotationType - the annotation type
Returns:
a list of the methods found

findMethodsWithAnnotation

public static List<Method> findMethodsWithAnnotation(Class<?> type,
                                                     Class<? extends Annotation> annotationType,
                                                     boolean checkMetaAnnotations)
Returns a list of methods which are annotated with the given annotation

Parameters:
type - the type to reflect on
annotationType - the annotation type
checkMetaAnnotations - check for meta annotations
Returns:
a list of the methods found

hasAnnotation

public static boolean hasAnnotation(AnnotatedElement elem,
                                    Class<? extends Annotation> annotationType,
                                    boolean checkMetaAnnotations)
Checks if a Class or Method are annotated with the given annotation

Parameters:
elem - the Class or Method to reflect on
annotationType - the annotation type
checkMetaAnnotations - check for meta annotations
Returns:
true if annotations is present

asString

public static String asString(Object[] objects)
Turns the given object arrays into a meaningful string

Parameters:
objects - an array of objects or null
Returns:
a meaningful string

isAssignableFrom

public static boolean isAssignableFrom(Class<?> a,
                                       Class<?> b)
Returns true if a class is assignable from another class like the Class.isAssignableFrom(Class) method but which also includes coercion between primitive types to deal with Java 5 primitive type wrapping


convertPrimitiveTypeToWrapperType

public static Class<?> convertPrimitiveTypeToWrapperType(Class<?> type)
Converts primitive types such as int to its wrapper type like Integer


getDefaultCharacterSet

public static String getDefaultCharacterSet()
Helper method to return the default character set name


getPropertyName

public static String getPropertyName(Method method)
Returns the Java Bean property name of the given method, if it is a setter


hasAnnotation

public static boolean hasAnnotation(Annotation[] annotations,
                                    Class<?> type)
Returns true if the given collection of annotations matches the given type


close

public static void close(Closeable closeable,
                         String name,
                         org.apache.commons.logging.Log log)
Closes the given resource if it is available, logging any closing exceptions to the given log

Parameters:
closeable - the object to close
name - the name of the resource
log - the log to use when reporting closure warnings

cast

public static <T> T cast(Class<T> toType,
                         Object value)
Converts the given value to the required type or throw a meaningful exception


newInstance

public static <T> T newInstance(Class<T> type)
A helper method to create a new instance of a type using the default constructor arguments.


newInstance

public static <T> T newInstance(Class<?> actualType,
                                Class<T> expectedType)
A helper method to create a new instance of a type using the default constructor arguments.


isJavaIdentifier

public static boolean isJavaIdentifier(String name)
Returns true if the given name is a valid java identifier


type

public static Object type(Object bean)
Returns the type of the given object or null if the value is null


evaluateValuePredicate

public static boolean evaluateValuePredicate(Object value)
Evaluate the value as a predicate which attempts to convert the value to a boolean otherwise true is returned if the value is not null


wrapRuntimeCamelException

public static RuntimeCamelException wrapRuntimeCamelException(Throwable e)
Wraps the caused exception in a RuntimeCamelException if its not already such an exception.

Parameters:
e - the caused exception
Returns:
the wrapper exception

wrapCamelExecutionException

public static CamelExecutionException wrapCamelExecutionException(Exchange exchange,
                                                                  Throwable e)
Wraps the caused exception in a CamelExecutionException if its not already such an exception.

Parameters:
e - the caused exception
Returns:
the wrapper exception

normalizeClassName

public static String normalizeClassName(String name)
Cleans the string to pure java identifier so we can use it for loading class names.

Especially from Spring DSL people can have \n \t or other characters that otherwise would result in ClassNotFoundException

Parameters:
name - the class name
Returns:
normalized classname that can be load by a class loader.

createExceptionIterator

public static Iterator<Throwable> createExceptionIterator(Throwable exception)
Creates an iterator to walk the exception from the bottom up (the last caused by going upwards to the root exception).

Parameters:
exception - the exception
Returns:
the iterator

getException

public static <T> T getException(Class<T> type,
                                 Throwable exception)
Retrieves the given exception type from the exception.

Is used to get the caused exception that typically have been wrapped in some sort of Camel wrapper exception

The strategy is to look in the exception hierarchy to find the first given cause that matches the type. Will start from the bottom (the real cause) and walk upwards.

Parameters:
type - the exception type wanted to retrieve
exception - the caused exception
Returns:
the exception found (or null if not found in the exception hierarchy)

getScanner

public static Scanner getScanner(Exchange exchange,
                                 Object value)
Creates a Scanner for scanning the given value.

Parameters:
exchange - the current exchange
value - the value, typically the message IN body
Returns:
the scanner, is newer null

getIdentityHashCode

public static String getIdentityHashCode(Object object)


Copyright © 2007-2010 IONA Open Source Community. All Rights Reserved.