org.szegedi.expose.model
Class ReflectionMapModel

java.lang.Object
  |
  +--org.szegedi.expose.model.ReflectionObjectModelBase
        |
        +--org.szegedi.expose.model.ReflectionObjectModel
              |
              +--org.szegedi.expose.model.ReflectionMapModel
All Implemented Interfaces:
freemarker.template.TemplateHashModel, freemarker.template.TemplateListModel, freemarker.template.TemplateMethodModel, freemarker.template.TemplateModel, freemarker.template.TemplateScalarModel

public class ReflectionMapModel
extends ReflectionObjectModel
implements freemarker.template.TemplateListModel, freemarker.template.TemplateMethodModel

A special case of ReflectionObjectModel that adds implementation for TemplateListModel and TemplateMethodModel on map objects. List model will iterate through entries, while method model is a shortcut for the Map.get() method. Using the model as a list model is thread-safe, as it maintains an iterator on a per-thread basis.

Version:
1.0
Author:
Attila Szegedi, attila@szegedi.org

Fields inherited from class org.szegedi.expose.model.ReflectionObjectModelBase
TYPE_ARRAY, TYPE_COLLECTION, TYPE_ENUMERATION, TYPE_ITERATOR, TYPE_MAP, TYPE_OBJECT, TYPE_OBJECT_BASE, TYPE_RESOURCE_BUNDLE
 
Constructor Summary
ReflectionMapModel(java.util.Map map)
          Creates a new model that wraps the specified map object.
 
Method Summary
 freemarker.template.TemplateModel exec(java.util.List arguments)
          The first argument is used as a key to call the map's get method.
 freemarker.template.TemplateModel get(int index)
          Throws an UnsupportedOperationException as map entry set elements can not be accessed at random.
static ReflectionMapModel getInstance(java.util.Map object)
          Returns a model wrapping the specified map object.
 int getType()
          Returns the type of this object (which is TYPE_MAP)
 boolean hasNext()
          Calls Iterator.hasNext().
 boolean isEmpty()
          Returns true if the underlying map is empty.
 boolean isRewound()
          Always return false to force calling rewind.
 freemarker.template.TemplateModel listSize()
          Returns a Simple scalar with a string containing the decimal representation of the map size.
 freemarker.template.TemplateModel next()
          Calls Iterator.next() and wraps the returned Map.Entry into a template model.
 void rewind()
          Creates a new iterator on the map's entry set that will be used to implement other TemplateListModel methods.
 
Methods inherited from class org.szegedi.expose.model.ReflectionObjectModel
get, getInstance
 
Methods inherited from class org.szegedi.expose.model.ReflectionObjectModelBase
getAsString, getInstance, getObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionMapModel

public ReflectionMapModel(java.util.Map map)
Creates a new model that wraps the specified map object.
Parameters:
object - the map object to wrap into a model.
Method Detail

getInstance

public static final ReflectionMapModel getInstance(java.util.Map object)
Returns a model wrapping the specified map object. If there is already a cached model instance for this map, returns the cached model instance. Models are cached using WeakReference objects. The caching can be turned off by setting the expose.reflection.nocache system property to true. In this case calling this method is equivalent to constructing a new model.
Parameters:
object - the map to wrap into a model.
Returns:
the model for the map

getType

public int getType()
Returns the type of this object (which is TYPE_MAP)
Overrides:
getType in class ReflectionObjectModel

hasNext

public boolean hasNext()
Calls Iterator.hasNext().
Specified by:
hasNext in interface freemarker.template.TemplateListModel

isRewound

public boolean isRewound()
Always return false to force calling rewind.
Specified by:
isRewound in interface freemarker.template.TemplateListModel

next

public freemarker.template.TemplateModel next()
Calls Iterator.next() and wraps the returned Map.Entry into a template model.
Specified by:
next in interface freemarker.template.TemplateListModel

rewind

public void rewind()
Creates a new iterator on the map's entry set that will be used to implement other TemplateListModel methods.
Specified by:
rewind in interface freemarker.template.TemplateListModel

exec

public freemarker.template.TemplateModel exec(java.util.List arguments)
The first argument is used as a key to call the map's get method.
Specified by:
exec in interface freemarker.template.TemplateMethodModel

isEmpty

public boolean isEmpty()
Returns true if the underlying map is empty.
Specified by:
isEmpty in interface freemarker.template.TemplateModel
Overrides:
isEmpty in class ReflectionObjectModelBase

get

public freemarker.template.TemplateModel get(int index)
Throws an UnsupportedOperationException as map entry set elements can not be accessed at random.

listSize

public freemarker.template.TemplateModel listSize()
Returns a Simple scalar with a string containing the decimal representation of the map size.