All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.config.model.producer.UserConfigRepo Maven / Gradle / Ivy

There is a newer version: 8.441.21
Show newest version
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.producer;

import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.config.ConfigPayloadBuilder;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * A UserConfigRepo is a repository for user configs, typically for a particular config producer. The repo encapsulates
 * how the user configs are stored, and defines the methods to retrieve user configs and merge the repo with others.
 *
 * @author Ulf Lilleengen
 */
public class UserConfigRepo {

    private final Map userConfigsMap;

    public UserConfigRepo() {
        this.userConfigsMap = new LinkedHashMap<>();
    }

    public UserConfigRepo(Map map) {
        this.userConfigsMap = map;
    }

    public ConfigPayloadBuilder get(ConfigDefinitionKey key) {
        return userConfigsMap.get(key);
    }

    public void merge(UserConfigRepo newRepo) {
        for (Map.Entry entry : newRepo.userConfigsMap.entrySet()) {
            if (entry.getValue() == null) continue;

            ConfigDefinitionKey key = entry.getKey();
            if (userConfigsMap.containsKey(key)) {
                ConfigPayloadBuilder lhsBuilder = userConfigsMap.get(key);
                ConfigPayloadBuilder rhsBuilder = entry.getValue();
                lhsBuilder.override(rhsBuilder);
            } else {
                userConfigsMap.put(key, entry.getValue());
            }
        }
    }

    public boolean isEmpty() {
        return userConfigsMap.isEmpty();
    }

    public int size() {
        return userConfigsMap.size();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (ConfigDefinitionKey key : userConfigsMap.keySet()) {
            sb.append(key.toString());
        }
        return sb.toString();
    }

    /**
     * The keys of all the configs contained in this.
     * @return a set of ConfigDefinitionsKey
     */
    public Set configsProduced() {
        return userConfigsMap.keySet();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy