Interface ContextNode

All Superinterfaces:
Iterable<ContextNode>

public interface ContextNode extends Iterable<ContextNode>
This interface serves two puproses:
  1. A way to represent the current context stack (see parent())
  2. Allow you to simulate JSON/Javscript object node like trees without being coupled to a particularly JSON lib.
The interface simply wraps Map and Iterable (and arrays) lazily through composition but generally cannot wrap other context nodes. If an object is wrapped that is not a Map or Iterable it becomes a leaf node similar to JSON.

It is not recommended you use this interface as it avoids much of the type checking safty of this library as well as increase coupling however it does provide a slightly better bridge to legacy Map<String,?> models over using the maps directly.

Author:
agentgt
API Note
The parents do not know anything about their children as it is the child that has reference to the parent.
  • Method Summary

    Modifier and Type
    Method
    Description
    default @Nullable ContextNode
    find(String field)
    Will search up the tree for a field starting at this nodes children first.
    default @Nullable ContextNode
    get(String field)
    Gets a field from a Map if ContextNode is wrapping one.
    static boolean
    isFalsey(@Nullable Object context)
    Determines if an object is falsey based on mustache spec semantics where: null, empty iterables, empty arrays and boolean false are falsey.
    If the node is a Map or a non iterable/array a singleton iterator will be returned.
    The object being wrapped.
    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.
    default @Nullable ContextNode
    ofChild(String name, @Nullable Object o)
    Creates a named child node off of this node where the return child nodes parent will be this node.
    static @Nullable ContextNode
    ofRoot(@Nullable Object o)
    Creates the root node which has no name.
    default @Nullable ContextNode
    The parent node.
    default String
    Convenience method for calling toString on the wrapped object.

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Method Details

    • ofRoot

      static @Nullable ContextNode ofRoot(@Nullable Object o)
      Creates the root node which has no name.
      Parameters:
      o - the object to be wrapped. Maybe null.
      Returns:
      null if the root object is null otherwise a new root node.
      API Note
      Unlike the other methods in this class if the passed in object is a context node it is simply returned if it is a root node otherwise it is rewrapped.
    • 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.
    • get

      default @Nullable ContextNode get(String field)
      Gets a field from a Map if ContextNode is wrapping one. This is direct access (end of a dotted path) and does not check the parents. Just like Map null will be returned if no field is found.
      Parameters:
      field - the name of the field
      Returns:
      a new child node. Maybe null.
    • find

      default @Nullable ContextNode find(String field)
      Will search up the tree for a field starting at this nodes children first.
      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.
    • object

      The object being wrapped.
      Returns:
      the Map, Iterable or object that was wrapped. Never null.
    • renderString

      default String renderString()
      Convenience method for calling toString on the wrapped object.
      Returns:
      a toString on the wrapped object.
    • parent

      default @Nullable ContextNode parent()
      The parent node.
      Returns:
      the parent node or null if this is the root.
    • iterator

      If the node is a Map or a non iterable/array a singleton iterator will be returned. Otherwise if it is an interable/array new child context nodes will be created lazily.
      Specified by:
      iterator in interface Iterable<ContextNode>
      Returns:
      lazy iterator of context nodes.
    • isFalsey

      static boolean isFalsey(@Nullable Object context)
      Determines if an object is falsey based on mustache spec semantics where: null, empty iterables, empty arrays and boolean false are falsey.
      Parameters:
      context - a context object. ContextNode are allowed as input as well as null.
      Returns:
      true if the object is falsey.