freemarker.template
Class SimpleHash

java.lang.Object
  extended by freemarker.template.WrappingTemplateModel
      extended by freemarker.template.SimpleHash
All Implemented Interfaces:
TemplateHashModel, TemplateHashModelEx, TemplateModel, java.io.Serializable
Direct Known Subclasses:
AllHttpScopesHashModel, Environment.Namespace

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.

Version:
$Id: SimpleHash.java,v 1.72.2.2 2006/02/26 18:26:18 revusky Exp $
See Also:
SimpleSequence, SimpleScalar, Serialized Form

Field Summary
 
Fields inherited from interface freemarker.template.TemplateModel
NOTHING
 
Constructor Summary
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.
 
Method Summary
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()
           
 
Methods inherited from class freemarker.template.WrappingTemplateModel
getDefaultObjectWrapper, getObjectWrapper, setDefaultObjectWrapper, setObjectWrapper, wrap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SimpleHash

public SimpleHash()
Constructs an empty hash that uses the default wrapper set in WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper).


SimpleHash

public 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).

Parameters:
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.

SimpleHash

public SimpleHash(ObjectWrapper wrapper)
Creates an empty simple hash using the specified object wrapper.

Parameters:
wrapper - The object wrapper to use to wrap objects into TemplateModel instances. If null, the default wrapper set in WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper) is used.

SimpleHash

public 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.

Parameters:
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
wrapper - The object wrapper to use to wrap objects into TemplateModel instances. If null, the default wrapper set in WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper) is used.
Method Detail

copyMap

protected java.util.Map copyMap(java.util.Map map)

put

public void put(java.lang.String key,
                java.lang.Object obj)
Adds a key-value entry to the map.

Parameters:
key - the name by which the object is identified in the template.
obj - the object to store.

put

public void put(java.lang.String key,
                boolean b)
Puts a boolean in the map

Parameters:
key - the name by which the resulting TemplateModel is identified in the template.
b - the boolean to store.

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
Description copied from interface: TemplateHashModel
Gets a TemplateModel from the hash.

Specified by:
get in interface TemplateHashModel
Parameters:
key - the name by which the TemplateModel is identified in the template.
Returns:
the TemplateModel referred to by the key, or null if not found.
Throws:
TemplateModelException

remove

public void remove(java.lang.String key)
Removes the given key from the underlying map.

Parameters:
key - the key to be removed

putAll

public void putAll(java.util.Map m)
Adds all the key/value entries in the map

Parameters:
m - the map with the entries to add, the keys are assumed to be strings.

toMap

public java.util.Map toMap()
                    throws TemplateModelException
Note that this method creates and returns a deep-copy of the underlying hash used internally. This could be a gotcha for some people at some point who want to alter something in the data model, but we should maintain our immutability semantics (at least using default SimpleXXX wrappers) for the data model. It will recursively unwrap the stuff in the underlying container.

Throws:
TemplateModelException

toString

public java.lang.String toString()
Convenience method for returning the String value of the underlying map.

Overrides:
toString in class java.lang.Object

size

public int size()
Specified by:
size in interface TemplateHashModelEx
Returns:
the number of key/value mappings in the hash.

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface TemplateHashModel

keys

public TemplateCollectionModel keys()
Specified by:
keys in interface TemplateHashModelEx
Returns:
a collection containing the keys in the hash. Every element of the returned collection must implement the TemplateScalarModel (as the keys of hashes are always strings).

values

public TemplateCollectionModel values()
Specified by:
values in interface TemplateHashModelEx
Returns:
a collection containing the values in the hash.

synchronizedWrapper

public SimpleHash synchronizedWrapper()