aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/main/java/com/yahoo/config/subscription/ConfigSet.java
blob: 3bed98dc2dd8c0e484960dacb27a9b5a5b3b9cf6 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.subscription;

import com.yahoo.config.ConfigInstance;
import com.yahoo.vespa.config.ConfigKey;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Config source as a programmatically built set of {@link com.yahoo.config.ConfigInstance}s
 *
 * @author Vegard Havdal
 */
public class ConfigSet implements ConfigSource {
    private final Map<ConfigKey<?>, ConfigInstance.Builder> configs = new ConcurrentHashMap<>();

    /**
     * Inserts a new builder in this set. If an existing entry exists, it is overwritten.
     *
     * @param configId The config id for this builder.
     * @param builder The builder that will produce config for the particular config id.
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void addBuilder(String configId, ConfigInstance.Builder builder) {
        Class<?> configClass = builder.getClass().getDeclaringClass();
        ConfigKey<?> key = new ConfigKey(configClass, configId);
        configs.put(key, builder);
    }

    /**
     * Returns a Builder matching the given key, or null if no match
     *
     * @param key a config key to get a Builder for
     * @return a ConfigInstance
     */
    public ConfigInstance.Builder get(ConfigKey<?> key) {
        return configs.get(key);
    }

    /**
     * Returns true if this set contains a config instance matching the given key
     *
     * @param key a config key
     * @return a ConfigInstance
     */
    public boolean contains(ConfigKey<?> key) {
        return configs.containsKey(key);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<ConfigKey<?>, ConfigInstance.Builder> entry : configs.entrySet()) {
            sb.append(entry.getKey()).append("=>").append(entry.getValue());
        }
        return sb.toString();
    }

}