Class ValueUtils


  • public class ValueUtils
    extends java.lang.Object
    Collection and property access utilities.
    Version:
    $Revision: 670728 $ $Date: 2008-06-23 15:12:44 -0500 (Mon, 23 Jun 2008) $
    Author:
    Dmitri Plotnikov
    • Constructor Summary

      Constructors 
      Constructor Description
      ValueUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.Object convert​(java.lang.Object value, java.lang.Class type)
      Convert value to type.
      static java.lang.Object expandCollection​(java.lang.Object collection, int size)
      Grows the collection if necessary to the specified size.
      static java.lang.reflect.Method getAccessibleMethod​(java.lang.reflect.Method method)
      Return an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.
      private static java.lang.reflect.Method getAccessibleMethodFromInterfaceNest​(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameterTypes)
      Return an accessible method (that is, one that can be invoked via reflection) that implements the specified method, by scanning through all implemented interfaces and subinterfaces.
      static int getCollectionHint​(java.lang.Class clazz)
      Returns 1 if the type is a collection, -1 if it is definitely not and 0 if it may be a collection in some cases.
      static DynamicPropertyHandler getDynamicPropertyHandler​(java.lang.Class clazz)
      Returns a shared instance of the dynamic property handler class returned by getDynamicPropertyHandlerClass().
      static int getIndexedPropertyLength​(java.lang.Object object, java.beans.IndexedPropertyDescriptor pd)
      If there is a regular non-indexed read method for this property, uses this method to obtain the collection and then returns its length.
      static int getLength​(java.lang.Object collection)
      Returns the length of the supplied collection.
      static java.lang.Object getValue​(java.lang.Object object)
      If the parameter is a container, opens the container and return the contents.
      static java.lang.Object getValue​(java.lang.Object collection, int index)
      Returns the index'th element of the supplied collection.
      static java.lang.Object getValue​(java.lang.Object bean, java.beans.PropertyDescriptor propertyDescriptor)
      Returns the value of the bean's property represented by the supplied property descriptor.
      static java.lang.Object getValue​(java.lang.Object bean, java.beans.PropertyDescriptor propertyDescriptor, int index)
      Returns the index'th element of the bean's property represented by the supplied property descriptor.
      static boolean isCollection​(java.lang.Object value)
      Returns true if the object is an array or a Collection.
      static java.util.Iterator iterate​(java.lang.Object collection)
      Returns an iterator for the supplied collection.
      static java.lang.Object remove​(java.lang.Object collection, int index)
      Remove the index'th element from the supplied collection.
      static void setValue​(java.lang.Object collection, int index, java.lang.Object value)
      Modifies the index'th element of the supplied collection.
      static void setValue​(java.lang.Object bean, java.beans.PropertyDescriptor propertyDescriptor, int index, java.lang.Object value)
      Modifies the index'th element of the bean's property represented by the supplied property descriptor.
      static void setValue​(java.lang.Object bean, java.beans.PropertyDescriptor propertyDescriptor, java.lang.Object value)
      Modifies the value of the bean's property represented by the supplied property descriptor.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • dynamicPropertyHandlerMap

        private static java.util.Map dynamicPropertyHandlerMap
      • UNKNOWN_LENGTH_MAX_COUNT

        private static final int UNKNOWN_LENGTH_MAX_COUNT
        See Also:
        Constant Field Values
    • Constructor Detail

      • ValueUtils

        public ValueUtils()
    • Method Detail

      • isCollection

        public static boolean isCollection​(java.lang.Object value)
        Returns true if the object is an array or a Collection.
        Parameters:
        value - to test
        Returns:
        boolean
      • getCollectionHint

        public static int getCollectionHint​(java.lang.Class clazz)
        Returns 1 if the type is a collection, -1 if it is definitely not and 0 if it may be a collection in some cases.
        Parameters:
        clazz - to test
        Returns:
        int
      • getIndexedPropertyLength

        public static int getIndexedPropertyLength​(java.lang.Object object,
                                                   java.beans.IndexedPropertyDescriptor pd)
        If there is a regular non-indexed read method for this property, uses this method to obtain the collection and then returns its length. Otherwise, attempts to guess the length of the collection by calling the indexed get method repeatedly. The method is supposed to throw an exception if the index is out of bounds.
        Parameters:
        object - collection
        pd - IndexedPropertyDescriptor
        Returns:
        int
      • getLength

        public static int getLength​(java.lang.Object collection)
        Returns the length of the supplied collection. If the supplied object is not a collection, returns 1. If collection is null, returns 0.
        Parameters:
        collection - to check
        Returns:
        int
      • iterate

        public static java.util.Iterator iterate​(java.lang.Object collection)
        Returns an iterator for the supplied collection. If the argument is null, returns an empty iterator. If the argument is not a collection, returns an iterator that produces just that one object.
        Parameters:
        collection - to iterate
        Returns:
        Iterator
      • expandCollection

        public static java.lang.Object expandCollection​(java.lang.Object collection,
                                                        int size)
        Grows the collection if necessary to the specified size. Returns the new, expanded collection.
        Parameters:
        collection - to expand
        size - desired size
        Returns:
        collection or array
      • remove

        public static java.lang.Object remove​(java.lang.Object collection,
                                              int index)
        Remove the index'th element from the supplied collection.
        Parameters:
        collection - to edit
        index - int
        Returns:
        the resulting collection
      • getValue

        public static java.lang.Object getValue​(java.lang.Object collection,
                                                int index)
        Returns the index'th element of the supplied collection.
        Parameters:
        collection - to read
        index - int
        Returns:
        collection[index]
      • setValue

        public static void setValue​(java.lang.Object collection,
                                    int index,
                                    java.lang.Object value)
        Modifies the index'th element of the supplied collection. Converts the value to the required type if necessary.
        Parameters:
        collection - to edit
        index - to replace
        value - new value
      • getValue

        public static java.lang.Object getValue​(java.lang.Object bean,
                                                java.beans.PropertyDescriptor propertyDescriptor)
        Returns the value of the bean's property represented by the supplied property descriptor.
        Parameters:
        bean - to read
        propertyDescriptor - indicating what to read
        Returns:
        Object value
      • setValue

        public static void setValue​(java.lang.Object bean,
                                    java.beans.PropertyDescriptor propertyDescriptor,
                                    java.lang.Object value)
        Modifies the value of the bean's property represented by the supplied property descriptor.
        Parameters:
        bean - to read
        propertyDescriptor - indicating what to read
        value - to set
      • convert

        private static java.lang.Object convert​(java.lang.Object value,
                                                java.lang.Class type)
        Convert value to type.
        Parameters:
        value - Object
        type - destination
        Returns:
        conversion result
      • getValue

        public static java.lang.Object getValue​(java.lang.Object bean,
                                                java.beans.PropertyDescriptor propertyDescriptor,
                                                int index)
        Returns the index'th element of the bean's property represented by the supplied property descriptor.
        Parameters:
        bean - to read
        propertyDescriptor - indicating what to read
        index - int
        Returns:
        Object
      • setValue

        public static void setValue​(java.lang.Object bean,
                                    java.beans.PropertyDescriptor propertyDescriptor,
                                    int index,
                                    java.lang.Object value)
        Modifies the index'th element of the bean's property represented by the supplied property descriptor. Converts the value to the required type if necessary.
        Parameters:
        bean - to edit
        propertyDescriptor - indicating what to set
        index - int
        value - to set
      • getValue

        public static java.lang.Object getValue​(java.lang.Object object)
        If the parameter is a container, opens the container and return the contents. The method is recursive.
        Parameters:
        object - to read
        Returns:
        Object
      • getDynamicPropertyHandler

        public static DynamicPropertyHandler getDynamicPropertyHandler​(java.lang.Class clazz)
        Returns a shared instance of the dynamic property handler class returned by getDynamicPropertyHandlerClass().
        Parameters:
        clazz - to handle
        Returns:
        DynamicPropertyHandler
      • getAccessibleMethod

        public static java.lang.reflect.Method getAccessibleMethod​(java.lang.reflect.Method method)
        Return an accessible method (that is, one that can be invoked via reflection) that implements the specified Method. If no such method can be found, return null.
        Parameters:
        method - The method that we wish to call
        Returns:
        Method
      • getAccessibleMethodFromInterfaceNest

        private static java.lang.reflect.Method getAccessibleMethodFromInterfaceNest​(java.lang.Class clazz,
                                                                                     java.lang.String methodName,
                                                                                     java.lang.Class[] parameterTypes)
        Return an accessible method (that is, one that can be invoked via reflection) that implements the specified method, by scanning through all implemented interfaces and subinterfaces. If no such Method can be found, return null.
        Parameters:
        clazz - Parent class for the interfaces to be checked
        methodName - Method name of the method we wish to call
        parameterTypes - The parameter type signatures
        Returns:
        Method