Class BeanAdapter

  • All Implemented Interfaces:
    java.lang.Iterable<java.lang.String>, Collection<java.lang.String>, Dictionary<java.lang.String,​java.lang.Object>, Map<java.lang.String,​java.lang.Object>

    public class BeanAdapter
    extends java.lang.Object
    implements Map<java.lang.String,​java.lang.Object>
    Exposes Java bean properties of an object via the Map interface. A call to Dictionary.get(Object) invokes the getter for the corresponding property, and a call to Map.put(Object, Object) invokes the property's setter.

    Properties may provide multiple setters; the appropriate setter to invoke is determined by the type of the value being set. If the value is null, the return type of the getter method is used.

    Getter methods must be named "getProperty" where "property" is the property name. If there is no "get" method, then an "isProperty" method can also be used. Setter methods (if present) must be named "setProperty".

    Getter and setter methods are checked before straight fields named "property" in order to support proper data encapsulation. And only public and non-static methods and fields can be accessed.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String GET_PREFIX
      Prefix for "getProperty" method names.
      static java.lang.String IS_PREFIX
      Prefix for "isProperty" method names.
      static java.lang.String SET_PREFIX
      Prefix for "setProperty" method names.
    • Constructor Summary

      Constructors 
      Constructor Description
      BeanAdapter​(java.lang.Object beanObject)
      Creates a new bean dictionary.
      BeanAdapter​(java.lang.Object beanObject, boolean ignoreReadOnlyValue)
      Creates a new bean dictionary which can ignore readonly fields (that is, straight fields marked as final or bean properties where there is a "get" method but no corresponding "set" method).
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all entries in the map.
      static <T> T coerce​(java.lang.Object value, java.lang.Class<? extends T> type, java.lang.String key)
      Coerces a value to a given type.
      boolean containsKey​(java.lang.String key)
      Verifies the existence of a property.
      java.lang.Object get​(java.lang.String key)
      Invokes the getter method for the given property.
      java.lang.Object getBean()
      Returns the bean object this dictionary wraps.
      java.util.Comparator<java.lang.String> getComparator()
      Returns the collection's sort order.
      int getCount()
      Returns the number of entries in the map.
      static java.lang.reflect.Field getField​(java.lang.Class<?> beanClass, java.lang.String key)
      Returns the public, non-static fields for a property.
      static java.lang.reflect.Type getGenericType​(java.lang.Class<?> beanClass, java.lang.String key)
      Returns the generic type of a property.
      java.lang.reflect.Type getGenericType​(java.lang.String key)
      Returns the generic type of a property.
      static java.lang.reflect.Method getGetterMethod​(java.lang.Class<?> beanClass, java.lang.String key)
      Returns the getter method for a property.
      ListenerList<MapListener<java.lang.String,​java.lang.Object>> getMapListeners()
      Returns the map listener collection.
      static java.lang.reflect.Method getSetterMethod​(java.lang.Class<?> beanClass, java.lang.String key, java.lang.Class<?> valueType)
      Returns the setter method for a property.
      static java.lang.Class<?> getType​(java.lang.Class<?> beanClass, java.lang.String key)
      Returns the type of a property.
      java.lang.Class<?> getType​(java.lang.String key)
      Returns the type of a property.
      boolean isEmpty()
      Tests the emptiness of the collection.
      static boolean isReadOnly​(java.lang.Class<?> beanClass, java.lang.String key)
      Tests the read-only state of a property.
      boolean isReadOnly​(java.lang.String key)
      Tests the read-only state of a property.
      java.util.Iterator<java.lang.String> iterator()
      Returns an iterator over the bean's properties.
      java.lang.Object put​(java.lang.String key, java.lang.Object value)
      Invokes the setter method for the given property.
      boolean putAll​(Map<java.lang.String,​?> valueMap, boolean ignoreErrors)
      Invokes the setter methods for all the given properties that are present in the map.
      java.lang.Object remove​(java.lang.String key)
      Removes a key/value pair from the map.
      void setComparator​(java.util.Comparator<java.lang.String> comparator)
      Sets the collection's sort order, re-ordering the collection's contents and ensuring that new entries preserve the sort order.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • GET_PREFIX

        public static final java.lang.String GET_PREFIX
        Prefix for "getProperty" method names.
        See Also:
        Constant Field Values
      • IS_PREFIX

        public static final java.lang.String IS_PREFIX
        Prefix for "isProperty" method names.
        See Also:
        Constant Field Values
      • SET_PREFIX

        public static final java.lang.String SET_PREFIX
        Prefix for "setProperty" method names.
        See Also:
        Constant Field Values
    • Constructor Detail

      • BeanAdapter

        public BeanAdapter​(java.lang.Object beanObject)
        Creates a new bean dictionary.
        Parameters:
        beanObject - The bean object to wrap.
      • BeanAdapter

        public BeanAdapter​(java.lang.Object beanObject,
                           boolean ignoreReadOnlyValue)
        Creates a new bean dictionary which can ignore readonly fields (that is, straight fields marked as final or bean properties where there is a "get" method but no corresponding "set" method).
        Parameters:
        beanObject - The bean object to wrap.
        ignoreReadOnlyValue - true if final or non-settable fields should be excluded from the dictionary, false to include all fields.
    • Method Detail

      • getBean

        public java.lang.Object getBean()
        Returns the bean object this dictionary wraps.
        Returns:
        The bean object, or null if no bean has been set.
      • get

        public java.lang.Object get​(java.lang.String key)
        Invokes the getter method for the given property.
        Specified by:
        get in interface Dictionary<java.lang.String,​java.lang.Object>
        Parameters:
        key - The property name.
        Returns:
        The value returned by the method, or null if no such method exists.
      • put

        public java.lang.Object put​(java.lang.String key,
                                    java.lang.Object value)
        Invokes the setter method for the given property. The method signature is determined by the type of the value. If the value is null, the return type of the getter method is used.
        Specified by:
        put in interface Dictionary<java.lang.String,​java.lang.Object>
        Specified by:
        put in interface Map<java.lang.String,​java.lang.Object>
        Parameters:
        key - The property name.
        value - The new property value.
        Returns:
        Returns null, since returning the previous value would require a call to the getter method, which may not be an efficient operation.
        Throws:
        PropertyNotFoundException - If the given property does not exist or is read-only.
        See Also:
        MapListener.valueAdded(Map, Object), MapListener.valueUpdated(Map, Object, Object)
      • putAll

        public boolean putAll​(Map<java.lang.String,​?> valueMap,
                              boolean ignoreErrors)
        Invokes the setter methods for all the given properties that are present in the map. The method signatures are determined by the type of the values. If any value is null, the return type of the getter method is used. There is an option to ignore (that is, not throw) exceptions during the process, but to return status if any exceptions were caught and ignored.
        Parameters:
        valueMap - The map of keys and values to be set.
        ignoreErrors - If true then any PropertyNotFoundException thrown by the put() method will be caught and ignored.
        Returns:
        true if any exceptions were caught, false if not.
      • remove

        @UnsupportedOperation
        public java.lang.Object remove​(java.lang.String key)
        Description copied from interface: Dictionary
        Removes a key/value pair from the map.
        Specified by:
        remove in interface Dictionary<java.lang.String,​java.lang.Object>
        Specified by:
        remove in interface Map<java.lang.String,​java.lang.Object>
        Parameters:
        key - The key whose mapping is to be removed.
        Returns:
        The value that was removed.
        Throws:
        java.lang.UnsupportedOperationException - This operation is not supported.
        See Also:
        MapListener.valueRemoved(Map, Object, Object)
      • clear

        @UnsupportedOperation
        public void clear()
        Description copied from interface: Map
        Removes all entries in the map.
        Specified by:
        clear in interface Collection<java.lang.String>
        Specified by:
        clear in interface Map<java.lang.String,​java.lang.Object>
        Throws:
        java.lang.UnsupportedOperationException - This operation is not supported.
        See Also:
        MapListener.mapCleared(Map)
      • containsKey

        public boolean containsKey​(java.lang.String key)
        Verifies the existence of a property. The property must have a getter method; write-only properties are not supported.
        Specified by:
        containsKey in interface Dictionary<java.lang.String,​java.lang.Object>
        Parameters:
        key - The property name.
        Returns:
        true if the property exists; false, otherwise.
      • isEmpty

        @UnsupportedOperation
        public boolean isEmpty()
        Description copied from interface: Collection
        Tests the emptiness of the collection.
        Specified by:
        isEmpty in interface Collection<java.lang.String>
        Returns:
        true if the collection contains no elements; false, otherwise.
        Throws:
        java.lang.UnsupportedOperationException - This operation is not supported.
      • getCount

        @UnsupportedOperation
        public int getCount()
        Description copied from interface: Map
        Returns the number of entries in the map.
        Specified by:
        getCount in interface Map<java.lang.String,​java.lang.Object>
        Returns:
        Current number of map entries.
        Throws:
        java.lang.UnsupportedOperationException - This operation is not supported.
      • getComparator

        public final java.util.Comparator<java.lang.String> getComparator()
        Description copied from interface: Collection
        Returns the collection's sort order.
        Specified by:
        getComparator in interface Collection<java.lang.String>
        Returns:
        The comparator used to order elements in the collection, or null if the sort order is undefined.
        See Also:
        Collection.setComparator(Comparator)
      • setComparator

        @UnsupportedOperation
        public void setComparator​(java.util.Comparator<java.lang.String> comparator)
        Description copied from interface: Collection
        Sets the collection's sort order, re-ordering the collection's contents and ensuring that new entries preserve the sort order.

        Calling this method more than once with the same comparator will re-sort the collection.

        Specified by:
        setComparator in interface Collection<java.lang.String>
        Specified by:
        setComparator in interface Map<java.lang.String,​java.lang.Object>
        Parameters:
        comparator - The comparator used to order elements in the collection, or null if the collection is unsorted.
        Throws:
        java.lang.UnsupportedOperationException - This operation is not supported.
        See Also:
        MapListener.comparatorChanged(Map, Comparator)
      • isReadOnly

        public boolean isReadOnly​(java.lang.String key)
        Tests the read-only state of a property.
        Parameters:
        key - The property name.
        Returns:
        true if the property is read-only; false, otherwise.
      • getType

        public java.lang.Class<?> getType​(java.lang.String key)
        Returns the type of a property.
        Parameters:
        key - The property name.
        Returns:
        The real class type of this property.
        See Also:
        getType(Class, String)
      • getGenericType

        public java.lang.reflect.Type getGenericType​(java.lang.String key)
        Returns the generic type of a property.
        Parameters:
        key - The property name.
        Returns:
        The generic type of this property.
        See Also:
        getGenericType(Class, String)
      • iterator

        public java.util.Iterator<java.lang.String> iterator()
        Returns an iterator over the bean's properties.
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.String>
        Returns:
        A property iterator for this bean.
      • getMapListeners

        public final ListenerList<MapListener<java.lang.String,​java.lang.Object>> getMapListeners()
        Description copied from interface: Map
        Returns the map listener collection.
        Specified by:
        getMapListeners in interface Map<java.lang.String,​java.lang.Object>
        Returns:
        Current list of listeners on this map.
      • isReadOnly

        public static boolean isReadOnly​(java.lang.Class<?> beanClass,
                                         java.lang.String key)
        Tests the read-only state of a property. Note that if no such property exists, this method will return true (it will not throw an exception).
        Parameters:
        beanClass - The bean class.
        key - The property name.
        Returns:
        true if the property is read-only; false, otherwise.
      • getType

        public static java.lang.Class<?> getType​(java.lang.Class<?> beanClass,
                                                 java.lang.String key)
        Returns the type of a property.
        Parameters:
        beanClass - The bean class.
        key - The property name.
        Returns:
        The type of the property, or null if no such bean property exists.
      • getGenericType

        public static java.lang.reflect.Type getGenericType​(java.lang.Class<?> beanClass,
                                                            java.lang.String key)
        Returns the generic type of a property.
        Parameters:
        beanClass - The bean class.
        key - The property name.
        Returns:
        The generic type of the property, or null if no such bean property exists. If the type is a generic, an instance of ParameterizedType will be returned. Otherwise, an instance of Class will be returned.
      • getField

        public static java.lang.reflect.Field getField​(java.lang.Class<?> beanClass,
                                                       java.lang.String key)
        Returns the public, non-static fields for a property. Note that fields will only be consulted for bean properties after bean methods.
        Parameters:
        beanClass - The bean class.
        key - The property name.
        Returns:
        The field, or null if the field does not exist, or is non-public or static.
      • getGetterMethod

        public static java.lang.reflect.Method getGetterMethod​(java.lang.Class<?> beanClass,
                                                               java.lang.String key)
        Returns the getter method for a property.
        Parameters:
        beanClass - The bean class.
        key - The property name.
        Returns:
        The getter method, or null if the method does not exist.
      • getSetterMethod

        public static java.lang.reflect.Method getSetterMethod​(java.lang.Class<?> beanClass,
                                                               java.lang.String key,
                                                               java.lang.Class<?> valueType)
        Returns the setter method for a property.
        Parameters:
        beanClass - The bean class.
        key - The property name.
        valueType - The type of the property.
        Returns:
        The setter method, or null if the method does not exist.
      • coerce

        public static <T> T coerce​(java.lang.Object value,
                                   java.lang.Class<? extends T> type,
                                   java.lang.String key)
        Coerces a value to a given type.
        Type Parameters:
        T - The parametric type to coerce to.
        Parameters:
        value - The object to be coerced.
        type - The type to coerce it to.
        key - The property name in question.
        Returns:
        The coerced value.
        Throws:
        java.lang.IllegalArgumentException - for all the possible other exceptions.