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:
-
Field Summary
Fields inherited from interface io.jstach.jstachio.context.ContextNode
CONTEXT_BINDING_NAME
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal @Nullable ContextNode
Will search up the tree for a field starting at this nodes children first.final @Nullable ContextNode
Gets a field from a ContextNode.abstract @Nullable Object
Get a value by key.final boolean
isFalsey()
Determines if the node is falsey.final Iterator<@Nullable ContextNode>
iterator()
If the node is a Map or a non iterable/array a singleton iterator will be returned.final Object
object()
The object being wrapped.default @Nullable ContextNode
Creates an indexed child node off of this node where the return child nodes parent will be this node.default @Nullable ContextNode
Creates a named child node off of this node where the return child nodes parent will be this node.final @Nullable ContextNode
parent()
The parent node.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.jstach.jstachio.context.ContextNode
renderString
Methods inherited from interface io.jstach.jstachio.context.Internal.ObjectContextNode
format
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
ObjectContext
protected ObjectContext()Do nothing constructor
-
-
Method Details
-
getValue
Get a value by key. This is analagous toMap.get(Object)
.- Specified by:
getValue
in interfaceio.jstach.jstachio.context.Internal.ObjectContextNode
- Parameters:
key
- not null- Returns:
- value mapped to key.
-
object
Description copied from interface:ContextNode
The object being wrapped.- Specified by:
object
in interfaceContextNode
- Returns:
- the Map, Iterable or object that was wrapped. Never
null
.
-
get
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 aMap
and use it to return a child context node. Just likeMap
null
will be returned if no field is found.- Specified by:
get
in interfaceContextNode
- Specified by:
get
in interfaceio.jstach.jstachio.context.Internal.ObjectContextNode
- Parameters:
field
- the name of the field- Returns:
- a new child node. Maybe
null
.
-
find
Description copied from interface:ContextNode
Will search up the tree for a field starting at this nodes children first.- Specified by:
find
in interfaceContextNode
- 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
Description copied from interface:ContextNode
The parent node.- Specified by:
parent
in interfaceContextNode
- Returns:
- the parent node or
null
if this is the root.
-
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 interfaceContextNode
- Specified by:
iterator
in interfaceio.jstach.jstachio.context.Internal.ObjectContextNode
- Specified by:
iterator
in interfaceIterable<@Nullable ContextNode>
- Returns:
- lazy iterator of context nodes.
-
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 ifContextNode.iterator()
has any next elements and if it does not it is falsey.- Specified by:
isFalsey
in interfaceContextNode
- Specified by:
isFalsey
in interfaceio.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 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:
null
if 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.
-