org.apache.commons.chain.impl
public class: ContextBase [javadoc |
source]
java.lang.Object
java.util.AbstractMap
java.util.HashMap
org.apache.commons.chain.impl.ContextBase
All Implemented Interfaces:
Context, Map, Serializable, Cloneable
Direct Known Subclasses:
ServletWebContext, FacesWebContext, TestContext, WebContext, PortletWebContext
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.
- author:
Craig - R. McClanahan
- version:
$ - Revision: 499247 $ $Date: 2007-01-24 04:09:44 +0000 (Wed, 24 Jan 2007) $
| Constructor: |
public ContextBase() {
super();
initialize();
}
Default, no argument constructor. |
public ContextBase(Map map) {
super(map);
initialize();
putAll(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.
- exception:
IllegalArgumentException - if an exception is thrown
writing a local property value
- exception:
UnsupportedOperationException - if a local property does not
have a write method.
|
| Methods from java.util.HashMap: |
|---|
|
clear, clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, put, putAll, remove, size, values |
| Methods from java.util.AbstractMap: |
|---|
|
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, values |
| Method from org.apache.commons.chain.impl.ContextBase Detail: |
public void clear() {
// ------------------------------------------------------------- Map Methods
if (descriptors == null) {
super.clear();
} else {
Iterator keys = keySet().iterator();
while (keys.hasNext()) {
Object key = keys.next();
if (!descriptors.containsKey(key)) {
keys.remove();
}
}
}
}
Override the default Map behavior to clear all keys and
values except those corresponding to JavaBeans properties.
|
public boolean containsValue(Object value) {
// Case 1 -- no local properties
if (descriptors == null) {
return (super.containsValue(value));
}
// Case 2 -- value found in the underlying Map
else if (super.containsValue(value)) {
return (true);
}
// Case 3 -- check the values of our readable properties
for (int i = 0; i < pd.length; i++) {
if (pd[i].getReadMethod() != null) {
Object prop = readProperty(pd[i]);
if (value == null) {
if (prop == null) {
return (true);
}
} else if (value.equals(prop)) {
return (true);
}
}
}
return (false);
}
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.
|
public Set entrySet() {
return (new EntrySetImpl());
}
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.
|
public Object get(Object key) {
// Case 1 -- no local properties
if (descriptors == null) {
return (super.get(key));
}
// Case 2 -- this is a local property
if (key != null) {
PropertyDescriptor descriptor =
(PropertyDescriptor) descriptors.get(key);
if (descriptor != null) {
if (descriptor.getReadMethod() != null) {
return (readProperty(descriptor));
} else {
return (null);
}
}
}
// Case 3 -- retrieve value from our underlying Map
return (super.get(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.
|
public boolean isEmpty() {
// Case 1 -- no local properties
if (descriptors == null) {
return (super.isEmpty());
}
// Case 2 -- compare key count to property count
return (super.size() < = descriptors.size());
}
Override the default Map behavior to return
true if the underlying Map only contains
key-value pairs for local properties (if any).
|
public Set keySet() {
return (super.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.
|
public Object put(Object key,
Object value) {
// Case 1 -- no local properties
if (descriptors == null) {
return (super.put(key, value));
}
// Case 2 -- this is a local property
if (key != null) {
PropertyDescriptor descriptor =
(PropertyDescriptor) descriptors.get(key);
if (descriptor != null) {
Object previous = null;
if (descriptor.getReadMethod() != null) {
previous = readProperty(descriptor);
}
writeProperty(descriptor, value);
return (previous);
}
}
// Case 3 -- store or replace value in our underlying map
return (super.put(key, value));
}
Override the default Map behavior to set the value
of a local property if the specified key matches a local property name.
|
public void putAll(Map map) {
Iterator pairs = map.entrySet().iterator();
while (pairs.hasNext()) {
Map.Entry pair = (Map.Entry) pairs.next();
put(pair.getKey(), pair.getValue());
}
}
Override the default Map behavior to call the
put() method individually for each key-value pair
in the specified Map.
|
public Object remove(Object key) {
// Case 1 -- no local properties
if (descriptors == null) {
return (super.remove(key));
}
// Case 2 -- this is a local property
if (key != null) {
PropertyDescriptor descriptor =
(PropertyDescriptor) descriptors.get(key);
if (descriptor != null) {
throw new UnsupportedOperationException
("Local property '" + key + "' cannot be removed");
}
}
// Case 3 -- remove from underlying Map
return (super.remove(key));
}
Override the default Map behavior to throw
UnsupportedOperationException on any attempt to
remove a key that is the name of a local property.
|
public Collection values() {
return (new ValuesImpl());
}
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.
|