DefaultMapAdapter (FreeMarker 2.3.35 API)
Description: declaration: package: freemarker.template, class: DefaultMapAdapter

Package freemarker.template

# Class DefaultMapAdapter

java.lang.Object
freemarker.template.WrappingTemplateModel
freemarker.template.DefaultMapAdapter

All Implemented Interfaces:
  `WrapperTemplateModel`, `AdapterTemplateModel`, `TemplateHashModel`, `TemplateHashModelEx`, `TemplateHashModelEx2`, `TemplateModel`, `TemplateModelWithAPISupport`, `Serializable`

public class DefaultMapAdapter extends WrappingTemplateModel implements TemplateHashModelEx2, AdapterTemplateModel, WrapperTemplateModel, TemplateModelWithAPISupport, Serializable
Adapts a `Map` to the corresponding `TemplateModel` interface(s), most importantly to `TemplateHashModelEx`. If you aren't wrapping an already existing `Map`, but build a hash specifically to be used from a template, also consider using `SimpleHash` (see comparison there).

Thread safety: A `DefaultMapAdapter` is as thread-safe as the `Map` that it wraps is. Normally you only have to consider read-only access, as the FreeMarker template language doesn't allow writing these hashes (though of course, Java methods called from the template can violate this rule).

This adapter is used by `DefaultObjectWrapper` if its `useAdaptersForCollections` property is `true`, which is the default when its `incompatibleImprovements` property is 2.3.22 or higher.

Since:
  2.3.22
See Also:
  Serialized Form

- ## Nested Class Summary

  ## Nested classes/interfaces inherited from interface freemarker.template.TemplateHashModelEx2

  `TemplateHashModelEx2.KeyValuePair, TemplateHashModelEx2.KeyValuePairIterator`

- ## Field Summary

  ### Fields inherited from interface freemarker.template.TemplateModel

  `NOTHING`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static DefaultMapAdapter`
  `adapt​(Map map, ObjectWrapperWithAPISupport wrapper)`
  Factory method for creating new adapter instances.
  `TemplateModel`
  `get​(String key)`
  Gets a `TemplateModel` from the hash.
  `Object`
  `getAdaptedObject​(Class hint)`
  Retrieves the underlying object, or some other object semantically equivalent to its value narrowed by the class hint.
  `TemplateModel`
  `getAPI()`
  Returns the model that exposes the (Java) API of the value.
  `Object`
  `getWrappedObject()`
  Retrieves the original object wrapped by this model.
  `boolean`
  `isEmpty()`
  `TemplateCollectionModel`
  `keys()`
  `TemplateHashModelEx2.KeyValuePairIterator`
  `keyValuePairIterator()`
  `int`
  `size()`
  `TemplateCollectionModel`
  `values()`

  ### Methods inherited from class freemarker.template.WrappingTemplateModel

  `getDefaultObjectWrapper, getObjectWrapper, setDefaultObjectWrapper, setObjectWrapper, wrap`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### adapt

    public static DefaultMapAdapter adapt​(Map map, ObjectWrapperWithAPISupport wrapper)
    Factory method for creating new adapter instances.
    Parameters:
      `map` - The map to adapt; can't be `null`.
      `wrapper` - The `ObjectWrapper` used to wrap the items in the array.

  - ### get

    public TemplateModel get​(String key) throws TemplateModelException
    Description copied from interface: `TemplateHashModel`
    Gets a `TemplateModel` from the 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`

  - ### isEmpty

    public boolean isEmpty()
    Specified by:
      `isEmpty` in interface `TemplateHashModel`

  - ### size

    public int size()
    Specified by:
      `size` in interface `TemplateHashModelEx`
    Returns:
      the number of key/value mappings in the hash.

  - ### keys

    public TemplateCollectionModel keys()
    Specified by:
      `keys` in interface `TemplateHashModelEx`
    Returns:
      a collection containing the keys in the hash. Every element of the returned collection must implement the `TemplateScalarModel` (as the keys of hashes are always strings).

  - ### values

    public TemplateCollectionModel values()
    Specified by:
      `values` in interface `TemplateHashModelEx`
    Returns:
      a collection containing the values in the hash. The elements of the returned collection can be any kind of `TemplateModel`-s.

  - ### keyValuePairIterator

    public TemplateHashModelEx2.KeyValuePairIterator keyValuePairIterator()
    Specified by:
      `keyValuePairIterator` in interface `TemplateHashModelEx2`
    Returns:
      The iterator that walks through the key-value pairs in the hash. Not `null`.

  - ### getAdaptedObject

    public Object getAdaptedObject​(Class hint)
    Description copied from interface: `AdapterTemplateModel`
    Retrieves the underlying object, or some other object semantically equivalent to its value narrowed by the class hint.
    Specified by:
      `getAdaptedObject` in interface `AdapterTemplateModel`
    Parameters:
      `hint` - the desired class of the returned value. An implementation should make reasonable effort to retrieve an object of the requested class, but if that is impossible, it must at least return the underlying object as-is. As a minimal requirement, an implementation must always return the exact underlying object when `hint.isInstance(underlyingObject)` holds. When called with `java.lang.Object.class`, it should return a generic Java object (i.e. if the model is wrapping a scripting language object that is further wrapping a Java object, the deepest underlying Java object should be returned).
    Returns:
      the underlying object, or its value accommodated for the hint class.

  - ### getWrappedObject

    public Object getWrappedObject()
    Description copied from interface: `WrapperTemplateModel`
    Retrieves the original object wrapped by this model.
    Specified by:
      `getWrappedObject` in interface `WrapperTemplateModel`

  - ### getAPI

    public TemplateModel getAPI() throws TemplateModelException
    Description copied from interface: `TemplateModelWithAPISupport`
    Returns the model that exposes the (Java) API of the value. This is usually implemented by delegating to `ObjectWrapperWithAPISupport.wrapAsAPI(Object)`.
    Specified by:
      `getAPI` in interface `TemplateModelWithAPISupport`
    Throws:
      `TemplateModelException`