aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/chains/InheritanceBuilder.java
blob: ef22ca3d1e62d994638628c994541df887dc439e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.builder.xml.dom.chains;

import com.yahoo.component.ComponentSpecification;
import com.yahoo.component.chain.model.ChainSpecification;
import com.yahoo.config.model.builder.xml.XmlHelper;
import org.w3c.dom.Element;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Build an Inheritance object from an inheritance section.
 * @author Tony Vaagenes
 */
public class InheritanceBuilder {
    final ChainSpecification.Inheritance inheritance;

    public InheritanceBuilder(Element spec) {
        inheritance = new ChainSpecification.Inheritance(
                read(spec, "inherits"),
                read(spec, "excludes"));
    }

    public ChainSpecification.Inheritance build() {
        return inheritance;
    }

    private Set<ComponentSpecification> read(Element spec, String attributeName) {
        Set<ComponentSpecification> componentSpecifications = new LinkedHashSet<>();

        componentSpecifications.addAll(spaceSeparatedComponentSpecificationsFromAttribute(spec, attributeName));

        return componentSpecifications;
    }

    private Collection<ComponentSpecification> spaceSeparatedComponentSpecificationsFromAttribute(Element spec, String attributeName) {
        return toComponentSpecifications(XmlHelper.spaceSeparatedSymbolsFromAttribute(spec, attributeName));
    }

    private Set<ComponentSpecification> toComponentSpecifications(Collection<String> symbols) {
        Set<ComponentSpecification> specifications = new LinkedHashSet<>();
        for (String symbol : symbols) {
            specifications.add(new ComponentSpecification(symbol));
        }
        return specifications;
    }
}