public class SimpleHash extends WrappingTemplateModel implements TemplateHashModelEx, java.io.Serializable
A simple implementation of the TemplateHashModelEx
interface, using an underlying Map
or SortedMap
.
This class is thread-safe if you don't call the put or remove methods after you have made the object available for multiple threads.
Note:
As of 2.0, this class is unsynchronized by default.
To obtain a synchronized wrapper, call the synchronizedWrapper()
method.
SimpleSequence
,
SimpleScalar
,
Serialized FormNOTHING
Constructor and Description |
---|
SimpleHash()
Constructs an empty hash that uses the default wrapper set in
WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper) . |
SimpleHash(java.util.Map map)
Creates a new simple hash with the copy of the underlying map and the
default wrapper set in
WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper) . |
SimpleHash(java.util.Map map,
ObjectWrapper wrapper)
Creates a new simple hash with the copy of the underlying map and
either the default wrapper set in
WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper) , or
the JavaBeans wrapper . |
SimpleHash(ObjectWrapper wrapper)
Creates an empty simple hash using the specified object wrapper.
|
Modifier and Type | Method and Description |
---|---|
boolean |
containsKey(java.lang.String key)
Tells if the map contains a key or not, regardless if the associated value is
null or not. |
protected java.util.Map |
copyMap(java.util.Map map) |
TemplateModel |
get(java.lang.String key)
Gets a TemplateModel from the hash.
|
boolean |
isEmpty() |
TemplateCollectionModel |
keys() |
void |
put(java.lang.String key,
boolean b)
Puts a boolean in the map
|
void |
put(java.lang.String key,
java.lang.Object obj)
Adds a key-value entry to the map.
|
void |
putAll(java.util.Map m)
Adds all the key/value entries in the map
|
void |
remove(java.lang.String key)
Removes the given key from the underlying map.
|
int |
size() |
SimpleHash |
synchronizedWrapper() |
java.util.Map |
toMap()
Note that this method creates and returns a deep-copy of the underlying hash used
internally.
|
java.lang.String |
toString()
Convenience method for returning the String value of the
underlying map.
|
TemplateCollectionModel |
values() |
getDefaultObjectWrapper, getObjectWrapper, setDefaultObjectWrapper, setObjectWrapper, wrap
public SimpleHash()
WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper)
.public SimpleHash(java.util.Map map)
WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper)
.map
- The Map to use for the key/value pairs. It makes a copy for
internal use. If the map implements the SortedMap
interface, the
internal copy will be a TreeMap
, otherwise it will be a
HashMap
.public SimpleHash(ObjectWrapper wrapper)
wrapper
- The object wrapper to use to wrap objects into
TemplateModel
instances. If null, the default wrapper set in
WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper)
is
used.public SimpleHash(java.util.Map map, ObjectWrapper wrapper)
WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper)
, or
the JavaBeans wrapper
.map
- The Map to use for the key/value pairs. It makes a copy for
internal use. If the map implements the SortedMap
interface, the
internal copy will be a TreeMap
, otherwise it will be awrapper
- The object wrapper to use to wrap objects into
TemplateModel
instances. If null, the default wrapper set in
WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper)
is
used.protected java.util.Map copyMap(java.util.Map map)
public void put(java.lang.String key, java.lang.Object obj)
key
- the name by which the object is
identified in the template.obj
- the object to store.public void put(java.lang.String key, boolean b)
key
- the name by which the resulting TemplateModel
is identified in the template.b
- the boolean to store.public TemplateModel get(java.lang.String key) throws TemplateModelException
TemplateHashModel
get
in interface TemplateHashModel
key
- the name by which the TemplateModel
is identified in the template.TemplateModelException
public boolean containsKey(java.lang.String key)
null
or not.public void remove(java.lang.String key)
key
- the key to be removedpublic void putAll(java.util.Map m)
m
- the map with the entries to add, the keys are assumed to be strings.public java.util.Map toMap() throws TemplateModelException
TemplateModelException
public java.lang.String toString()
toString
in class java.lang.Object
public int size()
size
in interface TemplateHashModelEx
public boolean isEmpty()
isEmpty
in interface TemplateHashModel
public TemplateCollectionModel keys()
keys
in interface TemplateHashModelEx
TemplateScalarModel
(as the keys of hashes are always strings).public TemplateCollectionModel values()
values
in interface TemplateHashModelEx
TemplateModel
-s.public SimpleHash synchronizedWrapper()