Class Node
- java.lang.Object
-
- software.amazon.smithy.model.node.Node
-
- All Implemented Interfaces:
FromSourceLocation,ToNode
- Direct Known Subclasses:
ArrayNode,BooleanNode,NullNode,NumberNode,ObjectNode,StringNode
public abstract class Node extends java.lang.Object implements FromSourceLocation, ToNode
Base class of for all Smithy model nodes.When loading a Smithy model the data is loaded from the source model file into a tree of nodes. These nodes represent the unvalidated structure of the model document.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNode.NonNumericFloatNon-numeric values for floats and doubles.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract <R> Raccept(NodeVisitor<R> visitor)Accepts a visitor with the node.static ArrayNodearrayNode()Creates an emptyArrayNode.static ArrayNodearrayNode(Node... nodes)Creates anArrayNodefrom a variadic list of Nodes.java.util.Optional<ArrayNode>asArrayNode()Gets the node as an ArrayNode if it is an array.java.util.Optional<BooleanNode>asBooleanNode()Gets the node as an BooleanNode if it is an boolean.java.util.Optional<NullNode>asNullNode()Gets the node as an NullNode if it is a null.java.util.Optional<NumberNode>asNumberNode()Gets the node as an NumberNode if it is an number.java.util.Optional<ObjectNode>asObjectNode()Gets the node as an ObjectNode if it is an object.static voidassertEquals(ToNode actual, ToNode expected)Testing helper used to compare two Nodes for equivalence.java.util.Optional<StringNode>asStringNode()Gets the node as an StringNode if it is an string.static java.util.List<java.lang.String>diff(ToNode actual, ToNode expected)Computes the differences between two Nodes as a String.ArrayNodeexpectArrayNode()Casts the current node to anArrayNode.ArrayNodeexpectArrayNode(java.lang.String message)Casts the current node to anArrayNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.ArrayNodeexpectArrayNode(java.util.function.Supplier<java.lang.String> message)Casts the current node to anArrayNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.BooleanNodeexpectBooleanNode()Casts the current node to aBooleanNode.BooleanNodeexpectBooleanNode(java.lang.String message)Casts the current node to aBooleanNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.BooleanNodeexpectBooleanNode(java.util.function.Supplier<java.lang.String> message)Casts the current node to aBooleanNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.NullNodeexpectNullNode()Casts the current node to aNullNode.NullNodeexpectNullNode(java.lang.String message)Casts the current node to aNullNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.NullNodeexpectNullNode(java.util.function.Supplier<java.lang.String> message)Casts the current node to aNullNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.NumberNodeexpectNumberNode()Casts the current node to aNumberNode.NumberNodeexpectNumberNode(java.lang.String message)Casts the current node to aNumberNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.NumberNodeexpectNumberNode(java.util.function.Supplier<java.lang.String> message)Casts the current node to aNumberNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.ObjectNodeexpectObjectNode()Casts the current node to anObjectNode.ObjectNodeexpectObjectNode(java.lang.String message)Casts the current node to anObjectNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.ObjectNodeexpectObjectNode(java.util.function.Supplier<java.lang.String> message)Casts the current node to anObjectNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.StringNodeexpectStringNode()Casts the current node to aStringNode.StringNodeexpectStringNode(java.lang.String message)Casts the current node to aStringNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.StringNodeexpectStringNode(java.util.function.Supplier<java.lang.String> message)Casts the current node to aStringNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.static BooleanNodefrom(boolean value)Create aBooleanNodefrom a boolean value.static NumberNodefrom(java.lang.Number number)Create aNumberNodefrom a Number value.static StringNodefrom(java.lang.String value)Create aStringNodefrom a String value.static Nodefrom(ToNode value)Create a Node from a potentially nullToNodevalue.static ArrayNodefromNodes(java.util.List<? extends Node> values)Creates anArrayNodefrom a Collection of Node values.static ArrayNodefromNodes(Node... values)Creates anArrayNodefrom a variadic list of Node values.static ArrayNodefromStrings(java.lang.String... values)Creates anArrayNodefrom a variadic list of String values.static ArrayNodefromStrings(java.util.Collection<java.lang.String> values)Creates anArrayNodefrom a Collection of String values.SourceLocationgetSourceLocation()Gets the source location of a value.abstract NodeTypegetType()Gets the type of the node.booleanisArrayNode()Checks if this node is an array type.booleanisBooleanNode()Checks if this node is a boolean type.booleanisNullNode()Checks if this node is a null type.booleanisNumberNode()Checks if this node is a number type.booleanisObjectNode()Checks if this node is an object type.booleanisStringNode()Checks if this node is a string type.static java.util.List<java.lang.String>loadArrayOfString(java.lang.String descriptor, Node node)Expects an array of strings and returns the loaded strings.static NullNodenullNode()Creates aNullNode.static ObjectNodeobjectNode()Creates an emptyObjectNode.static ObjectNodeobjectNode(java.util.Map<StringNode,Node> values)Creates anObjectNodefrom the given map of Nodes.static ObjectNode.BuilderobjectNodeBuilder()Creates anObjectNode.Builder.static Nodeparse(java.io.InputStream json)Attempts to parse the given JSON input stream and returns a Node.static Nodeparse(java.io.InputStream json, java.lang.String file)Attempts to parse the given JSON input stream and returns a Node.static Nodeparse(java.lang.String json)Attempts to parse the given JSON string and return a Node.static Nodeparse(java.lang.String json, java.lang.String file)Attempts to parse the given JSON string and File Name and return a Node.static NodeparseJsonWithComments(java.lang.String json)Attempts to parse the given JSON string and return a Node.static NodeparseJsonWithComments(java.lang.String json, java.lang.String file)Attempts to parse the given JSON string and File Name and return a Node.static java.lang.StringprettyPrintJson(Node node)Writes the contents of a Node to a pretty-printed JSON string.static java.lang.StringprettyPrintJson(Node node, java.lang.String indentString)Writes the contents of a Node to a pretty-printed JSON string.static java.lang.StringprintJson(Node node)Writes the contents of a Node to a non-pretty-printed JSON string.NodetoNode()Converts a value to aNode.NodewithDeepSortedKeys()Returns a node with sorted keys and sorted keys of all nested object nodes.NodewithDeepSortedKeys(java.util.Comparator<StringNode> keyComparator)Returns a node with sorted keys and sorted keys of all nested object nodes using a custom comparator.
-
-
-
Method Detail
-
parse
public static Node parse(java.lang.String json)
Attempts to parse the given JSON string and return a Node.- Parameters:
json- JSON text to parse.- Returns:
- Returns the parsed Node on success.
- Throws:
ModelSyntaxException- if the JSON text is invalid.
-
parse
public static Node parse(java.lang.String json, java.lang.String file)
Attempts to parse the given JSON string and File Name and return a Node.- Parameters:
json- JSON text to parse.file- Filename corresponding to json text- Returns:
- Returns the parsed Node on success.
- Throws:
ModelSyntaxException- if the JSON text is invalid.
-
parse
public static Node parse(java.io.InputStream json)
Attempts to parse the given JSON input stream and returns a Node.- Parameters:
json- JSON input stream to parse. The input stream is closed automatically when the content is fully parsed.- Returns:
- Returns the parsed Node on success.
- Throws:
ModelSyntaxException- if the JSON text is invalid.
-
parse
public static Node parse(java.io.InputStream json, java.lang.String file)
Attempts to parse the given JSON input stream and returns a Node.- Parameters:
json- JSON input stream to parse. The input stream is closed automatically when the content is fully parsed.file- Filename corresponding to json text- Returns:
- Returns the parsed Node on success.
- Throws:
ModelSyntaxException- if the JSON text is invalid.
-
parseJsonWithComments
public static Node parseJsonWithComments(java.lang.String json, java.lang.String file)
Attempts to parse the given JSON string and File Name and return a Node.This parser allows for comments in the JSON.
- Parameters:
json- JSON text to parse.file- Filename corresponding to json text- Returns:
- Returns the parsed Node on success.
- Throws:
ModelSyntaxException- if the JSON text is invalid.
-
parseJsonWithComments
public static Node parseJsonWithComments(java.lang.String json)
Attempts to parse the given JSON string and return a Node.This parser allows for comments in the JSON.
- Parameters:
json- JSON text to parse.- Returns:
- Returns the parsed Node on success.
- Throws:
ModelSyntaxException- if the JSON text is invalid.
-
prettyPrintJson
public static java.lang.String prettyPrintJson(Node node)
Writes the contents of a Node to a pretty-printed JSON string.- Parameters:
node- Node to write.- Returns:
- Returns the serialized Node.
-
prettyPrintJson
public static java.lang.String prettyPrintJson(Node node, java.lang.String indentString)
Writes the contents of a Node to a pretty-printed JSON string.- Parameters:
node- Node to write.indentString- String to use for indention.- Returns:
- Returns the serialized Node.
-
printJson
public static java.lang.String printJson(Node node)
Writes the contents of a Node to a non-pretty-printed JSON string.- Parameters:
node- Node to write.- Returns:
- Returns the serialized Node.
-
from
public static StringNode from(java.lang.String value)
Create aStringNodefrom a String value.- Parameters:
value- Value to create a node from.- Returns:
- Returns the created StringNode.
-
from
public static NumberNode from(java.lang.Number number)
Create aNumberNodefrom a Number value.- Parameters:
number- Value to create a node from.- Returns:
- Returns the created NumberNode.
-
from
public static BooleanNode from(boolean value)
Create aBooleanNodefrom a boolean value.- Parameters:
value- Value to create a node from.- Returns:
- Returns the created BooleanNode.
-
from
public static Node from(ToNode value)
Create a Node from a potentially nullToNodevalue.- Parameters:
value- Value to create a node from.- Returns:
- Returns the created Node.
-
fromNodes
public static ArrayNode fromNodes(java.util.List<? extends Node> values)
Creates anArrayNodefrom a Collection of Node values.- Parameters:
values- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
fromNodes
public static ArrayNode fromNodes(Node... values)
Creates anArrayNodefrom a variadic list of Node values.- Parameters:
values- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
fromStrings
public static ArrayNode fromStrings(java.util.Collection<java.lang.String> values)
Creates anArrayNodefrom a Collection of String values.- Parameters:
values- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
fromStrings
public static ArrayNode fromStrings(java.lang.String... values)
Creates anArrayNodefrom a variadic list of String values.- Parameters:
values- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
objectNodeBuilder
public static ObjectNode.Builder objectNodeBuilder()
Creates anObjectNode.Builder.- Returns:
- Returns the ObjectNode builder.
-
objectNode
public static ObjectNode objectNode()
Creates an emptyObjectNode.- Returns:
- Returns the ObjectNode.
-
objectNode
public static ObjectNode objectNode(java.util.Map<StringNode,Node> values)
Creates anObjectNodefrom the given map of Nodes.- Parameters:
values- Values to add to the object node.- Returns:
- Returns the created ObjectNode.
-
arrayNode
public static ArrayNode arrayNode()
Creates an emptyArrayNode.- Returns:
- Returns the ArrayNode.
-
arrayNode
public static ArrayNode arrayNode(Node... nodes)
Creates anArrayNodefrom a variadic list of Nodes.- Parameters:
nodes- Nodes to add to the array.- Returns:
- Returns the created ArrayNode.
-
loadArrayOfString
public static java.util.List<java.lang.String> loadArrayOfString(java.lang.String descriptor, Node node)Expects an array of strings and returns the loaded strings.- Parameters:
descriptor- Name of the property being loaded.node- Node to load.- Returns:
- Returns the loaded strings.
- Throws:
SourceException- on error.
-
assertEquals
public static void assertEquals(ToNode actual, ToNode expected)
Testing helper used to compare two Nodes for equivalence.Compares two Node values and throws if they aren't equal. The thrown exception contains a message that shows the differences between the two Nodes as returned by
diff(ToNode, ToNode).- Parameters:
actual- Node to use as the starting node.expected- Node to compare against.- Throws:
ExpectationNotMetException- if the nodes are not equivalent.
-
diff
public static java.util.List<java.lang.String> diff(ToNode actual, ToNode expected)
Computes the differences between two Nodes as a String.- Parameters:
actual- Node to use as the starting node.expected- Node to compare against.- Returns:
- Returns the differences as a String.
-
getType
public abstract NodeType getType()
Gets the type of the node.- Returns:
- Returns the node type.
-
accept
public abstract <R> R accept(NodeVisitor<R> visitor)
Accepts a visitor with the node.- Type Parameters:
R- visitor return type.- Parameters:
visitor- Visitor to dispatch to.- Returns:
- Returns the accepted result.
-
isObjectNode
public final boolean isObjectNode()
Checks if this node is an object type.- Returns:
- Returns true if this node is an object type.
-
isArrayNode
public final boolean isArrayNode()
Checks if this node is an array type.- Returns:
- Returns true if this node is an array type.
-
isStringNode
public final boolean isStringNode()
Checks if this node is a string type.- Returns:
- Returns true if this node is a string type.
-
isNumberNode
public final boolean isNumberNode()
Checks if this node is a number type.- Returns:
- Returns true if this node is a number type.
-
isBooleanNode
public final boolean isBooleanNode()
Checks if this node is a boolean type.- Returns:
- Returns true if this node is a boolean type.
-
isNullNode
public final boolean isNullNode()
Checks if this node is a null type.- Returns:
- Returns true if this node is a null type.
-
asObjectNode
public java.util.Optional<ObjectNode> asObjectNode()
Gets the node as an ObjectNode if it is an object.- Returns:
- Returns the optional object node.
-
asArrayNode
public java.util.Optional<ArrayNode> asArrayNode()
Gets the node as an ArrayNode if it is an array.- Returns:
- Returns the optional array node.
-
asStringNode
public java.util.Optional<StringNode> asStringNode()
Gets the node as an StringNode if it is an string.- Returns:
- Returns the optional StringNode.
-
asBooleanNode
public java.util.Optional<BooleanNode> asBooleanNode()
Gets the node as an BooleanNode if it is an boolean.- Returns:
- Returns the optional BooleanNode.
-
asNumberNode
public java.util.Optional<NumberNode> asNumberNode()
Gets the node as an NumberNode if it is an number.- Returns:
- Returns the optional NumberNode.
-
asNullNode
public java.util.Optional<NullNode> asNullNode()
Gets the node as an NullNode if it is a null.- Returns:
- Returns the optional NullNode.
-
expectObjectNode
public final ObjectNode expectObjectNode()
Casts the current node to anObjectNode.- Returns:
- Returns an object node.
- Throws:
ExpectationNotMetException- when the node is not anObjectNode.
-
expectObjectNode
public ObjectNode expectObjectNode(java.lang.String message)
Casts the current node to anObjectNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message to use if the node is of the wrong type.- Returns:
- Returns an object node.
- Throws:
ExpectationNotMetException- when the node is not anObjectNode.
-
expectObjectNode
public ObjectNode expectObjectNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to anObjectNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message supplier.- Returns:
- Returns an object node.
- Throws:
ExpectationNotMetException- when the node is not anObjectNode.
-
expectArrayNode
public final ArrayNode expectArrayNode()
Casts the current node to anArrayNode.- Returns:
- Returns an array node.
- Throws:
ExpectationNotMetException- when the node is not anArrayNode.
-
expectArrayNode
public ArrayNode expectArrayNode(java.lang.String message)
Casts the current node to anArrayNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message to use if the node is of the wrong type.- Returns:
- Returns an array node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectArrayNode
public ArrayNode expectArrayNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to anArrayNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message supplier.- Returns:
- Returns an array node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectStringNode
public final StringNode expectStringNode()
Casts the current node to aStringNode.- Returns:
- Returns a string node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectStringNode
public StringNode expectStringNode(java.lang.String message)
Casts the current node to aStringNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message to use if the node is of the wrong type.- Returns:
- Returns a string node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectStringNode
public StringNode expectStringNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to aStringNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message supplier.- Returns:
- Returns a string node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectNumberNode
public final NumberNode expectNumberNode()
Casts the current node to aNumberNode.- Returns:
- Returns a number node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectNumberNode
public NumberNode expectNumberNode(java.lang.String message)
Casts the current node to aNumberNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message to use if the node is of the wrong type.- Returns:
- Returns a number node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectNumberNode
public NumberNode expectNumberNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to aNumberNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message supplier.- Returns:
- Returns a number node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectBooleanNode
public final BooleanNode expectBooleanNode()
Casts the current node to aBooleanNode.- Returns:
- Returns a boolean node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectBooleanNode
public BooleanNode expectBooleanNode(java.lang.String message)
Casts the current node to aBooleanNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message to use if the node is of the wrong type.- Returns:
- Returns a boolean node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectBooleanNode
public BooleanNode expectBooleanNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to aBooleanNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message supplier.- Returns:
- Returns a boolean node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectNullNode
public final NullNode expectNullNode()
Casts the current node to aNullNode.- Returns:
- Returns a null node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectNullNode
public NullNode expectNullNode(java.lang.String message)
Casts the current node to aNullNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message to use if the node is of the wrong type.- Returns:
- Returns a null node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectNullNode
public NullNode expectNullNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to aNullNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message supplier.- Returns:
- Returns a null node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
getSourceLocation
public final SourceLocation getSourceLocation()
Description copied from interface:FromSourceLocationGets the source location of a value.- Specified by:
getSourceLocationin interfaceFromSourceLocation- Returns:
- Returns the source location of the value.
-
toNode
public final Node toNode()
Description copied from interface:ToNodeConverts a value to aNode.
-
withDeepSortedKeys
public final Node withDeepSortedKeys()
Returns a node with sorted keys and sorted keys of all nested object nodes.- Returns:
- Returns the node in which all object nodes have sorted keys.
-
withDeepSortedKeys
public final Node withDeepSortedKeys(java.util.Comparator<StringNode> keyComparator)
Returns a node with sorted keys and sorted keys of all nested object nodes using a custom comparator.- Parameters:
keyComparator- Compares keys.- Returns:
- Returns the node in which all object nodes have sorted keys.
-
-