freemarker.template
Class SimpleHash

java.lang.Object
  |
  +--freemarker.template.SimpleHash
All Implemented Interfaces:
java.io.Serializable, TemplateHashModel, TemplateModel, TemplateModelRoot, TemplateObjectModel
Direct Known Subclasses:
ExtendedHash

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

A simple implementation of the TemplateHashModel and TemplateModelRoot interfaces, using an underlying Map.

All the public methods in this implementation are synchronized.

Version:
$Id: SimpleHash.java,v 1.20 2003/11/27 08:03:44 run2000 Exp $
See Also:
SimpleList, SimpleScalar, SimpleNumber, Serialized Form

Field Summary
protected  java.util.Map hash
          The contents of this SimpleHash are stored in this Map object.
 
Constructor Summary
SimpleHash()
          Constructs an empty SimpleHash.
SimpleHash(java.util.Map hash)
          Constructs a SimpleHash given the backing Map.
 
Method Summary
 boolean equals(java.lang.Object o)
          Tests this object for equality with the given object.
 TemplateModel get(java.lang.String key)
          Gets a TemplateModel from the underlying hash.
 java.lang.Object getAsObject()
          Return an unmodifiable copy of the underlying Map object for manipulation by the freemarker.ext.beans package.
 int hashCode()
          Return the hash value for this object.
 boolean isEmpty()
          Is the underlying Map empty?
 void put(java.lang.String key, boolean value)
          Puts a boolean in the hash, by first wrapping the boolean in a FastBoolean.
 void put(java.lang.String key, long value)
          Puts a number in the hash, by first wrapping the string in a FastNumber.
 void put(java.lang.String key, java.lang.Number value)
          Puts a number in the hash, by first wrapping the string in a FastNumber.
 void put(java.lang.String key, java.lang.String value)
          Puts a string in the hash, by first wrapping the string in a FastScalar.
 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 Map.
 java.lang.String toString()
          Convenience method for returning the String value of the underlying hash.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

hash

protected java.util.Map hash
The contents of this SimpleHash are stored in this Map object.
Constructor Detail

SimpleHash

public SimpleHash()
Constructs an empty SimpleHash.

SimpleHash

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

isEmpty

public boolean isEmpty()
                throws TemplateModelException
Is the underlying Map empty?
Specified by:
isEmpty in interface TemplateModel
Returns:
true if the Map is empty, otherwise false

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 value)
Puts a string in the hash, by first wrapping the string in a FastScalar.
Parameters:
key - the name by which the resulting TemplateModel is identified in the template.
value - the string to store.

put

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

put

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

put

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

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 Map.
Specified by:
remove in interface TemplateModelRoot
Parameters:
key - the key to be removed

getAsObject

public java.lang.Object getAsObject()
                             throws TemplateModelException
Return an unmodifiable copy of the underlying Map object for manipulation by the freemarker.ext.beans package.
Specified by:
getAsObject in interface TemplateObjectModel
Returns:
an unmodifiable view of the underlying Map object

toString

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

equals

public boolean equals(java.lang.Object o)
Tests this object for equality with the given object.
Overrides:
equals in class java.lang.Object
Parameters:
o - the object to be compared against
Returns:
true if the objects are equal, otherwise false

hashCode

public int hashCode()
Return the hash value for this object.
Overrides:
hashCode in class java.lang.Object
Returns:
a hash code corresponding to the value of the object