Class ImmutableNode.Builder

  • Enclosing class:
    ImmutableNode

    public static final class ImmutableNode.Builder
    extends java.lang.Object

    A builder class for creating instances of ImmutableNode.

    This class can be used to set all properties of an immutable node instance. Eventually call the create() method to obtain the resulting instance.

    Implementation note: This class is not thread-safe. It is intended to be used to define a single node instance only.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.String,​java.lang.Object> attributes
      A map for storing the attributes of the new node.
      private java.util.List<ImmutableNode> children
      A list for the children of the new node.
      private java.util.Map<java.lang.String,​java.lang.Object> directAttributes
      The direct map of attributes of the new node.
      private java.util.List<ImmutableNode> directChildren
      The direct list of children of the new node.
      private java.lang.String name
      The name of the node.
      private java.lang.Object value
      The value of the node.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        Builder()
      Creates a new instance of Builder which does not contain any property definitions yet.
        Builder​(int childCount)
      Creates a new instance of Builder and sets the number of expected child nodes.
      private Builder​(int childCount, java.util.Map<java.lang.String,​java.lang.Object> dirAttrs)
      Creates a new instance of Builder and initializes the attributes of the new node and prepares the collection for the children.
      private Builder​(java.util.List<ImmutableNode> dirChildren, java.util.Map<java.lang.String,​java.lang.Object> dirAttrs)
      Creates a new instance of Builder and initializes the children and attributes of the new node.
    • Field Detail

      • directChildren

        private final java.util.List<ImmutableNode> directChildren
        The direct list of children of the new node.
      • directAttributes

        private final java.util.Map<java.lang.String,​java.lang.Object> directAttributes
        The direct map of attributes of the new node.
      • children

        private java.util.List<ImmutableNode> children
        A list for the children of the new node. This list is populated by the addChild() method.
      • attributes

        private java.util.Map<java.lang.String,​java.lang.Object> attributes
        A map for storing the attributes of the new node. This map is populated by addAttribute().
      • name

        private java.lang.String name
        The name of the node.
      • value

        private java.lang.Object value
        The value of the node.
    • Constructor Detail

      • Builder

        public Builder()
        Creates a new instance of Builder which does not contain any property definitions yet.
      • Builder

        public Builder​(int childCount)
        Creates a new instance of Builder and sets the number of expected child nodes. Using this constructor helps the class to create a properly sized list for the child nodes to be added.
        Parameters:
        childCount - the number of child nodes
      • Builder

        private Builder​(int childCount,
                        java.util.Map<java.lang.String,​java.lang.Object> dirAttrs)
        Creates a new instance of Builder and initializes the attributes of the new node and prepares the collection for the children. This constructor is used internally by methods of ImmutableNode which update the node and change the children. The new number of child nodes can be passed so that the collection for the new children can be created with an appropriate size.
        Parameters:
        childCount - the expected number of new children
        dirAttrs - the attributes of the new node
      • Builder

        private Builder​(java.util.List<ImmutableNode> dirChildren,
                        java.util.Map<java.lang.String,​java.lang.Object> dirAttrs)
        Creates a new instance of Builder and initializes the children and attributes of the new node. This constructor is used internally by the ImmutableNode class for creating instances derived from another node. The passed in collections are passed directly to the newly created instance; thus they already need to be immutable. (Background is that the creation of intermediate objects is to be avoided.)
        Parameters:
        dirChildren - the children of the new node
        dirAttrs - the attributes of the new node
    • Method Detail

      • filterNull

        private static java.util.Collection<? extends ImmutableNode> filterNull​(java.util.Collection<? extends ImmutableNode> children)
        Filters null entries from the passed in collection with child nodes.
        Parameters:
        children - the collection to be filtered
        Returns:
        the collection with null entries removed
      • addAttribute

        public ImmutableNode.Builder addAttribute​(java.lang.String name,
                                                  java.lang.Object value)
        Adds an attribute to this builder. The passed in attribute key and value are stored in an internal map. If there is already an attribute with this name, it is overridden.
        Parameters:
        name - the attribute name
        value - the attribute value
        Returns:
        a reference to this object for method chaining
      • addAttributes

        public ImmutableNode.Builder addAttributes​(java.util.Map<java.lang.String,​?> attrs)
        Adds all attributes of the given map to this builder. This method works like addAttribute(String, Object), but it allows setting multiple attributes at once.
        Parameters:
        attrs - the map with attributes to be added (may be null
        Returns:
        a reference to this object for method chaining
      • addChild

        public ImmutableNode.Builder addChild​(ImmutableNode c)
        Adds a child node to this builder. The passed in node becomes a child of the newly created node. If it is null, it is ignored.
        Parameters:
        c - the child node (must not be null)
        Returns:
        a reference to this object for method chaining
      • addChildren

        public ImmutableNode.Builder addChildren​(java.util.Collection<? extends ImmutableNode> children)
        Adds multiple child nodes to this builder. This method works like addChild(ImmutableNode), but it allows setting a number of child nodes at once.
        Parameters:
        children - a collection with the child nodes to be added
        Returns:
        a reference to this object for method chaining
      • create

        public ImmutableNode create()
        Creates a new ImmutableNode instance based on the properties set for this builder.
        Returns:
        the newly created ImmutableNode
      • createAttributes

        private java.util.Map<java.lang.String,​java.lang.Object> createAttributes()
        Creates a map with the attributes of the newly created node. This is an immutable map. If direct attributes were set, they are returned. Otherwise an unmodifiable map from the attributes passed to this builder is constructed.
        Returns:
        a map with the attributes for the new node
      • createChildren

        java.util.List<ImmutableNode> createChildren()
        Creates a list with the children of the newly created node. The list returned here is always immutable. It depends on the way this builder was populated.
        Returns:
        the list with the children of the new node
      • ensureAttributesExist

        private void ensureAttributesExist()
        Ensures that the map for the attributes exists. It is created on demand.
      • ensureChildrenExist

        private void ensureChildrenExist()
        Ensures that the collection for the child nodes exists. It is created on demand.
      • initChildrenCollection

        private void initChildrenCollection​(int childCount)
        Creates the collection for child nodes based on the expected number of children.
        Parameters:
        childCount - the expected number of new children
      • name

        public ImmutableNode.Builder name​(java.lang.String n)
        Sets the name of the node to be created.
        Parameters:
        n - the node name
        Returns:
        a reference to this object for method chaining
      • value

        public ImmutableNode.Builder value​(java.lang.Object v)
        Sets the value of the node to be created.
        Parameters:
        v - the value
        Returns:
        a reference to this object for method chaining