Class ObjectNode

  • All Implemented Interfaces:
    FromSourceLocation, ToNode, software.amazon.smithy.utils.ToSmithyBuilder<ObjectNode>

    public final class ObjectNode
    extends Node
    implements software.amazon.smithy.utils.ToSmithyBuilder<ObjectNode>
    Represents an object node.
    • Method Detail

      • fromStringMap

        public static ObjectNode fromStringMap​(java.util.Map<java.lang.String,​java.lang.String> map)
      • 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​(java.lang.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.
      • asObjectNode

        public java.util.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 ToNodeObjectNode 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 ToNodeObjectNode withMember​(java.lang.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​(java.lang.String key,
                                     java.lang.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​(java.lang.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​(java.lang.String key,
                                     java.lang.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 ToNodeObjectNode withOptionalMember​(java.lang.String key,
                                                                java.util.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​(java.lang.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 java.util.Map<StringNode,​Node> getMembers()
        Gets the map of members.
        Returns:
        Returns a map of nodes.
      • containsMember

        public boolean containsMember​(java.lang.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 java.util.Optional<Node> getMember​(java.lang.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 java.util.Map<java.lang.String,​Node> getMembersByPrefix​(java.lang.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 java.util.Map<java.lang.String,​Node> getStringMap()
        Gets an immutable Map<String, Node> that represents the ObjectNode.
        Returns:
        Returns the immutable map.
      • getStringMember

        public java.util.Optional<StringNode> getStringMember​(java.lang.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 java.lang.String getStringMemberOrDefault​(java.lang.String memberName,
                                                         java.lang.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 java.util.Optional<NumberNode> getNumberMember​(java.lang.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 java.lang.Number getNumberMemberOrDefault​(java.lang.String memberName,
                                                         java.lang.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 java.util.Optional<ArrayNode> getArrayMember​(java.lang.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 java.util.Optional<ObjectNode> getObjectMember​(java.lang.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 java.util.Optional<BooleanNode> getBooleanMember​(java.lang.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 java.lang.Boolean getBooleanMemberOrDefault​(java.lang.String memberName,
                                                           java.lang.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​(java.lang.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​(java.lang.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:
        java.lang.IllegalArgumentException - when memberName is null.
        ExpectationNotMetException - when memberName is is not present in the members map.
      • expectMember

        public Node expectMember​(java.lang.String name,
                                 java.lang.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 is not present in the members map.
      • expectArrayMember

        public ArrayNode expectArrayMember​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.util.Collection<java.lang.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​(java.util.Collection<java.lang.String> allowedProperties)
        Warns if unknown properties are found in object.
        Parameters:
        allowedProperties - Properties that may exist.
        Returns:
        Returns self
      • 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> java.util.stream.Collector<T,​java.util.Map<StringNode,​Node>,​ObjectNode> collect​(java.util.function.Function<T,​StringNode> keyMapper,
                                                                                                                            java.util.function.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> java.util.stream.Collector<T,​java.util.Map<StringNode,​Node>,​ObjectNode> collectStringKeys​(java.util.function.Function<T,​java.lang.String> keyMapper,
                                                                                                                                      java.util.function.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​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

        public ObjectNode.Builder toBuilder()
        Specified by:
        toBuilder in interface software.amazon.smithy.utils.ToSmithyBuilder<ObjectNode>