Package uk.blankaspect.common.basictree
Class MapNodeKeyGenerator
java.lang.Object
uk.blankaspect.common.basictree.MapNodeKeyGenerator
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe prefix of a key that is generated by the default key-generator function. -
Constructor Summary
ConstructorsConstructorDescriptionMapNodeKeyGenerator(MapNode mapNode) Creates a new instance of a generator of keys for the specified map node. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(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.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.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.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.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.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.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.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.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.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.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.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.static MapNodeKeyGeneratorCreates and returns a new instance of a generator of keys for the specified map node.Returns the function that is used to generate keys for the associated map node.keyGenerator(Function<MapNode, String> generator) Sets the function that is used to generate keys for the associated map node.intkeyIndex()Returns the next value of the auto-incrementing index that is used by the default key-generator function.keyIndex(int index) Sets the next value of the auto-incrementing index that is used by the default key-generator function.Returns the prefix of the keys that are generated by the default key-generator function.Sets the prefix of the keys that are generated by the default key-generator function.voidsetKeyGenerator(Function<MapNode, String> generator) Sets the function that is used to generate keys for the associated map node.voidsetKeyIndex(int index) Sets the next value of the auto-incrementing index that is used by the default key-generator function.voidsetKeyPrefix(String prefix) Sets the prefix of the keys that are generated by the default key-generator function.
-
Field Details
-
DEFAULT_GENERATED_KEY_PREFIX
The prefix of a key that is generated by the default key-generator function.- See Also:
-
-
Constructor Details
-
MapNodeKeyGenerator
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- ifmapNodeisnull.
-
-
Method Details
-
forNode
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- ifmapNodeisnull.
-
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
nullif the default function is used. - See Also:
-
keyGenerator
Sets the function that is used to generate keys for the associated map node. If the specified function isnull, 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 isnull, the default key generator is set.- Returns:
- this key generator.
- See Also:
-
setKeyGenerator
Sets the function that is used to generate keys for the associated map node. If the specified function isnull, 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 isnull, the default key generator is set.- See Also:
-
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
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 isnull, the prefix is set to the empty string.- Returns:
- this key generator.
- See Also:
-
setKeyPrefix
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 isnull, 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
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
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
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
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
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
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
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
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
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
elementsand 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
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
elementsand 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
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
pairsand 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
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
pairsand 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
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
pairsand 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.
-