aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/component/ConfigProducerGroup.java
blob: 5460407e7cc92b9357bdebee50311a391a95e5c5 (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
// Copyright 2017 Yahoo Holdings. 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.AbstractConfigProducer;

import java.util.*;

/**
 * A group of config producers that have a component id.
 *
 * @author tonytv
 */
public class ConfigProducerGroup<CHILD extends AbstractConfigProducer<?>> extends AbstractConfigProducer<CHILD> {

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

    public ConfigProducerGroup(AbstractConfigProducer parent, String subId) {
        super(parent, subId);
    }

    public void addComponent(ComponentId id, CHILD producer) {
        boolean wasAdded = producerById.put(id, producer) == null;
        if (!wasAdded) {
            throw new IllegalArgumentException("Two entities have the same component id '" +
                                               id + "' in the same scope.");
        }
        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);
    }

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

}