aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@yahooinc.com>2022-01-10 19:18:16 +0100
committerHåkon Hallingstad <hakon@yahooinc.com>2022-01-10 19:23:11 +0100
commit3c9f5a714250876dbd31482c363b5486baa3e361 (patch)
treec0fa42d7bbd040a2fbda0f7c1099a8eb9b76213b /node-admin
parent7863aa3a9c5b13a4b420eeaab3c28144202200c6 (diff)
TemplateBuilder
Diffstat (limited to 'node-admin')
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java56
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateBuilder.java63
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java14
3 files changed, 79 insertions, 54 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java
index 75055c216c6..a497d54ba3a 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java
@@ -4,9 +4,8 @@ package com.yahoo.vespa.hosted.node.admin.task.util.template;
import com.yahoo.vespa.hosted.node.admin.task.util.text.Cursor;
import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange;
-import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.function.Consumer;
/**
@@ -39,12 +38,15 @@ import java.util.function.Consumer;
* @author hakonhall
*/
public class Template {
- private final Cursor start;
- private final Cursor end;
+ private final CursorRange range;
+ private final List<Consumer<FormBuilder>> sections;
+ private final Map<String, Cursor> names;
- private final List<Consumer<FormBuilder>> sections = new ArrayList<>();
- /** The value contains the location of the name of a sample variable section (with that name). */
- private final HashMap<String, Cursor> names = new HashMap<>();
+ public Template(CursorRange range, List<Consumer<FormBuilder>> sections, Map<String, Cursor> names) {
+ this.range = new CursorRange(range);
+ this.sections = List.copyOf(sections);
+ this.names = Map.copyOf(names);
+ }
public static Template from(String text) { return from(text, new TemplateDescriptor()); }
@@ -52,45 +54,7 @@ public class Template {
return TemplateParser.parse(text, descriptor).template();
}
- Template(Cursor start) {
- this.start = new Cursor(start);
- this.end = new Cursor(start);
- }
-
public Form instantiate() { return instantiate(null); }
- Form instantiate(Form parent) {
- return FormBuilder.build(parent, range(), sections);
- }
-
- void appendLiteralSection(Cursor end) {
- CursorRange range = verifyAndUpdateEnd(end);
- sections.add((FormBuilder builder) -> builder.addLiteralSection(range));
- }
-
- void appendVariableSection(String name, Cursor nameOffset, Cursor end) {
- CursorRange range = verifyAndUpdateEnd(end);
- sections.add(formBuilder -> formBuilder.addVariableSection(range, name, nameOffset));
- }
-
- void appendSubformSection(String name, Cursor nameCursor, Cursor end, Template body) {
- CursorRange range = verifyAndUpdateEnd(end);
- verifyNewName(name, nameCursor);
- sections.add(formBuilder -> formBuilder.addSubformSection(range, name, body));
- }
-
- private CursorRange range() { return new CursorRange(start, end); }
-
- private CursorRange verifyAndUpdateEnd(Cursor newEnd) {
- var range = new CursorRange(this.end, newEnd);
- this.end.set(newEnd);
- return range;
- }
-
- private void verifyNewName(String name, Cursor cursor) {
- Cursor alreadyDefinedNameCursor = names.put(name, cursor);
- if (alreadyDefinedNameCursor != null) {
- throw new NameAlreadyExistsTemplateException(name, alreadyDefinedNameCursor, cursor);
- }
- }
+ Form instantiate(Form parent) { return FormBuilder.build(parent, range, sections); }
}
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateBuilder.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateBuilder.java
new file mode 100644
index 00000000000..07c87e3833c
--- /dev/null
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateBuilder.java
@@ -0,0 +1,63 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vespa.hosted.node.admin.task.util.template;
+
+import com.yahoo.vespa.hosted.node.admin.task.util.text.Cursor;
+import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+
+/**
+ * @author hakonhall
+ */
+class TemplateBuilder {
+ private final Cursor start;
+ private final Cursor end;
+
+ private final List<Consumer<FormBuilder>> sections = new ArrayList<>();
+
+ /** The value contains the location of the name of a sample variable section (with that name). */
+ private final Map<String, Cursor> names = new HashMap<>();
+
+ TemplateBuilder(Cursor start) {
+ this.start = new Cursor(start);
+ this.end = new Cursor(start);
+ }
+
+ void appendLiteralSection(Cursor end) {
+ CursorRange range = verifyAndUpdateEnd(end);
+ sections.add((FormBuilder builder) -> builder.addLiteralSection(range));
+ }
+
+ void appendVariableSection(String name, Cursor nameOffset, Cursor end) {
+ CursorRange range = verifyAndUpdateEnd(end);
+ sections.add(formBuilder -> formBuilder.addVariableSection(range, name, nameOffset));
+ }
+
+ void appendSubformSection(String name, Cursor nameCursor, Cursor end, Template body) {
+ CursorRange range = verifyAndUpdateEnd(end);
+ verifyNewName(name, nameCursor);
+ sections.add(formBuilder -> formBuilder.addSubformSection(range, name, body));
+ }
+
+ Template build() {
+ var range = new CursorRange(start, end);
+ return new Template(range, sections, names);
+ }
+
+ private CursorRange verifyAndUpdateEnd(Cursor newEnd) {
+ var range = new CursorRange(this.end, newEnd);
+ this.end.set(newEnd);
+ return range;
+ }
+
+ private void verifyNewName(String name, Cursor cursor) {
+ Cursor alreadyDefinedNameCursor = names.put(name, cursor);
+ if (alreadyDefinedNameCursor != null) {
+ throw new NameAlreadyExistsTemplateException(name, alreadyDefinedNameCursor, cursor);
+ }
+ }
+}
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java
index 48bc4d737e2..5a853fe1eb5 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java
@@ -2,7 +2,6 @@
package com.yahoo.vespa.hosted.node.admin.task.util.template;
import com.yahoo.vespa.hosted.node.admin.task.util.text.Cursor;
-import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange;
import java.util.Optional;
@@ -15,7 +14,7 @@ class TemplateParser {
private final TemplateDescriptor descriptor;
private final Cursor start;
private final Cursor current;
- private final Template template;
+ private final TemplateBuilder templateBuilder;
private final FormEndsIn formEndsIn;
static TemplateParser parse(String text, TemplateDescriptor descriptor) {
@@ -34,18 +33,17 @@ class TemplateParser {
this.descriptor = descriptor;
this.start = new Cursor(start);
this.current = new Cursor(start);
- this.template = new Template(start);
+ this.templateBuilder = new TemplateBuilder(start);
this.formEndsIn = formEndsIn;
}
- CursorRange range() { return new CursorRange(start, current); }
- Template template() { return template; }
+ Template template() { return templateBuilder.build(); }
private void parse() {
do {
current.advanceTo(descriptor.startDelimiter());
if (!current.equals(start)) {
- template.appendLiteralSection(current);
+ templateBuilder.appendLiteralSection(current);
}
if (current.eot()) {
@@ -93,7 +91,7 @@ class TemplateParser {
var nameStart = new Cursor(current);
String name = parseId();
parseEndDelimiter(true);
- template.appendVariableSection(name, nameStart, current);
+ templateBuilder.appendVariableSection(name, nameStart, current);
}
private void parseEndDirective() {
@@ -109,7 +107,7 @@ class TemplateParser {
TemplateParser bodyParser = parse(descriptor, current, FormEndsIn.END);
current.set(bodyParser.current);
- template.appendSubformSection(name, startOfName, current, bodyParser.template());
+ templateBuilder.appendSubformSection(name, startOfName, current, bodyParser.template());
}
private void skipRequiredWhitespaces() {