Class ObjectNode

    • 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.
      • expectObjectNode

        public ObjectNode expectObjectNode​(java.util.function.Supplier<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 supplier.
        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 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 not present in the members map.
      • expectMember

        public Node expectMember​(java.lang.String name,
                                 java.util.function.Supplier<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 - 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​(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
      • expectMember

        public <T> ObjectNode expectMember​(java.lang.String key,
                                           java.util.function.Function<Node,​T> mapper,
                                           java.util.function.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​(java.lang.String key,
                                        java.util.function.Function<Node,​T> mapper,
                                        java.util.function.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​(java.lang.String name,
                                             java.util.function.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​(java.lang.String memberName,
                                          java.util.function.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​(java.lang.String key,
                                             java.util.function.Consumer<java.lang.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​(java.lang.String key,
                                          java.util.function.Consumer<java.lang.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​(java.lang.String key,
                                              java.util.function.Consumer<java.lang.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​(java.lang.String key,
                                           java.util.function.Consumer<java.lang.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​(java.lang.String key,
                                             java.util.function.Consumer<java.lang.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​(java.lang.String key,
                                          java.util.function.Consumer<java.lang.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​(java.lang.String key,
                                         java.util.function.Consumer<java.util.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​(java.lang.String k,
                                                                     java.util.function.Function<N,​T> map,
                                                                     java.util.function.Consumer<java.util.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​(java.lang.String k,
                                                                  java.util.function.Function<N,​T> map,
                                                                  java.util.function.Consumer<java.util.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> 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