public final class ObjectHelper extends Object
Modifier and Type | Method and Description |
---|---|
static String |
after(String text,
String after)
Returns the string after the given token
|
static <T> Optional<T> |
after(String text,
String after,
Function<String,T> mapper)
Returns an object after the given token
|
static int |
arrayLength(Object[] pojo) |
static String |
asString(Object[] objects)
Turns the given object arrays into a meaningful string
|
static String |
before(String text,
String before)
Returns the string before the given token
|
static <T> Optional<T> |
before(String text,
String before,
Function<String,T> mapper)
Returns an object before the given token
|
static String |
between(String text,
String after,
String before)
Returns the string between the given tokens
|
static <T> Optional<T> |
between(String text,
String after,
String before,
Function<String,T> mapper)
Returns an object between the given token
|
static String |
betweenOuterPair(String text,
char before,
char after)
Returns the string between the most outer pair of tokens
The number of token pairs must be evenly, eg there must be same number of before and after tokens, otherwise null is returned
This implementation skips matching when the text is either single or double quoted.
|
static <T> Optional<T> |
betweenOuterPair(String text,
char before,
char after,
Function<String,T> mapper)
Returns an object between the most outer pair of tokens
|
static Object |
callWithTCCL(Callable<?> call,
ClassLoader classloader)
Calling the Callable with the setting of TCCL with a given classloader.
|
static Object |
callWithTCCL(Callable<?> call,
Exchange exchange)
Calling the Callable with the setting of TCCL with the camel context application classloader.
|
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.slf4j.Logger log)
Deprecated.
will be removed in Camel 3.0. Instead use
IOHelper.close(java.io.Closeable, String, org.slf4j.Logger) instead |
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 Iterable<Throwable> |
createExceptionIterable(Throwable exception)
Creates an Iterable to walk the exception from the bottom up
(the last caused by going upwards to the root exception).
|
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 Iterable<Object> |
createIterable(Object value)
Creates an iterable over the value if the value is a collection, an
Object[], a String with values separated by comma,
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 Iterable<Object> |
createIterable(Object value,
String delimiter)
Creates an iterable over the value if the value is a collection, an
Object[], a String with values separated by the given delimiter,
or a primitive type array; otherwise to simplify the caller's
code, we just create a singleton collection iterator over a single value
This method does not allow empty values
|
static Iterable<Object> |
createIterable(Object value,
String delimiter,
boolean allowEmptyValues)
Creates an iterable over the value if the value is a collection, an
Object[], a String with values separated by the given delimiter,
or a primitive type array; otherwise to simplify the caller's
code, we just create a singleton collection iterator over a single value
|
static Iterable<Object> |
createIterable(Object value,
String delimiter,
boolean allowEmptyValues,
boolean pattern)
Creates an iterable over the value if the value is a collection, an
Object[], a String with values separated by the given delimiter,
or a primitive type array; otherwise to simplify the caller's
code, we just create a singleton collection iterator over a single value
|
static Iterator<Object> |
createIterator(Object value)
Creates an iterator over the value if the value is a collection, an
Object[], a String with values separated by comma,
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[], a String with values separated by the given delimiter,
or a primitive type array; otherwise to simplify the caller's
code, we just create a singleton collection iterator over a single value
This method does not allow empty values
|
static Iterator<Object> |
createIterator(Object value,
String delimiter,
boolean allowEmptyValues)
Creates an iterator over the value if the value is a collection, an
Object[], a String with values separated by the given delimiter,
or a primitive type array; otherwise to simplify the caller's
code, we just create a singleton collection iterator over a single value
|
static Iterator<Object> |
createIterator(Object value,
String delimiter,
boolean allowEmptyValues,
boolean pattern)
Creates an iterator over the value if the value is a collection, an
Object[], a String with values separated by the given delimiter,
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 |
equal(Object a,
Object b,
boolean ignoreCase)
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 <A extends Annotation> |
getAnnotation(Object instance,
Class<A> type)
Gets the annotation from the given instance.
|
static Properties |
getCamelPropertiesWithPrefix(String prefix,
CamelContext camelContext)
A helper method to access a camel context properties with a prefix
|
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 boolean |
hasDefaultPublicNoArgConstructor(Class<?> type)
Does the given class have a default public no-arg constructor.
|
static <T> void |
ifNotEmpty(T value,
Consumer<T> consumer)
Tests whether the value is not null, an empty string or an empty collection.
|
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 |
isNaN(Object value)
Is the given value a numeric NaN type
|
static boolean |
isNotEmpty(Object value)
Tests whether the value is not null, an empty string or an empty collection.
|
static boolean |
isOverridingMethod(Method source,
Method target)
Tests whether the target method overrides the source method.
|
static boolean |
isOverridingMethod(Method source,
Method target,
boolean exact)
Tests whether the target method overrides the source method.
|
static boolean |
isPrimitiveArrayType(Class<?> clazz)
Returns if the given
clazz type is a Java primitive array type. |
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 InputStream |
loadResourceAsStream(String name,
ClassLoader loader)
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 URL |
loadResourceAsURL(String name,
ClassLoader loader)
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 Enumeration<URL> |
loadResourcesAsURL(String packageName)
Attempts to load the given resources from the given package name using the thread context
class loader or the class loader used to load this class
|
static Enumeration<URL> |
loadResourcesAsURL(String packageName,
ClassLoader loader)
Attempts to load the given resources from the given package name using the thread context
class loader or the class loader used to load this class
|
static Class<?> |
loadSimpleType(String name)
Load a simple type
|
static String |
lookupConstantFieldValue(Class<?> clazz,
String name)
Lookup the constant field on the given class with the given name
|
static boolean |
matches(List<?> 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 a pure Java identifier so we can use it for loading class names.
|
static String |
notEmpty(String value,
String name)
Asserts whether the string is not empty.
|
static String |
notEmpty(String value,
String name,
Object on)
Asserts whether the string is not empty.
|
static <T> T |
notNull(T value,
String name)
Asserts whether the value is not null
|
static <T> T |
notNull(T 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 coercion 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 coercion to coerce
types between the left and right values.
|
static boolean |
typeCoerceEquals(TypeConverter converter,
Object leftValue,
Object rightValue,
boolean ignoreCase)
A helper method for comparing objects for equality in which it uses type coercion 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 inequality in which it uses type coercion 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. |
public static boolean typeCoerceEquals(TypeConverter converter, Object leftValue, Object rightValue)
public static boolean typeCoerceEquals(TypeConverter converter, Object leftValue, Object rightValue, boolean ignoreCase)
public static boolean typeCoerceNotEquals(TypeConverter converter, Object leftValue, Object rightValue)
public static int typeCoerceCompare(TypeConverter converter, Object leftValue, Object rightValue)
public static boolean equal(Object a, Object b)
public static boolean equal(Object a, Object b, boolean ignoreCase)
public static boolean equalByteArray(byte[] a, byte[] b)
public static boolean isEqualToAny(Object object, Object... values)
public static int compare(Object a, Object b)
public static int compare(Object a, Object b, boolean ignoreCase)
a
- the first objectb
- the second objectignoreCase
- ignore case for string comparisonpublic static <T> T notNull(T value, String name)
value
- the value to testname
- the key that resolved the valuevalue
as isIllegalArgumentException
- is thrown if assertion failspublic static <T> T notNull(T value, String name, Object on)
value
- the value to teston
- additional description to indicate where this problem occurred (appended as toString())name
- the key that resolved the valuevalue
as isIllegalArgumentException
- is thrown if assertion failspublic static String notEmpty(String value, String name)
value
- the string to testname
- the key that resolved the valuevalue
as isIllegalArgumentException
- is thrown if assertion failspublic static String notEmpty(String value, String name, Object on)
value
- the string to teston
- additional description to indicate where this problem occurred (appended as toString())name
- the key that resolved the valuevalue
as isIllegalArgumentException
- is thrown if assertion failspublic static boolean isEmpty(Object value)
value
- the value, if its a String it will be tested for text length as wellpublic static boolean isNotEmpty(Object value)
value
- the value, if its a String it will be tested for text length as wellpublic static <T> void ifNotEmpty(T value, Consumer<T> consumer)
value
- the value, if its a String it will be tested for text length as wellconsumer
- the consumer, the operation to be executed against value if not emptypublic static String[] splitOnCharacter(String value, String needle, int count)
public static String removeStartingCharacters(String text, char ch)
text
- the stringch
- the initial characters to removepublic static String capitalize(String text)
public static String after(String text, String after)
text
- the textafter
- the tokenpublic static <T> Optional<T> after(String text, String after, Function<String,T> mapper)
text
- the textafter
- the tokenmapper
- a mapping function to convert the string after the token to type Tpublic static String before(String text, String before)
text
- the textbefore
- the tokenpublic static <T> Optional<T> before(String text, String before, Function<String,T> mapper)
text
- the textbefore
- the tokenmapper
- a mapping function to convert the string before the token to type Tpublic static String between(String text, String after, String before)
text
- the textafter
- the before tokenbefore
- the after tokenpublic static <T> Optional<T> between(String text, String after, String before, Function<String,T> mapper)
text
- the textafter
- the before tokenbefore
- the after tokenmapper
- a mapping function to convert the string between the token to type Tpublic static String betweenOuterPair(String text, char before, char after)
text
- the textafter
- the before tokenbefore
- the after tokenpublic static <T> Optional<T> betweenOuterPair(String text, char before, char after, Function<String,T> mapper)
text
- the textafter
- the before tokenbefore
- the after tokenmapper
- a mapping function to convert the string between the most outer pair of tokens to type Tpublic static boolean contains(Object collectionOrArray, Object value)
public static Iterable<Object> createIterable(Object value)
value
- the valuepublic static Iterable<Object> createIterable(Object value, String delimiter)
value
- the valuedelimiter
- delimiter for separating String valuespublic static Iterator<Object> createIterator(Object value)
value
- the valuepublic static Iterator<Object> createIterator(Object value, String delimiter)
value
- the valuedelimiter
- delimiter for separating String valuespublic static Iterator<Object> createIterator(Object value, String delimiter, boolean allowEmptyValues)
Iterator
iterates
over the corresponding Java primitive wrapper objects of the given elements
inside the value
array. That's we get an autoboxing of the primitive
types here for free as it's also the case in Java language itself.value
- the valuedelimiter
- delimiter for separating String valuesallowEmptyValues
- whether to allow empty valuespublic static Iterator<Object> createIterator(Object value, String delimiter, boolean allowEmptyValues, boolean pattern)
Iterator
iterates
over the corresponding Java primitive wrapper objects of the given elements
inside the value
array. That's we get an autoboxing of the primitive
types here for free as it's also the case in Java language itself.value
- the valuedelimiter
- delimiter for separating String valuesallowEmptyValues
- whether to allow empty valuespattern
- whether the delimiter is a patternpublic static Iterable<Object> createIterable(Object value, String delimiter, boolean allowEmptyValues)
Iterable
iterates
over the corresponding Java primitive wrapper objects of the given elements
inside the value
array. That's we get an autoboxing of the primitive
types here for free as it's also the case in Java language itself.value
- the valuedelimiter
- delimiter for separating String valuesallowEmptyValues
- whether to allow empty valuesIterable
public static Iterable<Object> createIterable(Object value, String delimiter, boolean allowEmptyValues, boolean pattern)
Iterable
iterates
over the corresponding Java primitive wrapper objects of the given elements
inside the value
array. That's we get an autoboxing of the primitive
types here for free as it's also the case in Java language itself.value
- the valuedelimiter
- delimiter for separating String valuesallowEmptyValues
- whether to allow empty valuespattern
- whether the delimiter is a patternIterable
public static boolean matches(List<?> list)
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 emptypublic static String getSystemProperty(String name, String defaultValue)
name
- the name of the system property requireddefaultValue
- the default value to use if the property is not
available or a security exception prevents accesspublic static boolean getSystemProperty(String name, Boolean defaultValue)
name
- the name of the system property requireddefaultValue
- the default value to use if the property is not
available or a security exception prevents accesspublic static Properties getCamelPropertiesWithPrefix(String prefix, CamelContext camelContext)
prefix
- the prefixcamelContext
- the camel contextpublic static String name(Class<?> type)
public static String classCanonicalName(Object value)
public static Class<?> loadClass(String name)
name
- the name of the class to loadpublic static Class<?> loadClass(String name, ClassLoader loader)
name
- the name of the class to loadloader
- the class loader to use after the thread context class loaderpublic static Class<?> loadClass(String name, ClassLoader loader, boolean needToWarn)
name
- the name of the class to loadloader
- the class loader to use after the thread context class loaderneedToWarn
- when true logs a warning when a class with the given name could not be loadedpublic static Class<?> loadSimpleType(String name)
name
- the name of the class to loadpublic static InputStream loadResourceAsStream(String name)
name
- the name of the resource to loadpublic static InputStream loadResourceAsStream(String name, ClassLoader loader)
name
- the name of the resource to loadloader
- optional classloader to attempt firstpublic static URL loadResourceAsURL(String name)
name
- the name of the resource to loadpublic static URL loadResourceAsURL(String name, ClassLoader loader)
name
- the name of the resource to loadloader
- optional classloader to attempt firstpublic static Enumeration<URL> loadResourcesAsURL(String packageName)
packageName
- the name of the package to load its resourcespublic static Enumeration<URL> loadResourcesAsURL(String packageName, ClassLoader loader)
packageName
- the name of the package to load its resourcesloader
- optional classloader to attempt firstpublic static Object invokeMethod(Method method, Object instance, Object... parameters)
RuntimeCamelException
instancesmethod
- the method to invokeinstance
- the object instance (or null for static methods)parameters
- the parameters to the methodpublic static boolean isOverridingMethod(Method source, Method target)
source
- the source methodtarget
- the target methodpublic static boolean isOverridingMethod(Method source, Method target, boolean exact)
source
- the source methodtarget
- the target methodexact
- true if the override must be exact same types, false if the types should be assignablepublic static List<Method> findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType)
type
- the type to reflect onannotationType
- the annotation typepublic static List<Method> findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType, boolean checkMetaAnnotations)
type
- the type to reflect onannotationType
- the annotation typecheckMetaAnnotations
- check for meta annotationspublic static boolean hasAnnotation(AnnotatedElement elem, Class<? extends Annotation> annotationType, boolean checkMetaAnnotations)
elem
- the Class or Method to reflect onannotationType
- the annotation typecheckMetaAnnotations
- check for meta annotationspublic static String asString(Object[] objects)
objects
- an array of objects or nullpublic static boolean isAssignableFrom(Class<?> a, Class<?> b)
Class.isAssignableFrom(Class)
method but which also includes
coercion between primitive types to deal with Java 5 primitive type
wrappingpublic static boolean isPrimitiveArrayType(Class<?> clazz)
clazz
type is a Java primitive array type.clazz
- the Java type to be checkedtrue
if the given type is a Java primitive array typepublic static int arrayLength(Object[] pojo)
public static Class<?> convertPrimitiveTypeToWrapperType(Class<?> type)
Integer
public static String getDefaultCharacterSet()
public static String getPropertyName(Method method)
public static boolean hasAnnotation(Annotation[] annotations, Class<?> type)
public static <A extends Annotation> A getAnnotation(Object instance, Class<A> type)
instance
- the instancetype
- the annotation@Deprecated public static void close(Closeable closeable, String name, org.slf4j.Logger log)
IOHelper.close(java.io.Closeable, String, org.slf4j.Logger)
insteadcloseable
- the object to closename
- the name of the resourcelog
- the log to use when reporting closure warningspublic static <T> T cast(Class<T> toType, Object value)
public static <T> T newInstance(Class<T> type)
public static <T> T newInstance(Class<?> actualType, Class<T> expectedType)
public static boolean hasDefaultPublicNoArgConstructor(Class<?> type)
public static boolean isJavaIdentifier(String name)
public static Object type(Object bean)
public static boolean evaluateValuePredicate(Object value)
public static RuntimeCamelException wrapRuntimeCamelException(Throwable e)
RuntimeCamelException
if its not
already such an exception.e
- the caused exceptionpublic static CamelExecutionException wrapCamelExecutionException(Exchange exchange, Throwable e)
CamelExecutionException
if its not
already such an exception.e
- the caused exceptionpublic static String normalizeClassName(String name)
name
- the class namepublic static Iterable<Throwable> createExceptionIterable(Throwable exception)
exception
- the exceptionIterable
public static Iterator<Throwable> createExceptionIterator(Throwable exception)
exception
- the exceptionIterator
public static <T> T getException(Class<T> type, Throwable exception)
type
- the exception type wanted to retrieveexception
- the caused exceptionpublic static Scanner getScanner(Exchange exchange, Object value)
Scanner
for scanning the given value.exchange
- the current exchangevalue
- the value, typically the message IN bodypublic static String getIdentityHashCode(Object object)
public static String lookupConstantFieldValue(Class<?> clazz, String name)
clazz
- the classname
- the name of the field to lookuppublic static boolean isNaN(Object value)
value
- the valueFloat.NaN
or Double.NaN
.public static Object callWithTCCL(Callable<?> call, Exchange exchange) throws Exception
call
- the Callable instanceexchange
- the exchangeException
public static Object callWithTCCL(Callable<?> call, ClassLoader classloader) throws Exception
call
- the Callable instanceclassloader
- the class loaderException
Apache Camel