/** * Create a new node tree with the given values, and the specified root * fallback value. * * @param values The values to be contained in this node tree * @param defaultValue The fallback value for any completely undefined nodes * @return The newly created node tree */ public static NodeTree of(Map<String, Boolean> values, Tristate defaultValue) { NodeTree newTree = new NodeTree(defaultValue); for (Map.Entry<String, Boolean> value : values.entrySet()) { Iterable<String> parts = NODE_SPLITTER.split(value.getKey().toLowerCase()); Node currentNode = newTree.rootNode; for (String part : parts) { if (currentNode.children.containsKey(part)) { currentNode = currentNode.children.get(part); } else { Node newNode = new Node(new HashMap<>()); currentNode.children.put(part, newNode); currentNode = newNode; } } currentNode.value = Tristate.fromBoolean(value.getValue()); } return newTree; }
/** * Create a new node tree with the given values, and the specified root fallback value. * * @param values The values to be contained in this node tree * @param defaultValue The fallback value for any completely undefined nodes * @return The newly created node tree */ public static NodeTree of(Map<String, Boolean> values, Tristate defaultValue) { NodeTree newTree = new NodeTree(defaultValue); for (Map.Entry<String, Boolean> value : values.entrySet()) { String[] parts = SPLIT_REGEX.split(value.getKey().toLowerCase()); Node currentNode = newTree.rootNode; for (String part : parts) { if (currentNode.children.containsKey(part)) { currentNode = currentNode.children.get(part); } else { Node newNode = new Node(new HashMap<>()); currentNode.children.put(part, newNode); currentNode = newNode; } } currentNode.value = Tristate.fromBoolean(value.getValue()); } return newTree; }
/** * Return a new NodeTree instance with a single changed value. * * @param node The node path to change the value of * @param value The value to change, or UNDEFINED to remove * @return The new, modified node tree */ public NodeTree withValue(String node, Tristate value) { String[] parts = SPLIT_REGEX.split(node.toLowerCase()); Node newRoot = new Node(new HashMap<>(this.rootNode.children)); Node newPtr = newRoot; Node currentPtr = this.rootNode; newPtr.value = currentPtr == null ? Tristate.UNDEFINED : currentPtr.value; for (String part : parts) { Node oldChild = currentPtr == null ? null : currentPtr.children.get(part); Node newChild = new Node(oldChild != null ? new HashMap<>(oldChild.children) : new HashMap<>()); newPtr.children.put(part, newChild); currentPtr = oldChild; newPtr = newChild; } newPtr.value = value; return new NodeTree(newRoot); }
private NodeTree(Tristate value) { this.rootNode = new Node(new HashMap<>()); this.rootNode.value = value; }
/** * Return a new NodeTree instance with a single changed value. * * @param node The node path to change the value of * @param value The value to change, or UNDEFINED to remove * @return The new, modified node tree */ public NodeTree withValue(String node, Tristate value) { Iterable<String> parts = NODE_SPLITTER.split(node.toLowerCase()); Node newRoot = new Node(new HashMap<>(this.rootNode.children)); Node newPtr = newRoot; Node currentPtr = this.rootNode; newPtr.value = currentPtr == null ? Tristate.UNDEFINED : currentPtr.value; for (String part : parts) { Node oldChild = currentPtr == null ? null : currentPtr.children.get(part); Node newChild = new Node(oldChild != null ? new HashMap<>(oldChild.children) : new HashMap<>()); newPtr.children.put(part, newChild); currentPtr = oldChild; newPtr = newChild; } newPtr.value = value; return new NodeTree(newRoot); }
private NodeTree(Tristate value) { this.rootNode = new Node(new HashMap<>()); this.rootNode.value = value; }