org.apache.commons.beanutils

Class DynaBeanMapDecorator

Implemented Interfaces:
Map

public class DynaBeanMapDecorator
extends java.lang.Object
implements Map

Decorates a DynaBean to provide Map behaviour.

The motivation for this implementation is to provide access to DynaBean properties in technologies that are unaware of BeanUtils and DynaBeans - such as the expression languages of JSTL and JSF.

This can be achieved either by wrapping the DynaBean prior to providing it to the technolody to process or by providing a Map accessor method on the DynaBean implementation:


         public Map getMap() {
             return new DynaBeanMapDecorator(this);
         }

This, for example, could be used in JSTL in the following way to access a DynaBean's fooProperty:

Usage

To decorate a DynaBean simply instantiate this class with the target DynaBean:

The above example creates a read only Map. To create a Map which can be modified, construct a DynaBeanMapDecorator with the read only attribute set to false:

Limitations

In this implementation the entrySet(), keySet() and values() methods create an unmodifiable Set and it does not support the Map's clear() and remove() operations.

Version:
$Revision: 546471 $ $Date: 2007-06-12 13:57:20 +0100 (Tue, 12 Jun 2007) $
Since:
BeanUtils 1.8.0

Nested Class Summary

private static class
DynaBeanMapDecorator.MapEntry
Map.Entry implementation.

Field Summary

private DynaBean
dynaBean
private Set
keySet
private boolean
readOnly

Constructor Summary

DynaBeanMapDecorator(DynaBean dynaBean)
Constructs a read only Map for the specified DynaBean.
DynaBeanMapDecorator(DynaBean dynaBean, boolean readOnly)
Construct a Map for the specified DynaBean.

Method Summary

void
clear()
clear() operation is not supported.
boolean
containsKey(Object key)
Indicate whether the DynaBean contains a specified value for one (or more) of its properties.
boolean
containsValue(Object value)
Indicates whether the decorated DynaBean contains a specified value.
Set
entrySet()
Returns the Set of the property/value mappings in the decorated DynaBean.
Object
get(Object key)
Return the value for the specified key from the decorated DynaBean.
DynaBean
getDynaBean()
Provide access to the underlying DynaBean this Map decorates.
private DynaProperty[]
getDynaProperties()
Convenience method to retrieve the DynaPropertys for this DynaClass.
boolean
isEmpty()
Indicate whether the decorated DynaBean has any properties.
boolean
isReadOnly()
Indicate whether the Map is read only.
Set
keySet()
Returns the Set of the property names in the decorated DynaBean.
Object
put(Object key, Object value)
Set the value for the specified property in the decorated DynaBean.
void
putAll(Map map)
Copy the contents of a Map to the decorated DynaBean.
Object
remove(Object key)
remove() operation is not supported.
int
size()
Returns the number properties in the decorated DynaBean.
private String
toString(Object obj)
Convenience method to convert an Object to a String.
Collection
values()
Returns the set of property values in the decorated DynaBean.

Field Details

dynaBean

private DynaBean dynaBean

keySet

private Set keySet

readOnly

private boolean readOnly

Constructor Details

DynaBeanMapDecorator

public DynaBeanMapDecorator(DynaBean dynaBean)
Constructs a read only Map for the specified DynaBean.
Parameters:
dynaBean - The dyna bean being decorated

DynaBeanMapDecorator

public DynaBeanMapDecorator(DynaBean dynaBean,
                            boolean readOnly)
Construct a Map for the specified DynaBean.
Parameters:
dynaBean - The dyna bean being decorated
readOnly - true if the Mpa is read only otherwise false

Method Details

clear

public void clear()
clear() operation is not supported.

containsKey

public boolean containsKey(Object key)
Indicate whether the DynaBean contains a specified value for one (or more) of its properties.
Parameters:
key - The DynaBean's property name
Returns:
true if one of the DynaBean's properties contains a specified value.

containsValue

public boolean containsValue(Object value)
Indicates whether the decorated DynaBean contains a specified value.
Parameters:
value - The value to check for.
Returns:
true if one of the the DynaBean's properties contains the specified value, otherwise false.

entrySet

public Set entrySet()
Returns:
An unmodifiable set of the DynaBean property name/value pairs

get

public Object get(Object key)
Parameters:
key - The DynaBean's property name
Returns:
The value for the specified property.

getDynaBean

public DynaBean getDynaBean()
Provide access to the underlying DynaBean this Map decorates.
Returns:
the decorated DynaBean.

getDynaProperties

private DynaProperty[] getDynaProperties()
Convenience method to retrieve the DynaPropertys for this DynaClass.
Returns:
The an array of the DynaPropertys.

isEmpty

public boolean isEmpty()
Returns:
true if the DynaBean has no properties, otherwise false.

isReadOnly

public boolean isReadOnly()
Indicate whether the Map is read only.
Returns:
true if the Map is read only, otherwise false.

keySet

public Set keySet()
Returns the Set of the property names in the decorated DynaBean.

N.B.For DynaBeans whose associated DynaClass is a MutableDynaClass a new Set is created every time, otherwise the Set is created only once and cached.

Returns:
An unmodifiable set of the DynaBeans property names.

put

public Object put(Object key,
                  Object value)
Parameters:
key - The DynaBean's property name
value - The value for the specified property.
Returns:
The previous property's value.

putAll

public void putAll(Map map)
Parameters:
map - The Map of values to copy.

remove

public Object remove(Object key)
remove() operation is not supported.
Returns:
the value removed

size

public int size()
Returns:
The number of properties.

toString

private String toString(Object obj)
Convenience method to convert an Object to a String.
Parameters:
obj - The Object to convert
Returns:
String representation of the object

values

public Collection values()
Returns:
Unmodifiable collection of values.

Copyright (c) 2001-2007 - Apache Software Foundation