diff options
Diffstat (limited to 'node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java')
-rw-r--r-- | node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java index 00e2e87fc5a..c95885d7753 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java @@ -3,6 +3,7 @@ package com.yahoo.vespa.hosted.node.admin.task.util.template; import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -13,25 +14,25 @@ import java.util.Optional; * @see Template * @author hakonhall */ -public class Form extends Section { - private final Form parent; +public class Form { + private Form parent = null; + private final CursorRange range; private final List<Section> sections; - private final Map<String, String> variables; + private final Map<String, String> values = new HashMap<>(); private final Map<String, SubformSection> subforms; - Form(Form parent, CursorRange range, List<Section> sections, Map<String, String> variables, - Map<String, SubformSection> subforms) { - super(range); - this.parent = parent; + Form(CursorRange range, List<Section> sections, Map<String, SubformSection> subforms) { + this.range = new CursorRange(range); this.sections = List.copyOf(sections); - this.variables = variables; // Mutable and referenced by the variable sections this.subforms = Map.copyOf(subforms); } + void setParent(Form parent) { this.parent = parent; } + /** Set the value of a variable expression, e.g. %{=color}. */ public Form set(String name, String value) { - variables.put(name, value); + values.put(name, value); return this; } @@ -52,24 +53,30 @@ public class Form extends Section { public Form add(String name) { var section = subforms.get(name); if (section == null) { - throw new NoSuchNameTemplateException(this, name); + throw new NoSuchNameTemplateException(range, name); } return section.add(); } public String render() { - var buffer = new StringBuilder((int) (range().length() * 1.2 + 128)); + var buffer = new StringBuilder((int) (range.length() * 1.2 + 128)); appendTo(buffer); return buffer.toString(); } - @Override public void appendTo(StringBuilder buffer) { sections.forEach(section -> section.appendTo(buffer)); } + /** Returns a deep copy of this. No changes to this affects the returned form, and vice versa. */ + Form copy() { + var builder = new FormBuilder(range.start()); + sections.forEach(section -> section.appendCopyTo(builder.topLevelSectionList())); + return builder.build(); + } + Optional<String> getVariableValue(String name) { - String value = variables.get(name); + String value = values.get(name); if (value != null) return Optional.of(value); if (parent != null) { return parent.getVariableValue(name); |