Class ObjectContext

java.lang.Object
io.jstach.jstachio.context.ObjectContext
All Implemented Interfaces:
ContextNode, io.jstach.jstachio.context.Internal.ObjectContextNode, Formatter.Formattable, Iterable<@Nullable ContextNode>

public abstract non-sealed class ObjectContext extends Object implements io.jstach.jstachio.context.Internal.ObjectContextNode
Extend this class to make JStache model act like JSON object or a java.util.Map.
Author:
agentgt
See Also:
  • Constructor Details

    • ObjectContext

      protected ObjectContext()
      Do nothing constructor
  • Method Details

    • getValue

      public abstract @Nullable Object getValue(String key)
      Get a value by key. This is analagous to Map.get(Object).
      Specified by:
      getValue in interface io.jstach.jstachio.context.Internal.ObjectContextNode
      Parameters:
      key - not null
      Returns:
      value mapped to key.
    • object

      public final Object object()
      Description copied from interface: ContextNode
      The object being wrapped.
      Specified by:
      object in interface ContextNode
      Returns:
      the Map, Iterable or object that was wrapped. Never null.
    • get

      public final @Nullable ContextNode get(String field)
      Description copied from interface: ContextNode
      Gets a field from a ContextNode. This is direct access (end of a dotted path) and does not check the parents. The default implementation will check if the wrapping object is a Map and use it to return a child context node. Just like Map null will be returned if no field is found.
      Specified by:
      get in interface ContextNode
      Specified by:
      get in interface io.jstach.jstachio.context.Internal.ObjectContextNode
      Parameters:
      field - the name of the field
      Returns:
      a new child node. Maybe null.
    • find

      public final @Nullable ContextNode find(String field)
      Description copied from interface: ContextNode
      Will search up the tree for a field starting at this nodes children first.
      Specified by:
      find in interface ContextNode
      Parameters:
      field - context name (e.g. section name)
      Returns:
      null if not found otherwise creates a new node from the map or object containing the field.
    • parent

      public final @Nullable ContextNode parent()
      Description copied from interface: ContextNode
      The parent node.
      Specified by:
      parent in interface ContextNode
      Returns:
      the parent node or null if this is the root.
    • iterator

      public final Iterator<@Nullable ContextNode> iterator()
      Description copied from interface: ContextNode
      If the node is a Map or a non iterable/array a singleton iterator will be returned. Otherwise if it is an iterable/array new child context nodes will be created lazily.
      Specified by:
      iterator in interface ContextNode
      Specified by:
      iterator in interface io.jstach.jstachio.context.Internal.ObjectContextNode
      Specified by:
      iterator in interface Iterable<@Nullable ContextNode>
      Returns:
      lazy iterator of context nodes.
    • isFalsey

      public final boolean isFalsey()
      Description copied from interface: ContextNode
      Determines if the node is falsey. If falsey (return of true) inverted section blocks will be executed. The default checks if ContextNode.iterator() has any next elements and if it does not it is falsey.
      Specified by:
      isFalsey in interface ContextNode
      Specified by:
      isFalsey in interface io.jstach.jstachio.context.Internal.ObjectContextNode
      Returns:
      true if falsey.
    • ofChild

      default @Nullable ContextNode ofChild(String name, @Nullable Object o) throws IllegalArgumentException
      Creates a named child node off of this node where the return child nodes parent will be this node.
      Parameters:
      name - the context name.
      o - the object to be wrapped.
      Returns:
      null if the child object is null otherwise a new child node.
      Throws:
      IllegalArgumentException - if the input object is a ContextNode
    • ofChild

      default @Nullable ContextNode ofChild(int index, @Nullable Object o)
      Creates an indexed child node off of this node where the return child nodes parent will be this node.
      Parameters:
      index - a numeric index
      o - the object to be wrapped. Maybe null.
      Returns:
      null if the child object is null otherwise a new child node.
      Throws:
      IllegalArgumentException - if the input object is a ContextNode
      API Note
      there is no checking to see if the same index is reused as the parent knows nothing of the child.