Class MapNodeKeyGenerator

java.lang.Object
uk.blankaspect.common.basictree.MapNodeKeyGenerator

public class MapNodeKeyGenerator extends Object
This class implements a decorator for a map node that allows key–value pairs to be added to the map node with keys that are generated by a specified function.

The class provides methods that create a node of the appropriate type for a specified value and add the node to a map node that is associated with an instance of this class. The new node is added to the associated map node with a key that is generated by a function that returns a string for a given map node.

The default key generator returns the concatenation of a prefix and the decimal string representation of an index that is incremented after each invocation of the key generator.

The addString(String) method may be used to add 'comments' — string nodes that have no semantic value — to the associated map node.

  • Field Details

    • DEFAULT_GENERATED_KEY_PREFIX

      public static final String DEFAULT_GENERATED_KEY_PREFIX
      The prefix of a key that is generated by the default key-generator function.
      See Also:
  • Constructor Details

    • MapNodeKeyGenerator

      public MapNodeKeyGenerator(MapNode mapNode)
      Creates a new instance of a generator of keys for the specified map node.
      Parameters:
      mapNode - the map node that will be associated with the key generator.
      Throws:
      IllegalArgumentException - if mapNode is null.
  • Method Details

    • forNode

      public static MapNodeKeyGenerator forNode(MapNode mapNode)
      Creates and returns a new instance of a generator of keys for the specified map node.
      Parameters:
      mapNode - the map node that will be associated with the key generator.
      Returns:
      a new instance of a key generator for mapNode.
      Throws:
      IllegalArgumentException - if mapNode is null.
    • keyGenerator

      public Function<MapNode,String> keyGenerator()
      Returns the function that is used to generate keys for the associated map node.
      Returns:
      the function is used to generate keys for the associated map node, or null if the default function is used.
      See Also:
    • keyGenerator

      public MapNodeKeyGenerator keyGenerator(Function<MapNode,String> generator)
      Sets the function that is used to generate keys for the associated map node. If the specified function is null, the key-generator function will be set to its default value, which returns the concatenation of a prefix and the decimal string representation of an auto-incrementing index.
      Parameters:
      generator - the function that generates a key for the associated map node. If it is null, the default key generator is set.
      Returns:
      this key generator.
      See Also:
    • setKeyGenerator

      public void setKeyGenerator(Function<MapNode,String> generator)
      Sets the function that is used to generate keys for the associated map node. If the specified function is null, the key-generator function will be set to its default value, which returns the concatenation of a prefix and the decimal string representation of an auto-incrementing index.
      Parameters:
      generator - the function that generates a key for the associated map node. If it is null, the default key generator is set.
      See Also:
    • keyPrefix

      public String keyPrefix()
      Returns the prefix of the keys that are generated by the default key-generator function.
      Returns:
      the prefix of the keys that are generated by the default key-generator function.
      See Also:
    • keyPrefix

      public MapNodeKeyGenerator keyPrefix(String prefix)
      Sets the prefix of the keys that are generated by the default key-generator function.
      Parameters:
      prefix - the prefix of the keys that are generated by the default key-generator function. If it is null, the prefix is set to the empty string.
      Returns:
      this key generator.
      See Also:
    • setKeyPrefix

      public void setKeyPrefix(String prefix)
      Sets the prefix of the keys that are generated by the default key-generator function.
      Parameters:
      prefix - the prefix of the keys that are generated by the default key-generator function. If it is null, the prefix is set to the empty string.
      See Also:
    • keyIndex

      public int keyIndex()
      Returns the next value of the auto-incrementing index that is used by the default key-generator function.
      Returns:
      the next value of the auto-incrementing index that is used by the default key-generator function.
      See Also:
    • keyIndex

      public MapNodeKeyGenerator keyIndex(int index)
      Sets the next value of the auto-incrementing index that is used by the default key-generator function.
      Parameters:
      index - the value of the index that will be used in the next invocation of the default key-generator function.
      Returns:
      this key generator.
      See Also:
    • setKeyIndex

      public void setKeyIndex(int index)
      Sets the next value of the auto-incrementing index that is used by the default key-generator function.
      Parameters:
      index - the value of the index that will be used in the next invocation of the default key-generator function.
      See Also:
    • add

      public void add(AbstractNode value)
      Adds a key–value pair with the specified value and a key that is generated by the key-generator function to the map node that is associated with this generator. If the map node already contains a KV pair with the generated key, the specified value will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      value - the value of the key–value pair.
    • addNull

      public NullNode addNull()
      Creates a new instance of a null node, adds it to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the generated key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Returns:
      the null node that was created and added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.
    • addBoolean

      public BooleanNode addBoolean(boolean value)
      Creates a new instance of a Boolean node, adds it to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the generated key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      value - the value of the new Boolean node.
      Returns:
      the Boolean node that was created and added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.
    • addInt

      public IntNode addInt(int value)
      Creates a new instance of an 'int' node, adds it to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the generated key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      value - the value of the new 'int' node.
      Returns:
      the 'int' node that was created and added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.
    • addLong

      public LongNode addLong(long value)
      Creates a new instance of a 'long' node, adds it to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the generated key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      value - the value of the new 'long' node.
      Returns:
      the 'long' node that was created and added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.
    • addDouble

      public DoubleNode addDouble(double value)
      Creates a new instance of a 'double' node, adds it to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the generated key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      value - the value of the new 'double' node.
      Returns:
      the 'double' node that was created and added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.
    • addString

      public StringNode addString(String value)
      Creates a new instance of a string node, adds it to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the generated key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      value - the value of the new string node.
      Returns:
      the string node that was created and added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.
    • addList

      public ListNode addList(AbstractNode... elements)
      Creates a new instance of a list node that contains the specified elements, adds it to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the generated key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      elements - the elements of the new list node.
      Returns:
      the new list node that contains elements and that was added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.
    • addList

      public ListNode addList(Iterable<? extends AbstractNode> elements)
      Creates a new instance of a list node that contains the specified elements, adds it to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the generated key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      elements - the elements of the new list node.
      Returns:
      the new list node that contains elements and that was added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.
    • addMap

      public MapNode addMap(MapNode.Pair... pairs)
      Creates a new instance of a map node that contains the specified pairs of keys and values, adds the new map node to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the specified key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      pairs - the key–value pairs of the new map node.
      Returns:
      the new map node that contains pairs and that was added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.
    • addMap

      public MapNode addMap(Iterable<? extends MapNode.Pair> pairs)
      Creates a new instance of a map node that contains the specified pairs of keys and values, adds the new map node to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the specified key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      pairs - the key–value pairs of the new map node.
      Returns:
      the new map node that contains pairs and that was added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.
    • addMap

      public MapNode addMap(Map<String,AbstractNode> pairs)
      Creates a new instance of a map node that contains the specified pairs of keys and values, adds the new map node to the map node that is associated with this generator as a key–value pair with a key that is generated by the key-generator function, and returns the new node. If the associated map node already contains a KV pair with the specified key, the new node will replace the value of the existing pair without affecting the order of the pairs; otherwise, a new KV pair will be added to the end of the collection of pairs.
      Parameters:
      pairs - the key–value pairs of the new map node.
      Returns:
      the new map node that contains pairs and that was added to the map node that is associated with this generator as a key–value pair whose key was generated by the key-generator function.