Class ContextBase
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap
-
- org.apache.commons.chain.impl.ContextBase
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map
,Context
- Direct Known Subclasses:
WebContext
public class ContextBase extends java.util.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 theMap
, with the key being the name of the property itself.IMPLEMENTATION NOTE - Because
empty
is a read-only property defined by theMap
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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
ContextBase.EntrySetImpl
Private implementation ofSet
that implements the semantics required for the value returned byentrySet()
.private class
ContextBase.EntrySetIterator
Private implementation ofIterator
for theSet
returned byentrySet()
.private class
ContextBase.MapEntryImpl
Private implementation ofMap.Entry
for each item inEntrySetImpl
.private class
ContextBase.ValuesImpl
Private implementation ofCollection
that implements the semantics required for the value returned byvalues()
.private class
ContextBase.ValuesIterator
Private implementation ofIterator
for theCollection
returned byvalues()
.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map
descriptors
ThePropertyDescriptor
s for all JavaBeans properties of thisContext
implementation class, keyed by property name.private java.beans.PropertyDescriptor[]
pd
The samePropertyDescriptor
s as an array.private static java.lang.Object
singleton
Distinguished singleton value that is stored in the map for each key that is actually a property.private static java.lang.Object[]
zeroParams
Zero-length array of parameter values for calling property getters.
-
Constructor Summary
Constructors Constructor Description ContextBase()
Default, no argument constructor.ContextBase(java.util.Map map)
Initialize the contents of thisContext
by copying the values from the specifiedMap
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Override the defaultMap
behavior to clear all keys and values except those corresponding to JavaBeans properties.boolean
containsValue(java.lang.Object value)
Override the defaultMap
behavior to returntrue
if the specified value is present in either the underlyingMap
or one of the local property values.private java.util.Iterator
entriesIterator()
Return anIterator
over the set ofMap.Entry
objects representing our key-value pairs.private java.util.Map.Entry
entry(java.lang.Object key)
Return aMap.Entry
for the specified key value, if it is present; otherwise, returnnull
.java.util.Set
entrySet()
Override the defaultMap
behavior to return aSet
that meets the specified default behavior except for attempts to remove the key for a property of theContext
implementation class, which will throwUnsupportedOperationException
.java.lang.Object
get(java.lang.Object key)
Override the defaultMap
behavior to return the value of a local property if the specified key matches a local property name.private void
initialize()
Customize the contents of our underlyingMap
so that it contains keys corresponding to all of the JavaBeans properties of theContext
implementation class.boolean
isEmpty()
Override the defaultMap
behavior to returntrue
if the underlyingMap
only contains key-value pairs for local properties (if any).java.util.Set
keySet()
Override the defaultMap
behavior to return aSet
that meets the specified default behavior except for attempts to remove the key for a property of theContext
implementation class, which will throwUnsupportedOperationException
.java.lang.Object
put(java.lang.Object key, java.lang.Object value)
Override the defaultMap
behavior to set the value of a local property if the specified key matches a local property name.void
putAll(java.util.Map map)
Override the defaultMap
behavior to call theput()
method individually for each key-value pair in the specifiedMap
.private java.lang.Object
readProperty(java.beans.PropertyDescriptor descriptor)
Get and return the value for the specified property.java.lang.Object
remove(java.lang.Object key)
Override the defaultMap
behavior to throwUnsupportedOperationException
on any attempt to remove a key that is the name of a local property.private boolean
remove(java.util.Map.Entry entry)
Remove the specified key-value pair, if it exists, and returntrue
.java.util.Collection
values()
Override the defaultMap
behavior to return aCollection
that meets the specified default behavior except for attempts to remove the key for a property of theContext
implementation class, which will throwUnsupportedOperationException
.private java.util.Iterator
valuesIterator()
Return anIterator
over the set of values in thisMap
.private void
writeProperty(java.beans.PropertyDescriptor descriptor, java.lang.Object value)
Set the value for the specified property.-
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll, size
-
-
-
-
Field Detail
-
descriptors
private transient java.util.Map descriptors
The
PropertyDescriptor
s for all JavaBeans properties of thisContext
implementation class, keyed by property name. This collection is allocated only if there are any JavaBeans properties.
-
pd
private transient java.beans.PropertyDescriptor[] pd
The same
PropertyDescriptor
s as an array.
-
singleton
private static java.lang.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 java.lang.Object[] zeroParams
Zero-length array of parameter values for calling property getters.
-
-
Constructor Detail
-
ContextBase
public ContextBase()
Default, no argument constructor.
-
ContextBase
public ContextBase(java.util.Map map)
Initialize the contents of this
Context
by copying the values from the specifiedMap
. Any keys inmap
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:
java.lang.IllegalArgumentException
- if an exception is thrown writing a local property valuejava.lang.UnsupportedOperationException
- if a local property does not have a write method.
-
-
Method Detail
-
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 interfacejava.util.Map
- Overrides:
clear
in classjava.util.HashMap
-
containsValue
public boolean containsValue(java.lang.Object value)
Override the default
Map
behavior to returntrue
if the specified value is present in either the underlyingMap
or one of the local property values.- Specified by:
containsValue
in interfacejava.util.Map
- Overrides:
containsValue
in classjava.util.HashMap
- Parameters:
value
- the value look for in the context.- Returns:
true
if found in this context otherwisefalse
.- Throws:
java.lang.IllegalArgumentException
- if a property getter throws an exception
-
entrySet
public java.util.Set entrySet()
Override the default
Map
behavior to return aSet
that meets the specified default behavior except for attempts to remove the key for a property of theContext
implementation class, which will throwUnsupportedOperationException
.- Specified by:
entrySet
in interfacejava.util.Map
- Overrides:
entrySet
in classjava.util.HashMap
- Returns:
- Set of entries in the Context.
-
get
public java.lang.Object get(java.lang.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 theMap
interface.- Specified by:
get
in interfacejava.util.Map
- Overrides:
get
in classjava.util.HashMap
- Parameters:
key
- Key of the value to be returned- Returns:
- The value for the specified key.
- Throws:
java.lang.IllegalArgumentException
- if an exception is thrown reading this local property valuejava.lang.UnsupportedOperationException
- if this local property does not have a read method.
-
isEmpty
public boolean isEmpty()
Override the default
Map
behavior to returntrue
if the underlyingMap
only contains key-value pairs for local properties (if any).- Specified by:
isEmpty
in interfacejava.util.Map
- Overrides:
isEmpty
in classjava.util.HashMap
- Returns:
true
if this Context is empty, otherwisefalse
.
-
keySet
public java.util.Set keySet()
Override the default
Map
behavior to return aSet
that meets the specified default behavior except for attempts to remove the key for a property of theContext
implementation class, which will throwUnsupportedOperationException
.- Specified by:
keySet
in interfacejava.util.Map
- Overrides:
keySet
in classjava.util.HashMap
- Returns:
- The set of keys for objects in this Context.
-
put
public java.lang.Object put(java.lang.Object key, java.lang.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 interfacejava.util.Map
- Overrides:
put
in classjava.util.HashMap
- Parameters:
key
- Key of the value to be stored or replacedvalue
- New value to be stored- Returns:
- The value added to the Context.
- Throws:
java.lang.IllegalArgumentException
- if an exception is thrown reading or wrting this local property valuejava.lang.UnsupportedOperationException
- if this local property does not have both a read method and a write method
-
putAll
public void putAll(java.util.Map map)
Override the default
Map
behavior to call theput()
method individually for each key-value pair in the specifiedMap
.- Specified by:
putAll
in interfacejava.util.Map
- Overrides:
putAll
in classjava.util.HashMap
- Parameters:
map
-Map
containing key-value pairs to store (or replace)- Throws:
java.lang.IllegalArgumentException
- if an exception is thrown reading or wrting a local property valuejava.lang.UnsupportedOperationException
- if a local property does not have both a read method and a write method
-
remove
public java.lang.Object remove(java.lang.Object key)
Override the default
Map
behavior to throwUnsupportedOperationException
on any attempt to remove a key that is the name of a local property.- Specified by:
remove
in interfacejava.util.Map
- Overrides:
remove
in classjava.util.HashMap
- Parameters:
key
- Key to be removed- Returns:
- The value removed from the Context.
- Throws:
java.lang.UnsupportedOperationException
- if the specifiedkey
matches the name of a local property
-
values
public java.util.Collection values()
Override the default
Map
behavior to return aCollection
that meets the specified default behavior except for attempts to remove the key for a property of theContext
implementation class, which will throwUnsupportedOperationException
.- Specified by:
values
in interfacejava.util.Map
- Overrides:
values
in classjava.util.HashMap
- Returns:
- The collection of values in this Context.
-
entriesIterator
private java.util.Iterator entriesIterator()
Return an
Iterator
over the set ofMap.Entry
objects representing our key-value pairs.
-
entry
private java.util.Map.Entry entry(java.lang.Object key)
Return a
Map.Entry
for the specified key value, if it is present; otherwise, returnnull
.- 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 theContext
implementation class.- Throws:
java.lang.IllegalArgumentException
- if an exception is thrown writing this local property valuejava.lang.UnsupportedOperationException
- if this local property does not have a write method.
-
readProperty
private java.lang.Object readProperty(java.beans.PropertyDescriptor descriptor)
Get and return the value for the specified property.
- Parameters:
descriptor
-PropertyDescriptor
for the specified property- Throws:
java.lang.IllegalArgumentException
- if an exception is thrown reading this local property valuejava.lang.UnsupportedOperationException
- if this local property does not have a read method.
-
remove
private boolean remove(java.util.Map.Entry entry)
Remove the specified key-value pair, if it exists, and return
true
. If this pair does not exist, returnfalse
.- Parameters:
entry
- Key-value pair to be removed- Throws:
java.lang.UnsupportedOperationException
- if the specified key identifies a property instead of an attribute
-
valuesIterator
private java.util.Iterator valuesIterator()
Return an
Iterator
over the set of values in thisMap
.
-
writeProperty
private void writeProperty(java.beans.PropertyDescriptor descriptor, java.lang.Object value)
Set the value for the specified property.
- Parameters:
descriptor
-PropertyDescriptor
for the specified propertyvalue
- The new value for this property (must be of the correct type)- Throws:
java.lang.IllegalArgumentException
- if an exception is thrown writing this local property valuejava.lang.UnsupportedOperationException
- if this local property does not have a write method.
-
-