public class SimpleHash extends WrappingTemplateModel implements TemplateHashModelEx2, java.io.Serializable
TemplateHashModelEx interface, using its own underlying Map or
SortedMap for storing the hash entries. If you are wrapping an already existing Map, you should
certainly use DefaultMapAdapter instead (see comparison below).
This class is thread-safe if you don't call modifying methods (like put(String, Object),
remove(String), etc.) after you have made the object available for multiple threads (assuming you have
published it safely to the other threads; see JSR-133 Java Memory Model). These methods aren't called by FreeMarker,
so it's usually not a concern.
SimpleHash VS DefaultMapAdapter - Which to use when?
For a Map that exists regardless of FreeMarker, only you need to access it from templates,
DefaultMapAdapter should be the default choice, as it reflects the exact behavior of the underlying
Map (no surprises), can be unwrapped to the originally wrapped object (important when passing it to Java
methods from the template), and has more predictable performance (no spikes).
For a hash that's made specifically to be used from templates, creating an empty SimpleHash then filling it
with put(String, Object) is usually the way to go, as the resulting hash is significantly faster
to read from templates than a DefaultMapAdapter (though it's somewhat slower to read from a plain Java method
to which it had to be passed adapted to a Map).
It also matters if for how many times will the same Map entry be read from the template(s) later, on
average. If, on average, you read each entry for more than 4 times, SimpleHash will be most certainly faster,
but if for 2 times or less (and especially if not at all) then DefaultMapAdapter will be faster. Before
choosing based on performance though, pay attention to the behavioral differences; SimpleHash will
shallow-copy the original Map at construction time, so key order will be lost in some cases, and it won't
reflect Map content changes after the SimpleHash construction, also SimpleHash can't be
unwrapped to the original Map instance.
DefaultMapAdapter,
TemplateHashModelEx,
Serialized FormTemplateHashModelEx2.KeyValuePair, TemplateHashModelEx2.KeyValuePairIteratorNOTHING| Constructor and Description |
|---|
SimpleHash()
Deprecated.
|
SimpleHash(java.util.Map map)
Deprecated.
|
SimpleHash(java.util.Map<java.lang.String,java.lang.Object> directMap,
ObjectWrapper wrapper,
int overloadDistinction)
Creates an instance that will use the specified
Map directly as its backing store; beware, the
Map will be possibly modified by SimpleHash, even if you only read the SimpleHash. |
SimpleHash(java.util.Map map,
ObjectWrapper wrapper)
Creates a new hash by shallow-coping (possibly cloning) the underlying map; in many applications you should use
DefaultMapAdapter instead. |
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() |
TemplateHashModelEx2.KeyValuePairIterator |
keyValuePairIterator() |
void |
put(java.lang.String key,
boolean b)
Puts a boolean in the map
|
void |
put(java.lang.String key,
java.lang.Object value)
Adds a key-value entry to this hash.
|
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()
Returns the
toString() of the underlying Map. |
TemplateCollectionModel |
values() |
getDefaultObjectWrapper, getObjectWrapper, setDefaultObjectWrapper, setObjectWrapper, wrap@Deprecated public SimpleHash()
SimpleHash(ObjectWrapper)WrappingTemplateModel.setDefaultObjectWrapper(ObjectWrapper).@Deprecated public SimpleHash(java.util.Map map)
SimpleHash(Map, ObjectWrapper)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<java.lang.String,java.lang.Object> directMap,
ObjectWrapper wrapper,
int overloadDistinction)
Map directly as its backing store; beware, the
Map will be possibly modified by SimpleHash, even if you only read the SimpleHash.
That's because when a value is read, it's replaced with the corresponding TemplateModel.
The goal of this constructor is to allow you to control technical aspects, like the initial capacity, and
ordering of the underlying Map implementation. The iteration order of the SimpleHash will be
the same as of the underlying Map.
directMap - The map that the instance will use directly as backing storage; possibly will be modified,
even when you only read the SimpleHash! Must allow any kind of object as value,
including null! Supporting null keys is not needed.overloadDistinction - To avoid ambiguity with other overloads; the value is unused.public SimpleHash(java.util.Map map,
ObjectWrapper wrapper)
DefaultMapAdapter instead.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 contained objects into TemplateModel instances. Using
null is deprecated but allowed, in which case the deprecated 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 value)
key - The name by which the object is identified in the template.value - The value to which the name will be associated. This will only be wrapped to TemplateModel
lazily when it's first read.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
TemplateHashModelget in interface TemplateHashModelkey - the name by which the TemplateModel
is identified in the template.TemplateModelExceptionpublic 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
TemplateModelExceptionpublic java.lang.String toString()
toString() of the underlying Map.toString in class java.lang.Objectpublic int size()
size in interface TemplateHashModelExpublic boolean isEmpty()
isEmpty in interface TemplateHashModelpublic TemplateCollectionModel keys()
keys in interface TemplateHashModelExTemplateScalarModel
(as the keys of hashes are always strings).public TemplateCollectionModel values()
values in interface TemplateHashModelExTemplateModel-s.public TemplateHashModelEx2.KeyValuePairIterator keyValuePairIterator()
keyValuePairIterator in interface TemplateHashModelEx2null.public SimpleHash synchronizedWrapper()