aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/ListSection.java
blob: bc68cf96153407212dd6c1ebeffb31742fdc7216 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 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.List;

/**
 * @author hakonhall
 */
class ListSection extends Section {
    private final String name;
    private final Cursor nameOffset;
    private final Form body;
    private final List<Form> elements = new ArrayList<>();

    ListSection(CursorRange range, String name, Cursor nameOffset, Form body) {
        super(range);
        this.name = name;
        this.nameOffset = new Cursor(nameOffset);
        this.body = body;
    }

    String name() { return name; }
    Cursor nameOffset() { return new Cursor(nameOffset); }

    @Override
    void setForm(Form form) {
        super.setForm(form);
        body.setParent(form);
    }

    Form add() {
        Form element = body.copy();
        element.setParent(form());
        elements.add(element);
        return element;
    }

    @Override
    void appendTo(StringBuilder buffer) {
        elements.forEach(form -> form.appendTo(buffer));
    }

    @Override
    void appendCopyTo(SectionList sectionList) {
        // avoid copying elements for now
        // Optimization: Reuse body in copy, since it is only used for copying.

        sectionList.appendListSection(name, nameOffset, range().end(), body);
    }
}