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

com.netflix.archaius.config.DefaultSettableConfig Maven / Gradle / Ivy

There is a newer version: 2.8.2
Show newest version
/**
 * Copyright 2015 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.netflix.archaius.config;

import com.netflix.archaius.api.Config;
import com.netflix.archaius.api.config.SettableConfig;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.function.BiConsumer;

public class DefaultSettableConfig extends AbstractConfig implements SettableConfig {
    private volatile Map props = Collections.emptyMap();
    
    public DefaultSettableConfig(String name) {
        super(name);
    }

    public DefaultSettableConfig() {
        super(generateUniqueName("settable-"));
    }
    
    @Override
    public synchronized  void setProperty(String propName, T propValue) {
        Map copy = new HashMap<>(props.size() + 1);
        copy.putAll(props);
        copy.put(propName, propValue);
        props = copy;
        notifyConfigUpdated(this);
    }
    
    @Override
    public void clearProperty(String propName) {
        if (props.containsKey(propName)) {
            synchronized (this) {
                Map copy = new HashMap<>(props);
                copy.remove(propName);
                props = copy;
                notifyConfigUpdated(this);
            }
        }
    }

    @Override
    public boolean containsKey(String key) {
        return props.containsKey(key);
    }

    @Override
    public boolean isEmpty() {
        return props.isEmpty();
    }

    @Override
    public Object getRawProperty(String key) {
        return props.get(key);
    }
    
    @Override
    public Iterator getKeys() {
        return props.keySet().iterator();
    }

    @Override
    public void setProperties(Properties src) {
        if (null != src) {
            synchronized (this) {
                Map copy = new HashMap<>(props.size() + src.size());
                copy.putAll(props);
                for (Entry prop : src.entrySet()) {
                    copy.put(prop.getKey().toString(), prop.getValue());
                }
                props = copy;
                notifyConfigUpdated(this);
            }
        }
    }

    @Override
    public void setProperties(Config src) {
        if (null != src) {
            synchronized (this) {
                Map copy = new HashMap<>(props);
                src.forEachProperty(copy::put);
                props = copy;
                notifyConfigUpdated(this);
            }
        }
    }

    @Override
    public void forEachProperty(BiConsumer consumer) {
        props.forEach(consumer);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy