aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/component/ConfigProducerGroup.java
blob: 3cb16b8ff316f5ea19ac2af7e4cc2e3fc7b6b699 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.component;

import com.yahoo.component.ComponentId;
import com.yahoo.config.model.producer.AnyConfigProducer;
import com.yahoo.config.model.producer.TreeConfigProducer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * A group of config producers that have a component id.
 *
 * @author Tony Vaagenes
 */
public class ConfigProducerGroup<CHILD extends AnyConfigProducer> extends TreeConfigProducer<CHILD> {

    private final Map<ComponentId, CHILD> producerById = new LinkedHashMap<>();

    public ConfigProducerGroup(TreeConfigProducer<? super ConfigProducerGroup> parent, String subId) {
        super(parent, subId);
    }

    public void addComponent(ComponentId id, CHILD producer) {
        CHILD existing = producerById.put(id, producer);
        if ( existing != null) {
            throw new IllegalArgumentException("Both " + producer + " and " + existing + " are configured" +
                                               " with the id '" + id + "'. All components must have a unique id.");
        }
        addChild(producer);
    }

    /**
     * Removes a component by id
     *
     * @return the removed component, or null if it was not present
     */
    public CHILD removeComponent(ComponentId componentId) {
        CHILD component = producerById.remove(componentId);
        if (component == null) return null;
        removeChild(component);
        return component;
    }

    public Collection<CHILD> getComponents() {
        return Collections.unmodifiableCollection(getChildren().values());
    }

    public <T extends CHILD> Collection<T> getComponents(Class<T> componentClass) {
        Collection<T> result = new ArrayList<>();

        for (CHILD child: getChildren().values()) {
            if (componentClass.isInstance(child)) {
                result.add(componentClass.cast(child));
            }
        }
        return Collections.unmodifiableCollection(result);
    }

    /** Returns a map of all components in this group, with (local) component ID as key. */
    public Map<ComponentId, CHILD> getComponentMap() {
        return Collections.unmodifiableMap(producerById);
    }

}