- All Superinterfaces:
Iterable<ContextNode>
This interface serves two puproses:
- A way to represent the current context stack (see
parent()) - Allow you to simulate JSON/Javscript object node like trees without being coupled to a particularly JSON lib.
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 TypeMethodDescriptiondefault @Nullable ContextNodeWill search up the tree for a field starting at this nodes children first.default @Nullable ContextNodeGets a field from aMapif ContextNode is wrapping one.static booleanDetermines if an object is falsey based on mustache spec semantics where:null, empty iterables, empty arrays and booleanfalseare falsey.default Iterator<ContextNode>iterator()If the node is a Map or a non iterable/array a singleton iterator will be returned.object()The object being wrapped.default @Nullable ContextNodeCreates an indexed child node off of this node where the return child nodes parent will be this node.default @Nullable ContextNodeCreates a named child node off of this node where the return child nodes parent will be this node.static @Nullable ContextNodeCreates the root node which has no name.default @Nullable ContextNodeparent()The parent node.default StringConvenience method for callingtoStringon the wrapped object.Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
ofRoot
Creates the root node which has no name.- Parameters:
o- the object to be wrapped. Maybenull.- Returns:
nullif 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:
nullif the child object is null otherwise a new child node.- Throws:
IllegalArgumentException- if the input object is aContextNode
-
ofChild
Creates an indexed child node off of this node where the return child nodes parent will be this node.- Parameters:
index- a numeric indexo- the object to be wrapped. Maybenull.- Returns:
nullif the child object is null otherwise a new child node.- Throws:
IllegalArgumentException- if the input object is aContextNode- API Note
- there is no checking to see if the same index is reused as the parent knows nothing of the child.
-
get
Gets a field from aMapif ContextNode is wrapping one. This is direct access (end of a dotted path) and does not check the parents. Just likeMapnullwill be returned if no field is found.- Parameters:
field- the name of the field- Returns:
- a new child node. Maybe
null.
-
find
Will search up the tree for a field starting at this nodes children first.- Parameters:
field- context name (e.g. section name)- Returns:
nullif 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
Convenience method for callingtoStringon the wrapped object.- Returns:
- a toString on the wrapped object.
-
parent
The parent node.- Returns:
- the parent node or
nullif 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:
iteratorin interfaceIterable<ContextNode>- Returns:
- lazy iterator of context nodes.
-
isFalsey
Determines if an object is falsey based on mustache spec semantics where:null, empty iterables, empty arrays and booleanfalseare falsey.- Parameters:
context- a context object. ContextNode are allowed as input as well asnull.- Returns:
- true if the object is falsey.
-