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

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

The newest version!
package com.netflix.archaius.config;

import com.netflix.archaius.api.Config;
import com.netflix.archaius.api.PropertyDetails;

import java.util.Iterator;
import java.util.function.BiConsumer;

/**
 * Extendable base class for the dependent config paradigm. Dependent configs are assumed to be configs which inherit
 * values from some number (1+) of parent configs. Dependent configs hold onto caches of the property data and operate
 * independently of the parent configs except in cases of propagation of instrumentation data and property value
 * changes.
 *
 * TODO: Move DependentConfigListener logic here as well?
 */
public abstract class AbstractDependentConfig extends AbstractConfig {

    public AbstractDependentConfig(String name) {
        super(name);
    }

    public AbstractDependentConfig() {
        super();
    }

    abstract CachedState getState();

    @Override
    public Object getRawProperty(String key) {
        CachedState state = getState();
        Object value = state.getData().get(key);
        Config config = state.getInstrumentedKeys().get(key);
        if (config != null) {
            config.recordUsage(createPropertyDetails(key, value));
        }
        return value;
    }

    @Override
    public Object getRawPropertyUninstrumented(String key) {
        return getState().getData().get(key);
    }

    /** Return a set of all unique keys tracked by any child of this composite. */
    @Override
    public Iterator getKeys() {
        return getState().getData().keySet().iterator();
    }

    @Override
    public Iterable keys() {
        return getState().getData().keySet();
    }

    @Override
    public void forEachProperty(BiConsumer consumer) {
        CachedState state = getState();
        state.getData().forEach((k, v) -> {
            Config config = state.getInstrumentedKeys().get(k);
            if (config != null) {
                config.recordUsage(createPropertyDetails(k, v));
            }
            consumer.accept(k, v);
        });
    }

    @Override
    public void forEachPropertyUninstrumented(BiConsumer consumer) {
        getState().getData().forEach(consumer);
    }

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

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

    @Override
    public void recordUsage(PropertyDetails propertyDetails) {
        CachedState state = getState();
        Config config = state.getInstrumentedKeys().get(propertyDetails.getKey());
        if (config != null) {
            config.recordUsage(createPropertyDetails(propertyDetails.getKey(), propertyDetails.getValue()));
        }
    }

    @Override
    public boolean instrumentationEnabled() {
        // In the case of dependent configs, instrumentation needs to be propagated.
        // So, if any of the parent configs are instrumented, we mark this config as instrumented as well.
        return !getState().getInstrumentedKeys().isEmpty();
    }
    
    protected PropertyDetails createPropertyDetails(String key, Object value) {
        return new PropertyDetails(key, null, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy