Class ObjectHelper


  • public final class ObjectHelper
    extends Object
    A number of useful helper methods for working with Objects
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Object callWithTCCL​(Callable<?> call, ClassLoader classloader)
      Calling the Callable with the setting of TCCL with a given classloader.
      static Object callWithTCCL​(Callable<?> call, org.apache.camel.Exchange exchange)
      Calling the Callable with the setting of TCCL with the camel context application classloader.
      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 boolean containsIgnoreCase​(Object collectionOrArray, Object value)
      Returns true if the collection contains the specified value by considering case insensitivity
      static Iterable<?> 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
      static Iterable<?> 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
      static Iterable<?> 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<?> 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 Iterable<String> createIterable​(String value)  
      static Iterable<String> createIterable​(String value, String delimiter)  
      static Iterable<String> createIterable​(String value, String delimiter, boolean allowEmptyValues)  
      static Iterable<String> createIterable​(String value, String delimiter, boolean allowEmptyValues, boolean pattern)  
      static Iterator<?> createIterator​(Object value)
      Creates an iterator over the value if the value is a Stream, 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
      static Iterator<?> createIterator​(Object value, String delimiter)
      Creates an iterator over the value if the value is a Stream, 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<?> createIterator​(Object value, String delimiter, boolean allowEmptyValues)
      Creates an iterator over the value if the value is a Stream, 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<?> createIterator​(Object value, String delimiter, boolean allowEmptyValues, boolean pattern)
      Creates an iterator over the value if the value is a Stream, 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 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 Object invokeMethodSafe​(Method method, Object instance, Object... parameters)
      A helper method to invoke a method via reflection in a safe way by allowing to invoke methods that are not accessible by default and wrap any exceptions as RuntimeCamelException instances
      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 int typeCoerceCompare​(org.apache.camel.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​(org.apache.camel.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​(org.apache.camel.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​(org.apache.camel.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.
    • Method Detail

      • typeCoerceEquals

        public static boolean typeCoerceEquals​(org.apache.camel.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. This allows you test for equality for example with a String and Integer type as Camel will be able to coerce the types.
      • typeCoerceEquals

        public static boolean typeCoerceEquals​(org.apache.camel.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. This allows you test for equality for example with a String and Integer type as Camel will be able to coerce the types.
      • typeCoerceNotEquals

        public static boolean typeCoerceNotEquals​(org.apache.camel.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. This allows you test for inequality for example with a String and Integer type as Camel will be able to coerce the types.
      • typeCoerceCompare

        public static int typeCoerceCompare​(org.apache.camel.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. This allows you test for ordering for example with a String and Integer type as Camel will be able to coerce the types.
      • 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
      • invokeMethodSafe

        public static Object invokeMethodSafe​(Method method,
                                              Object instance,
                                              Object... parameters)
                                       throws InvocationTargetException,
                                              IllegalAccessException
        A helper method to invoke a method via reflection in a safe way by allowing to invoke methods that are not accessible by default 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
        Throws:
        InvocationTargetException
        IllegalAccessException
      • 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.
      • 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
      • callWithTCCL

        public static Object callWithTCCL​(Callable<?> call,
                                          org.apache.camel.Exchange exchange)
                                   throws Exception
        Calling the Callable with the setting of TCCL with the camel context application classloader.
        Parameters:
        call - the Callable instance
        exchange - the exchange
        Returns:
        the result of Callable return
        Throws:
        Exception
      • callWithTCCL

        public static Object callWithTCCL​(Callable<?> call,
                                          ClassLoader classloader)
                                   throws Exception
        Calling the Callable with the setting of TCCL with a given classloader.
        Parameters:
        call - the Callable instance
        classloader - the class loader
        Returns:
        the result of Callable return
        Throws:
        Exception
      • createIterable

        public static Iterable<?> 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. This method does not allow empty values

        Parameters:
        value - the value
        Returns:
        the iterable
      • createIterable

        public static Iterable<?> 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

        Parameters:
        value - the value
        delimiter - delimiter for separating String values
        Returns:
        the iterable
      • createIterable

        public static Iterable<String> createIterable​(String value,
                                                      String delimiter,
                                                      boolean allowEmptyValues,
                                                      boolean pattern)
      • createIterator

        public static Iterator<?> createIterator​(Object value)
        Creates an iterator over the value if the value is a Stream, 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. This method does not allow empty values

        Parameters:
        value - the value
        Returns:
        the iterator
      • createIterator

        public static Iterator<?> createIterator​(Object value,
                                                 String delimiter)
        Creates an iterator over the value if the value is a Stream, 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

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

        public static Iterator<?> createIterator​(Object value,
                                                 String delimiter,
                                                 boolean allowEmptyValues)
        Creates an iterator over the value if the value is a Stream, 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

        In case of primitive type arrays the returned 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.
        Parameters:
        value - the value
        delimiter - delimiter for separating String values
        allowEmptyValues - whether to allow empty values
        Returns:
        the iterator
      • createIterator

        public static Iterator<?> createIterator​(Object value,
                                                 String delimiter,
                                                 boolean allowEmptyValues,
                                                 boolean pattern)
        Creates an iterator over the value if the value is a Stream, 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

        In case of primitive type arrays the returned 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.
        Parameters:
        value - the value
        delimiter - delimiter for separating String values
        allowEmptyValues - whether to allow empty values
        pattern - whether the delimiter is a pattern
        Returns:
        the iterator
      • createIterable

        public static Iterable<?> 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

        In case of primitive type arrays the returned 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.
        Parameters:
        value - the value
        delimiter - delimiter for separating String values
        allowEmptyValues - whether to allow empty values
        Returns:
        the iterable
        See Also:
        Iterable
      • createIterable

        public static Iterable<?> 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

        In case of primitive type arrays the returned 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.
        Parameters:
        value - the value
        delimiter - delimiter for separating String values
        allowEmptyValues - whether to allow empty values
        pattern - whether the delimiter is a pattern
        Returns:
        the iterable
        See Also:
        Iterable
      • contains

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

        public static boolean containsIgnoreCase​(Object collectionOrArray,
                                                 Object value)
        Returns true if the collection contains the specified value by considering case insensitivity