Class ArrayNode
- java.lang.Object
-
- software.amazon.smithy.model.node.Node
-
- software.amazon.smithy.model.node.ArrayNode
-
- All Implemented Interfaces:
java.lang.Iterable<Node>
,FromSourceLocation
,ToNode
,ToSmithyBuilder<ArrayNode>
public final class ArrayNode extends Node implements java.lang.Iterable<Node>, ToSmithyBuilder<ArrayNode>
Represents an array of nodes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ArrayNode.Builder
Builder used to efficiently create an ArrayNode.-
Nested classes/interfaces inherited from class software.amazon.smithy.model.node.Node
Node.NonNumericFloat
-
-
Constructor Summary
Constructors Constructor Description ArrayNode(java.util.List<Node> elements, SourceLocation sourceLocation)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> R
accept(NodeVisitor<R> visitor)
Accepts a visitor with the node.java.util.Optional<ArrayNode>
asArrayNode()
Gets the node as an ArrayNode if it is an array.static ArrayNode.Builder
builder()
static <T extends ToNode>
java.util.stream.Collector<T,java.util.List<Node>,ArrayNode>collect()
static <T extends ToNode>
java.util.stream.Collector<T,java.util.List<Node>,ArrayNode>collect(SourceLocation sloc)
boolean
equals(java.lang.Object other)
ArrayNode
expectArrayNode(java.lang.String errorMessage)
Casts the current node to anArrayNode
, throwingExpectationNotMetException
when the node is the wrong type.ArrayNode
expectArrayNode(java.util.function.Supplier<java.lang.String> errorMessage)
Casts the current node to anArrayNode
, throwingExpectationNotMetException
when the node is the wrong type.java.util.Optional<Node>
get(int index)
Gets a node from the given index.java.util.List<Node>
getElements()
Gets the list of nodes.<T extends Node>
java.util.List<T>getElementsAs(java.lang.Class<T> type)
Gets the elements of the array as a list of a specific type ofNode
.<T,K extends Node>
java.util.List<T>getElementsAs(java.util.function.Function<K,T> f)
Gets the elements of the ArrayNode as a specific type by applying a mapping function to each node.NodeType
getType()
Gets the type of the node.int
hashCode()
boolean
isEmpty()
Returns true if the array node is empty.java.util.Iterator<Node>
iterator()
ArrayNode
merge(ArrayNode other)
Merges two ArrayNodes into a new ArrayNode.int
size()
Returns the number of elements in the array node.ArrayNode.Builder
toBuilder()
Take this object and create a builder that contains all of the current property values of this object.ArrayNode
withValue(Node node)
Copies the values from the current array node, adding the given node, returning them in a newArrayNode
.-
Methods inherited from class software.amazon.smithy.model.node.Node
arrayNode, arrayNode, asBooleanNode, asNullNode, asNumberNode, asObjectNode, assertEquals, asStringNode, diff, expectArrayNode, expectBooleanNode, expectBooleanNode, expectBooleanNode, expectNullNode, expectNullNode, expectNullNode, expectNumberNode, expectNumberNode, expectNumberNode, expectObjectNode, expectObjectNode, expectObjectNode, expectStringNode, expectStringNode, expectStringNode, from, from, from, from, fromNodes, fromNodes, fromStrings, fromStrings, getSourceLocation, isArrayNode, isBooleanNode, isNullNode, isNumberNode, isObjectNode, isStringNode, loadArrayOfString, nullNode, objectNode, objectNode, objectNodeBuilder, parse, parse, parse, parse, parseJsonWithComments, parseJsonWithComments, prettyPrintJson, prettyPrintJson, printJson, toNode, withDeepSortedKeys, withDeepSortedKeys
-
-
-
-
Constructor Detail
-
ArrayNode
public ArrayNode(java.util.List<Node> elements, SourceLocation sourceLocation)
-
-
Method Detail
-
builder
public static ArrayNode.Builder builder()
-
toBuilder
public ArrayNode.Builder toBuilder()
Description copied from interface:ToSmithyBuilder
Take this object and create a builder that contains all of the current property values of this object.- Specified by:
toBuilder
in interfaceToSmithyBuilder<ArrayNode>
- Returns:
- a builder for type T
-
iterator
public java.util.Iterator<Node> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<Node>
-
accept
public <R> R accept(NodeVisitor<R> visitor)
Description copied from class:Node
Accepts a visitor with the node.
-
expectArrayNode
public ArrayNode expectArrayNode(java.lang.String errorMessage)
Description copied from class:Node
Casts the current node to anArrayNode
, throwingExpectationNotMetException
when the node is the wrong type.- Overrides:
expectArrayNode
in classNode
- Parameters:
errorMessage
- Error message to use if the node is of the wrong type.- Returns:
- Returns an array node.
-
expectArrayNode
public ArrayNode expectArrayNode(java.util.function.Supplier<java.lang.String> errorMessage)
Description copied from class:Node
Casts the current node to anArrayNode
, throwingExpectationNotMetException
when the node is the wrong type.- Overrides:
expectArrayNode
in classNode
- Parameters:
errorMessage
- Error message supplier.- Returns:
- Returns an array node.
-
asArrayNode
public java.util.Optional<ArrayNode> asArrayNode()
Description copied from class:Node
Gets the node as an ArrayNode if it is an array.- Overrides:
asArrayNode
in classNode
- Returns:
- Returns the optional array node.
-
getElements
public java.util.List<Node> getElements()
Gets the list of nodes.- Returns:
- Returns a list of nodes.
-
get
public java.util.Optional<Node> get(int index)
Gets a node from the given index.- Parameters:
index
- Index of the value to get.- Returns:
- Returns an optional node at the given index.
-
isEmpty
public boolean isEmpty()
Returns true if the array node is empty.- Returns:
- Returns true if the array node is empty.
-
size
public int size()
Returns the number of elements in the array node.- Returns:
- Returns the number of elements in the array node.
-
withValue
public ArrayNode withValue(Node node)
Copies the values from the current array node, adding the given node, returning them in a newArrayNode
.- Parameters:
node
- The node to add.- Returns:
- Returns a new array node.
-
getElementsAs
public <T extends Node> java.util.List<T> getElementsAs(java.lang.Class<T> type)
Gets the elements of the array as a list of a specific type ofNode
.- Type Parameters:
T
- Type of Node to expect in each position.- Parameters:
type
- Type of Node to expect in each position.- Returns:
- Returns the List of the specified type.
- Throws:
ExpectationNotMetException
- if the list contains elements of a different type.
-
getElementsAs
public <T,K extends Node> java.util.List<T> getElementsAs(java.util.function.Function<K,T> f)
Gets the elements of the ArrayNode as a specific type by applying a mapping function to each node.Each Node is cast to
K
and then mapped with the provided function to return typeT
.ArrayNode array = Node.fromStrings("foo", "baz", "bar"); List<String> strings = array.getElementsAs(StringNode::getValue);
- Type Parameters:
K
- Expected Node type.T
- Mapping function return value.- Parameters:
f
- Mapping function that takesK
and returnT
.- Returns:
- Returns the List of type T.
- Throws:
ExpectationNotMetException
- if the list contains elements of a different type thanK
.
-
merge
public ArrayNode merge(ArrayNode other)
Merges two ArrayNodes into a new ArrayNode.If the current node has a source location, it is applied to the result. Otherwise, the source location of
other
is applied to the result.- Parameters:
other
- Node to merge with.- Returns:
- Returns a new merged array node.
-
collect
public static <T extends ToNode> java.util.stream.Collector<T,java.util.List<Node>,ArrayNode> collect()
- Type Parameters:
T
- Type of value in the collection.- Returns:
- Creates a collector that create an ArrayNode.
-
collect
public static <T extends ToNode> java.util.stream.Collector<T,java.util.List<Node>,ArrayNode> collect(SourceLocation sloc)
- Type Parameters:
T
- Type of value in the collection.- Parameters:
sloc
- Source location to use on the created node.- Returns:
- Creates a collector that create an ArrayNode.
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-