Class ObjectNode

java.lang.Object
software.amazon.smithy.model.node.Node
software.amazon.smithy.model.node.ObjectNode
All Implemented Interfaces:
FromSourceLocation, ToNode, ToSmithyBuilder<ObjectNode>

public final class ObjectNode extends Node implements ToSmithyBuilder<ObjectNode>
Represents an object node.
  • Constructor Details

  • Method Details

    • fromStringMap

      public static ObjectNode fromStringMap(Map<String,String> map)
    • builder

      public static ObjectNode.Builder builder()
    • getType

      public NodeType getType()
      Description copied from class: Node
      Gets the type of the node.
      Specified by:
      getType in class Node
      Returns:
      Returns the node type.
    • accept

      public <R> R accept(NodeVisitor<R> visitor)
      Description copied from class: Node
      Accepts a visitor with the node.
      Specified by:
      accept in class Node
      Type Parameters:
      R - visitor return type.
      Parameters:
      visitor - Visitor to dispatch to.
      Returns:
      Returns the accepted result.
    • expectObjectNode

      public ObjectNode expectObjectNode(String errorMessage)
      Description copied from class: Node
      Casts the current node to an ObjectNode, throwing ExpectationNotMetException when the node is the wrong type.
      Overrides:
      expectObjectNode in class Node
      Parameters:
      errorMessage - Error message to use if the node is of the wrong type.
      Returns:
      Returns an object node.
    • expectObjectNode

      public ObjectNode expectObjectNode(Supplier<String> errorMessage)
      Description copied from class: Node
      Casts the current node to an ObjectNode, throwing ExpectationNotMetException when the node is the wrong type.
      Overrides:
      expectObjectNode in class Node
      Parameters:
      errorMessage - Error message supplier.
      Returns:
      Returns an object node.
    • asObjectNode

      public Optional<ObjectNode> asObjectNode()
      Description copied from class: Node
      Gets the node as an ObjectNode if it is an object.
      Overrides:
      asObjectNode in class Node
      Returns:
      Returns the optional object node.
    • withMember

      public <T extends ToNode> ObjectNode withMember(StringNode key, T value)
      Constructs a new object node with the given member added.
      Type Parameters:
      T - Type of the value member to add.
      Parameters:
      key - Name of the member to add.
      value - Value of the member to add.
      Returns:
      Returns a new object node.
    • withMember

      public <T extends ToNode> ObjectNode withMember(String key, T value)
      Constructs a new object node with the given member added.
      Type Parameters:
      T - Type of the value member to add.
      Parameters:
      key - Name of the member to add as a string.
      value - Value of the member to add.
      Returns:
      Returns a new object node.
    • withMember

      public ObjectNode withMember(String key, String value)
      Constructs a new object node with the given member added.
      Parameters:
      key - Name of the member to add as a string.
      value - Value of the member to add.
      Returns:
      Returns a new object node.
    • withMember

      public ObjectNode withMember(String key, boolean value)
      Constructs a new object node with the given member added.
      Parameters:
      key - Name of the member to add as a string.
      value - Value of the member to add.
      Returns:
      Returns a new object node.
    • withMember

      public ObjectNode withMember(String key, Number value)
      Constructs a new object node with the given member added.
      Parameters:
      key - Name of the member to add as a string.
      value - Value of the member to add.
      Returns:
      Returns a new object node.
    • withOptionalMember

      public <T extends ToNode> ObjectNode withOptionalMember(String key, Optional<T> value)
      Adds a member to a new ObjectNode if the provided value is present.
      Type Parameters:
      T - Type of the value member to add.
      Parameters:
      key - Key to set if value is present.
      value - Value that may be present.
      Returns:
      Returns an object with the optionally added member.
    • withoutMember

      public ObjectNode withoutMember(String memberName)
      Constructs a new object node from the current node, but without the named member.
      Parameters:
      memberName - Name of a member that should be omitted.
      Returns:
      Returns a new object node.
    • getMembers

      public Map<StringNode,Node> getMembers()
      Gets the map of members.
      Returns:
      Returns a map of nodes.
    • containsMember

      public boolean containsMember(String memberName)
      Checks if the given member name exists in the ObjectNode.
      Parameters:
      memberName - Member name to check.
      Returns:
      Returns true if this member is in the ObjectNode.
    • getMember

      public Optional<Node> getMember(String memberName)
      Gets the member with the given name.
      Parameters:
      memberName - Name of the member to get.
      Returns:
      Returns the optional node with the given member name.
    • getMembersByPrefix

      public Map<String,Node> getMembersByPrefix(String prefix)
      Gets a map of all members where the key starts with the given prefix.
      Parameters:
      prefix - Prefix to search for in keys.
      Returns:
      Returns the map of matching members.
    • getStringMap

      public Map<String,Node> getStringMap()
      Gets an immutable Map<String, Node> that represents the ObjectNode.
      Returns:
      Returns the immutable map.
    • getStringMember

      public Optional<StringNode> getStringMember(String memberName)
      Gets the member with the given name, and if present, expects it to be a string.
      Parameters:
      memberName - Name of the member to get.
      Returns:
      Returns the optional node with the given member name.
      Throws:
      ExpectationNotMetException - if the member is not a string.
    • getStringMemberOrDefault

      public String getStringMemberOrDefault(String memberName, String defaultValue)
      Gets the member with the given name, and if present, expects it to be a string, otherwise returns the default value.
      Parameters:
      memberName - Name of the member to get.
      defaultValue - Default string value to return if the member is not present.
      Returns:
      Returns a String value contained in the String node or the default value.
      Throws:
      ExpectationNotMetException - if the member is not a string.
    • getNumberMember

      public Optional<NumberNode> getNumberMember(String memberName)
      Gets the member with the given name, and if present, expects it to be a number.
      Parameters:
      memberName - Name of the member to get.
      Returns:
      Returns the optional node with the given member name.
      Throws:
      ExpectationNotMetException - if the member is not a number.
    • getNumberMemberOrDefault

      public Number getNumberMemberOrDefault(String memberName, Number defaultValue)
      Gets the member with the given name, and if present, expects it to be a number, otherwise returns the default value.
      Parameters:
      memberName - Name of the member to get.
      defaultValue - Default value to return if a member is not found.
      Returns:
      Returns the Number value or a default value.
      Throws:
      ExpectationNotMetException - if the member is not a number.
    • getArrayMember

      public Optional<ArrayNode> getArrayMember(String memberName)
      Gets the member with the given name, and if present, expects it to be an array.
      Parameters:
      memberName - Name of the member to get.
      Returns:
      Returns the optional node with the given member name.
      Throws:
      ExpectationNotMetException - if the member is not an array.
    • getObjectMember

      public Optional<ObjectNode> getObjectMember(String memberName)
      Gets the member with the given name, and if present, expects it to be an object.
      Parameters:
      memberName - Name of the member to get.
      Returns:
      Returns the optional node with the given member name.
      Throws:
      ExpectationNotMetException - if the member is not an object.
    • getBooleanMember

      public Optional<BooleanNode> getBooleanMember(String memberName)
      Gets the member with the given name, and if present, expects it to be a boolean.
      Parameters:
      memberName - Name of the member to get.
      Returns:
      Returns the optional node with the given member name.
      Throws:
      ExpectationNotMetException - if the member is not a boolean.
    • getBooleanMemberOrDefault

      public Boolean getBooleanMemberOrDefault(String memberName, Boolean defaultValue)
      Gets the member with the given name, and if present, expects it to be a boolean, otherwise returns a default value.
      Parameters:
      memberName - Name of the member to get.
      defaultValue - Default value to return if not present.
      Returns:
      Returns the boolean value or a default value.
      Throws:
      ExpectationNotMetException - if the member is not a boolean.
    • getBooleanMemberOrDefault

      public boolean getBooleanMemberOrDefault(String memberName)
      Gets the member with the given name, and if present, expects it to be a boolean, otherwise returns false.
      Parameters:
      memberName - Name of the member to get.
      Returns:
      Returns the boolean value or false if not found.
      Throws:
      ExpectationNotMetException - if the member is found and not a boolean.
    • expectMember

      public Node expectMember(String name)
      Gets the member with the given name.
      Parameters:
      name - Name of the member to get.
      Returns:
      Returns the node with the given member name.
      Throws:
      IllegalArgumentException - when memberName is null.
      ExpectationNotMetException - when memberName is not present in the members map.
    • expectMember

      public Node expectMember(String name, String errorMessage)
      Gets the member with the given name, throwing ExpectationNotMetException when the member is not present.
      Parameters:
      name - Name of the member to get.
      errorMessage - The error message to use if the expectation is not met.
      Returns:
      Returns the node with the given member name.
      Throws:
      ExpectationNotMetException - when memberName is not present in the members map.
    • expectMember

      public Node expectMember(String name, Supplier<String> errorMessage)
      Gets the member with the given name, throwing ExpectationNotMetException when the member is not present.
      Parameters:
      name - Name of the member to get.
      errorMessage - Error message supplier.
      Returns:
      Returns the node with the given member name.
      Throws:
      ExpectationNotMetException - when memberName is not present in the members map.
    • expectArrayMember

      public ArrayNode expectArrayMember(String name)
      Gets a member and requires it to be an array.
      Parameters:
      name - Name of the member to get.
      Returns:
      Returns the node with the given member name.
      Throws:
      ExpectationNotMetException - when not present or not an array.
    • expectBooleanMember

      public BooleanNode expectBooleanMember(String name)
      Gets a member and requires it to be a boolean.
      Parameters:
      name - Name of the member to get.
      Returns:
      Returns the node with the given member name.
      Throws:
      ExpectationNotMetException - when not present or not a boolean.
    • expectNullMember

      public NullNode expectNullMember(String name)
      Gets a member and requires it to be a null.
      Parameters:
      name - Name of the member to get.
      Returns:
      Returns the node with the given member name.
      Throws:
      ExpectationNotMetException - when not present or not a null.
    • expectNumberMember

      public NumberNode expectNumberMember(String name)
      Gets a member and requires it to be a number.
      Parameters:
      name - Name of the member to get.
      Returns:
      Returns the node with the given member name.
      Throws:
      ExpectationNotMetException - when not present or not a number.
    • expectObjectMember

      public ObjectNode expectObjectMember(String name)
      Gets a member and requires it to be an object.
      Parameters:
      name - Name of the member to get.
      Returns:
      Returns the node with the given member name.
      Throws:
      ExpectationNotMetException - when not present or not an object.
    • expectStringMember

      public StringNode expectStringMember(String name)
      Gets a member and requires it to be a string.
      Parameters:
      name - Name of the member to get.
      Returns:
      Returns the node with the given member name.
      Throws:
      ExpectationNotMetException - when not present or not a string.
    • expectNoAdditionalProperties

      public ObjectNode expectNoAdditionalProperties(Collection<String> allowedProperties)
      Ensures that there are no additional properties other than the provided member names.
      Parameters:
      allowedProperties - Properties that may exist.
      Returns:
      Returns self
      Throws:
      ExpectationNotMetException - if other properties are found.
    • warnIfAdditionalProperties

      public ObjectNode warnIfAdditionalProperties(Collection<String> allowedProperties)
      Warns if unknown properties are found in object.
      Parameters:
      allowedProperties - Properties that may exist.
      Returns:
      Returns self
    • expectMember

      public <T> ObjectNode expectMember(String key, Function<Node,T> mapper, Consumer<T> consumer)
      Requires that the key member is present, passes the value through the given mapper, and then passes the mapped value to consumer.
      Type Parameters:
      T - Mapped value type.
      Parameters:
      key - Key to get from the object.
      mapper - Mapping function used to convert the node value.
      consumer - Consumer to pass the found value to.
      Returns:
      Returns the node.
    • getMember

      public <T> ObjectNode getMember(String key, Function<Node,T> mapper, Consumer<T> consumer)
      The same as expectMember(String, Function, Consumer), but the member is optional.
      Type Parameters:
      T - Mapped value type.
      Parameters:
      key - Key to get from the object.
      mapper - Mapping function used to convert the node value.
      consumer - Consumer to pass the found value to.
      Returns:
      Returns the node.
    • expectObjectMember

      public ObjectNode expectObjectMember(String name, Consumer<ObjectNode> consumer)
      Gets a member and requires it to be an object.
      Parameters:
      name - Name of the member to get.
      consumer - Consumer that accepts the object member.
      Returns:
      Returns the node.
      Throws:
      ExpectationNotMetException - when not present or not an object.
    • getObjectMember

      public ObjectNode getObjectMember(String memberName, Consumer<ObjectNode> consumer)
      Gets the member with the given name, and if present, expects it to be an object.
      Parameters:
      memberName - Name of the member to get.
      consumer - Consumer that accepts the member if found.
      Returns:
      Returns the node.
      Throws:
      ExpectationNotMetException - if the member is not an object.
    • expectStringMember

      public ObjectNode expectStringMember(String key, Consumer<String> consumer)
      Requires that key exists, is a string, and passes the value to consumer.
      Parameters:
      key - Key to retrieve.
      consumer - Consumer that accepts the string value.
      Returns:
      Returns the node.
    • getStringMember

      public ObjectNode getStringMember(String key, Consumer<String> consumer)
      The same as expectStringMember(String, Consumer) but the member is optional.
      Parameters:
      key - Key to retrieve.
      consumer - Consumer that accepts the string value.
      Returns:
      Returns the node.
    • expectBooleanMember

      public ObjectNode expectBooleanMember(String key, Consumer<Boolean> consumer)
      Requires that key exists, is a boolean, and passes the value to consumer.
      Parameters:
      key - Key to retrieve.
      consumer - Consumer that accepts the boolean value.
      Returns:
      Returns the node.
    • getBooleanMember

      public ObjectNode getBooleanMember(String key, Consumer<Boolean> consumer)
      The same as expectBooleanMember(String, Consumer) but the member is optional.
      Parameters:
      key - Key to retrieve.
      consumer - Consumer that accepts the boolean value.
      Returns:
      Returns the node.
    • expectNumberMember

      public ObjectNode expectNumberMember(String key, Consumer<Number> consumer)
      Requires that key exists, is a number, and passes the value to consumer.
      Parameters:
      key - Key to retrieve.
      consumer - Consumer that accepts the number value.
      Returns:
      Returns the node.
    • getNumberMember

      public ObjectNode getNumberMember(String key, Consumer<Number> consumer)
      The same as expectNumberMember(String, Consumer) but the member is optional.
      Parameters:
      key - Key to retrieve.
      consumer - Consumer that accepts the number value.
      Returns:
      Returns the node.
    • getArrayMember

      public ObjectNode getArrayMember(String key, Consumer<List<Node>> consumer)
      Gets the nodes of an optional member that contains an array.
      Parameters:
      key - Key to retrieve.
      consumer - Consumer that accepts the array node value.
      Returns:
      Returns the node.
    • expectArrayMember

      public <N extends Node, T> ObjectNode expectArrayMember(String k, Function<N,T> map, Consumer<List<T>> consumer)
      Requires that the given member exists and is an array; then creates a list of values for the array by passing array element through the mapping function, and passes that list to the consumer.
      Type Parameters:
      N - Type of Node to expect in each array element.
      T - Type of value returned from the mapper.
      Parameters:
      k - Key to retrieve.
      map - Mapper that takes each array node and returns a mapped value.
      consumer - Consumer that accepts the collected mapped values.
      Returns:
      Returns the node.
    • getArrayMember

      public <N extends Node, T> ObjectNode getArrayMember(String k, Function<N,T> map, Consumer<List<T>> consumer)
      The same as expectArrayMember(String, Function, Consumer), but the member is optional.
      Type Parameters:
      N - Type of Node to expect in each array element.
      T - Type of value returned from the mapper.
      Parameters:
      k - Key to retrieve.
      map - Mapper that takes each array node and returns a mapped value.
      consumer - Consumer that accepts the collected mapped values.
      Returns:
      Returns the node.
    • isEmpty

      public boolean isEmpty()
      Returns true if this object has no members.
      Returns:
      Returns if this object is empty.
    • size

      public int size()
      Returns the number of members.
      Returns:
      Returns the number of members.
    • merge

      public ObjectNode merge(ObjectNode other)
      Merge this object node with another, creating a new ObjectNode.

      Conflicting keys are overwritten by other. If the current object has a source location, it is applied to the result. Otherwise, the source location of other is applied to the result.

      Parameters:
      other - Other object node to merge with.
      Returns:
      Returns the merged object node.
    • collect

      public static <T> Collector<T,Map<StringNode,Node>,ObjectNode> collect(Function<T,StringNode> keyMapper, Function<T,ToNode> valueMapper)
      Creates a collector that creates an ObjectNode.
      Type Parameters:
      T - Type being collected over (e.g., Map.Entry, Pair, etc.).
      Parameters:
      keyMapper - Key mapping function that returns a ToNode.
      valueMapper - Value mapping function that returns a ToNode.
      Returns:
      Returns the created collector.
    • collectStringKeys

      public static <T> Collector<T,Map<StringNode,Node>,ObjectNode> collectStringKeys(Function<T,String> keyMapper, Function<T,ToNode> valueMapper)
      Creates a collector that creates an ObjectNode.
      Type Parameters:
      T - Type being collected over (e.g., Map.Entry, Pair, etc.).
      Parameters:
      keyMapper - Key mapping function that returns a string.
      valueMapper - Value mapping function that returns a ToNode.
      Returns:
      Returns the created collector.
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toBuilder

      public ObjectNode.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 interface ToSmithyBuilder<ObjectNode>
      Returns:
      a builder for type T