Class MapModel

All Implemented Interfaces:
WrapperTemplateModel, AdapterTemplateModel, TemplateHashModel, TemplateHashModelEx, TemplateMethodModel, TemplateMethodModelEx, TemplateModel, TemplateModelWithAPISupport, TemplateScalarModel

public class MapModel extends StringModel implements TemplateMethodModelEx

A special case of BeanModel that adds implementation for TemplateMethodModelEx on map objects that is a shortcut for the Map.get() method. Note that if the passed argument itself is a reflection-wrapper model, then the map lookup will be performed using the wrapped object as the key. Note that you can call get() using the map.key syntax inherited from BeanModel as well, however in that case the key is always a string.

The class itself does not implement the TemplateCollectionModel. You can, however use map.entrySet(), map.keySet(), or map.values() to obtain TemplateCollectionModel instances for various aspects of the map.

  • Constructor Details

    • MapModel

      public MapModel(Map map, BeansWrapper wrapper)
      Creates a new model that wraps the specified map object.
      Parameters:
      map - the map object to wrap into a model.
      wrapper - the BeansWrapper associated with this model. Every model has to have an associated BeansWrapper instance. The model gains many attributes from its wrapper, including the caching behavior, method exposure level, method-over-item shadowing policy etc.
  • Method Details

    • exec

      public Object exec(List arguments) throws TemplateModelException
      The first argument is used as a key to call the map's get method.
      Specified by:
      exec in interface TemplateMethodModel
      Specified by:
      exec in interface TemplateMethodModelEx
      Parameters:
      arguments - a List of TemplateModel-s, containing the arguments passed to the method. If the implementation absolutely wants to operate on POJOs, it can use the static utility methods in the DeepUnwrap class to easily obtain them. However, unwrapping is not always possible (or not perfectly), and isn't always efficient, so it's recommended to use the original TemplateModel value as much as possible.
      Returns:
      the return value of the method, or null. If the returned value does not implement TemplateModel, it will be automatically wrapped using the environment's object wrapper.
      Throws:
      TemplateModelException
    • invokeGenericGet

      protected TemplateModel invokeGenericGet(Map keyMap, Class clazz, String key) throws TemplateModelException
      Overridden to invoke the generic get method by casting to Map instead of through reflection - should yield better performance.
      Overrides:
      invokeGenericGet in class BeanModel
      Throws:
      TemplateModelException
    • isEmpty

      public boolean isEmpty()
      Description copied from class: BeanModel
      Tells whether the model is empty. It is empty if either the wrapped object is null, or it's a Boolean with false value.
      Specified by:
      isEmpty in interface TemplateHashModel
      Overrides:
      isEmpty in class BeanModel
    • size

      public int size()
      Specified by:
      size in interface TemplateHashModelEx
      Overrides:
      size in class BeanModel
      Returns:
      the number of key/value mappings in the hash.
    • keySet

      protected Set keySet()
      Description copied from class: BeanModel
      Helper method to support TemplateHashModelEx. Returns the Set of Strings which are available via the TemplateHashModel interface. Subclasses that override invokeGenericGet to provide additional hash keys should also override this method.
      Overrides:
      keySet in class BeanModel