freemarker.template
Class SimpleHash

java.lang.Object
  |
  +--freemarker.template.SimpleHash
All Implemented Interfaces:
java.io.Serializable, TemplateHashModel, TemplateHashModelEx, TemplateModel, TemplateModelRoot

public class SimpleHash
extends java.lang.Object
implements TemplateModelRoot, TemplateHashModelEx, java.io.Serializable

A simple implementation of the TemplateHashModelEx and TemplateModelRoot interfaces, using an underlying HashMap.

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.14 2002/04/10 14:29:53 revusky Exp $
See Also:
SimpleList, SimpleScalar, Serialized Form

Field Summary
protected  java.util.HashMap hash
           
 
Constructor Summary
SimpleHash()
          Constructs an empty SimpleHash.
SimpleHash(java.util.Map map)
          Constructs a SimpleHash given the backing Map.
 
Method Summary
 TemplateModel get(java.lang.String key)
          Gets a TemplateModel from the underlying hash.
 java.util.Locale getLocale()
          gets the locale to be used for number/date formatting (A hook for later user currently -- revusky)
 boolean isEmpty()
          Is the underlying hash empty?
 TemplateSequenceModel keys()
           
 void put(java.lang.String key, boolean b)
          Puts a boolean in the hash, by first wrapping the boolean in a SimpleScalar.
 void put(java.lang.String key, java.lang.Number num)
           
 void put(java.lang.String key, java.lang.String s)
          Puts a string in the hash, by first wrapping the string in a SimpleScalar.
 void put(java.lang.String key, TemplateModel model)
          Puts a TemplateModel in the hash.
 void remove(java.lang.String key)
          Removes the given key from the underlying hash.
 void setLocale(java.util.Locale locale)
          sets the locale to be used for number/date formatting (A hook for later user currently -- revusky)
 int size()
           
 SimpleHash synchronizedWrapper()
           
 java.lang.String toString()
          Convenience method for returning the String value of the underlying hash.
 TemplateSequenceModel values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

hash

protected java.util.HashMap hash
Constructor Detail

SimpleHash

public SimpleHash()
Constructs an empty SimpleHash.

SimpleHash

public SimpleHash(java.util.Map map)
Constructs a SimpleHash given the backing Map.
Parameters:
hash - The Map to use as the backing for this SimpleHash.
Method Detail

isEmpty

public boolean isEmpty()
                throws TemplateModelException
Is the underlying hash empty?
Specified by:
isEmpty in interface TemplateModel
Following copied from interface: freemarker.template.TemplateModel
Returns:
true if this object is empty.

put

public void put(java.lang.String key,
                TemplateModel model)
Puts a TemplateModel in the hash.
Specified by:
put in interface TemplateModelRoot
Parameters:
key - the name by which the TemplateModel is identified in the template.
model - the TemplateModel to store.

put

public void put(java.lang.String key,
                java.lang.String s)
Puts a string in the hash, by first wrapping the string in a SimpleScalar.
Parameters:
key - the name by which the resulting TemplateModel is identified in the template.
s - the string to store.

put

public void put(java.lang.String key,
                boolean b)
Puts a boolean in the hash, by first wrapping the boolean in a SimpleScalar.
Parameters:
key - the name by which the resulting TemplateModel is identified in the template.
b - the boolean to store.

put

public void put(java.lang.String key,
                java.lang.Number num)

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
Gets a TemplateModel from the underlying 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.

remove

public void remove(java.lang.String key)
Removes the given key from the underlying hash.
Specified by:
remove in interface TemplateModelRoot
Parameters:
key - the key to be removed

toString

public java.lang.String toString()
Convenience method for returning the String value of the underlying hash.
Overrides:
toString in class java.lang.Object

setLocale

public void setLocale(java.util.Locale locale)
Description copied from interface: TemplateModelRoot
sets the locale to be used for number/date formatting (A hook for later user currently -- revusky)
Specified by:
setLocale in interface TemplateModelRoot

getLocale

public java.util.Locale getLocale()
Description copied from interface: TemplateModelRoot
gets the locale to be used for number/date formatting (A hook for later user currently -- revusky)
Specified by:
getLocale in interface TemplateModelRoot

size

public int size()
         throws TemplateModelException
Specified by:
size in interface TemplateHashModelEx
Following copied from interface: freemarker.template.TemplateHashModelEx
Returns:
the number of key/value mappings in the hash.

keys

public TemplateSequenceModel keys()
                           throws TemplateModelException
Specified by:
keys in interface TemplateHashModelEx
Following copied from interface: freemarker.template.TemplateHashModelEx
Returns:
a sequence containing the keys in the hash.

values

public TemplateSequenceModel values()
                             throws TemplateModelException
Specified by:
values in interface TemplateHashModelEx
Following copied from interface: freemarker.template.TemplateHashModelEx
Returns:
a sequence containing the values in the hash.

synchronizedWrapper

public SimpleHash synchronizedWrapper()