Class ContextBase

java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap
org.apache.commons.chain.impl.ContextBase
All Implemented Interfaces:
Serializable, Cloneable, Map, Context
Direct Known Subclasses:
WebContext

public class ContextBase extends HashMap implements Context

Convenience base class for Context implementations.

In addition to the minimal functionality required by the Context interface, this class implements the recommended support for Attribute-Property Transparency. This is implemented by analyzing the available JavaBeans properties of this class (or its subclass), exposes them as key-value pairs in the Map, with the key being the name of the property itself.

IMPLEMENTATION NOTE - Because empty is a read-only property defined by the Map interface, it may not be utilized as an attribute key or property name.

Version:
$Revision: 499247 $ $Date: 2007-01-24 04:09:44 +0000 (Wed, 24 Jan 2007) $
Author:
Craig R. McClanahan
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private class 
    Private implementation of Set that implements the semantics required for the value returned by entrySet().
    private class 
    Private implementation of Iterator for the Set returned by entrySet().
    private class 
    Private implementation of Map.Entry for each item in EntrySetImpl.
    private class 
    Private implementation of Collection that implements the semantics required for the value returned by values().
    private class 
    Private implementation of Iterator for the Collection returned by values().

    Nested classes/interfaces inherited from class java.util.AbstractMap

    AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>

    Nested classes/interfaces inherited from interface java.util.Map

    Map.Entry<K extends Object,V extends Object>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private Map
    The PropertyDescriptors for all JavaBeans properties of this Context implementation class, keyed by property name.
    The same PropertyDescriptors as an array.
    private static Object
    Distinguished singleton value that is stored in the map for each key that is actually a property.
    private static Object[]
    Zero-length array of parameter values for calling property getters.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default, no argument constructor.
    Initialize the contents of this Context by copying the values from the specified Map.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Override the default Map behavior to clear all keys and values except those corresponding to JavaBeans properties.
    boolean
    Override the default Map behavior to return true if the specified value is present in either the underlying Map or one of the local property values.
    private Iterator
    Return an Iterator over the set of Map.Entry objects representing our key-value pairs.
    private Map.Entry
    Return a Map.Entry for the specified key value, if it is present; otherwise, return null.
    Override the default Map behavior to return a Set that meets the specified default behavior except for attempts to remove the key for a property of the Context implementation class, which will throw UnsupportedOperationException.
    get(Object key)
    Override the default Map behavior to return the value of a local property if the specified key matches a local property name.
    private void
    Customize the contents of our underlying Map so that it contains keys corresponding to all of the JavaBeans properties of the Context implementation class.
    boolean
    Override the default Map behavior to return true if the underlying Map only contains key-value pairs for local properties (if any).
    Override the default Map behavior to return a Set that meets the specified default behavior except for attempts to remove the key for a property of the Context implementation class, which will throw UnsupportedOperationException.
    put(Object key, Object value)
    Override the default Map behavior to set the value of a local property if the specified key matches a local property name.
    void
    putAll(Map map)
    Override the default Map behavior to call the put() method individually for each key-value pair in the specified Map.
    private Object
    Get and return the value for the specified property.
    Override the default Map behavior to throw UnsupportedOperationException on any attempt to remove a key that is the name of a local property.
    private boolean
    Remove the specified key-value pair, if it exists, and return true.
    Override the default Map behavior to return a Collection that meets the specified default behavior except for attempts to remove the key for a property of the Context implementation class, which will throw UnsupportedOperationException.
    private Iterator
    Return an Iterator over the set of values in this Map.
    private void
    Set the value for the specified property.

    Methods inherited from class java.util.AbstractMap

    equals, hashCode, toString

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • descriptors

      private transient Map descriptors

      The PropertyDescriptors for all JavaBeans properties of this Context implementation class, keyed by property name. This collection is allocated only if there are any JavaBeans properties.

    • pd

      private transient PropertyDescriptor[] pd

      The same PropertyDescriptors as an array.

    • singleton

      private static Object singleton

      Distinguished singleton value that is stored in the map for each key that is actually a property. This value is used to ensure that equals() comparisons will always fail.

    • zeroParams

      private static Object[] zeroParams

      Zero-length array of parameter values for calling property getters.

  • Constructor Details

    • ContextBase

      public ContextBase()
      Default, no argument constructor.
    • ContextBase

      public ContextBase(Map map)

      Initialize the contents of this Context by copying the values from the specified Map. Any keys in map that correspond to local properties will cause the setter method for that property to be called.

      Parameters:
      map - Map whose key-value pairs are added
      Throws:
      IllegalArgumentException - if an exception is thrown writing a local property value
      UnsupportedOperationException - if a local property does not have a write method.
  • Method Details

    • clear

      public void clear()

      Override the default Map behavior to clear all keys and values except those corresponding to JavaBeans properties.

      Specified by:
      clear in interface Map
      Overrides:
      clear in class HashMap
    • containsValue

      public boolean containsValue(Object value)

      Override the default Map behavior to return true if the specified value is present in either the underlying Map or one of the local property values.

      Specified by:
      containsValue in interface Map
      Overrides:
      containsValue in class HashMap
      Parameters:
      value - the value look for in the context.
      Returns:
      true if found in this context otherwise false.
      Throws:
      IllegalArgumentException - if a property getter throws an exception
    • entrySet

      public Set entrySet()

      Override the default Map behavior to return a Set that meets the specified default behavior except for attempts to remove the key for a property of the Context implementation class, which will throw UnsupportedOperationException.

      Specified by:
      entrySet in interface Map
      Overrides:
      entrySet in class HashMap
      Returns:
      Set of entries in the Context.
    • get

      public Object get(Object key)

      Override the default Map behavior to return the value of a local property if the specified key matches a local property name.

      IMPLEMENTATION NOTE - If the specified key identifies a write-only property, null will arbitrarily be returned, in order to avoid difficulties implementing the contracts of the Map interface.

      Specified by:
      get in interface Map
      Overrides:
      get in class HashMap
      Parameters:
      key - Key of the value to be returned
      Returns:
      The value for the specified key.
      Throws:
      IllegalArgumentException - if an exception is thrown reading this local property value
      UnsupportedOperationException - if this local property does not have a read method.
    • isEmpty

      public boolean isEmpty()

      Override the default Map behavior to return true if the underlying Map only contains key-value pairs for local properties (if any).

      Specified by:
      isEmpty in interface Map
      Overrides:
      isEmpty in class HashMap
      Returns:
      true if this Context is empty, otherwise false.
    • keySet

      public Set keySet()

      Override the default Map behavior to return a Set that meets the specified default behavior except for attempts to remove the key for a property of the Context implementation class, which will throw UnsupportedOperationException.

      Specified by:
      keySet in interface Map
      Overrides:
      keySet in class HashMap
      Returns:
      The set of keys for objects in this Context.
    • put

      public Object put(Object key, Object value)

      Override the default Map behavior to set the value of a local property if the specified key matches a local property name.

      Specified by:
      put in interface Map
      Overrides:
      put in class HashMap
      Parameters:
      key - Key of the value to be stored or replaced
      value - New value to be stored
      Returns:
      The value added to the Context.
      Throws:
      IllegalArgumentException - if an exception is thrown reading or wrting this local property value
      UnsupportedOperationException - if this local property does not have both a read method and a write method
    • putAll

      public void putAll(Map map)

      Override the default Map behavior to call the put() method individually for each key-value pair in the specified Map.

      Specified by:
      putAll in interface Map
      Overrides:
      putAll in class HashMap
      Parameters:
      map - Map containing key-value pairs to store (or replace)
      Throws:
      IllegalArgumentException - if an exception is thrown reading or wrting a local property value
      UnsupportedOperationException - if a local property does not have both a read method and a write method
    • remove

      public Object remove(Object key)

      Override the default Map behavior to throw UnsupportedOperationException on any attempt to remove a key that is the name of a local property.

      Specified by:
      remove in interface Map
      Overrides:
      remove in class HashMap
      Parameters:
      key - Key to be removed
      Returns:
      The value removed from the Context.
      Throws:
      UnsupportedOperationException - if the specified key matches the name of a local property
    • values

      public Collection values()

      Override the default Map behavior to return a Collection that meets the specified default behavior except for attempts to remove the key for a property of the Context implementation class, which will throw UnsupportedOperationException.

      Specified by:
      values in interface Map
      Overrides:
      values in class HashMap
      Returns:
      The collection of values in this Context.
    • entriesIterator

      private Iterator entriesIterator()

      Return an Iterator over the set of Map.Entry objects representing our key-value pairs.

    • entry

      private Map.Entry entry(Object key)

      Return a Map.Entry for the specified key value, if it is present; otherwise, return null.

      Parameters:
      key - Attribute key or property name
    • initialize

      private void initialize()

      Customize the contents of our underlying Map so that it contains keys corresponding to all of the JavaBeans properties of the Context implementation class.

      Throws:
      IllegalArgumentException - if an exception is thrown writing this local property value
      UnsupportedOperationException - if this local property does not have a write method.
    • readProperty

      private Object readProperty(PropertyDescriptor descriptor)

      Get and return the value for the specified property.

      Parameters:
      descriptor - PropertyDescriptor for the specified property
      Throws:
      IllegalArgumentException - if an exception is thrown reading this local property value
      UnsupportedOperationException - if this local property does not have a read method.
    • remove

      private boolean remove(Map.Entry entry)

      Remove the specified key-value pair, if it exists, and return true. If this pair does not exist, return false.

      Parameters:
      entry - Key-value pair to be removed
      Throws:
      UnsupportedOperationException - if the specified key identifies a property instead of an attribute
    • valuesIterator

      private Iterator valuesIterator()

      Return an Iterator over the set of values in this Map.

    • writeProperty

      private void writeProperty(PropertyDescriptor descriptor, Object value)

      Set the value for the specified property.

      Parameters:
      descriptor - PropertyDescriptor for the specified property
      value - The new value for this property (must be of the correct type)
      Throws:
      IllegalArgumentException - if an exception is thrown writing this local property value
      UnsupportedOperationException - if this local property does not have a write method.