freemarker.template
Class SimpleHash

java.lang.Object
  extended by freemarker.template.SimpleHash
All Implemented Interfaces:
TemplateHashModel, TemplateModel, TemplateModelRoot, TemplateObjectModel, TemplateWriteableHashModel, java.io.Serializable
Direct Known Subclasses:
ExtendedHash

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

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

All the public methods in this implementation are synchronized.

Version:
$Id: SimpleHash.java 1101 2013-04-01 04:17:32Z run2000 $
See Also:
SimpleList, SimpleScalar, SimpleNumber, Serialized Form

Field Summary
protected  java.util.Map<java.lang.String,TemplateModel> hash
          The contents of this SimpleHash are stored in this Map object.
 
Constructor Summary
SimpleHash()
          Constructs an empty SimpleHash.
SimpleHash(java.util.Map<java.lang.String,? extends TemplateModel> hash)
          Constructs a SimpleHash given a Map of values.
 
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<java.lang.String,TemplateModel> 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<java.lang.String,? extends TemplateModel> hash)
Constructs a SimpleHash given a Map of values. A defensive copy of the map is taken.

Parameters:
hash - The Map of values to be copied into this SimpleHash.
Throws:
java.lang.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
Throws:
TemplateModelException

put

public void put(java.lang.String key,
                TemplateModel model)
Puts a TemplateModel in the hash.

Specified by:
put in interface TemplateModelRoot
Specified by:
put in interface TemplateWriteableHashModel
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.
Throws:
TemplateModelException - there was a problem getting the value for the given key

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
Throws:
TemplateModelException - the object could not be returned

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