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

com.github.datalking.common.env.CompositePropertySource Maven / Gradle / Ivy

package com.github.datalking.common.env;

import com.github.datalking.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 可保存多个PropertySource
 *
 * @author yaoo on 5/29/18
 */
public class CompositePropertySource extends EnumerablePropertySource {

    private final Set> propertySources = new LinkedHashSet<>();

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

    @Override
    public Object getProperty(String name) {
        for (PropertySource propertySource : this.propertySources) {
            Object candidate = propertySource.getProperty(name);
            if (candidate != null) {
                return candidate;
            }
        }
        return null;
    }

    @Override
    public boolean containsProperty(String name) {
        for (PropertySource propertySource : this.propertySources) {
            if (propertySource.containsProperty(name)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String[] getPropertyNames() {
        Set names = new LinkedHashSet<>();
        for (PropertySource propertySource : this.propertySources) {
            if (!(propertySource instanceof EnumerablePropertySource)) {
                throw new IllegalStateException(
                        "Failed to enumerate property names due to non-enumerable property source: " + propertySource);
            }
            names.addAll(Arrays.asList(((EnumerablePropertySource) propertySource).getPropertyNames()));
        }
        return StringUtils.toStringArray(names);
    }


    public void addPropertySource(PropertySource propertySource) {
        this.propertySources.add(propertySource);
    }

    public void addFirstPropertySource(PropertySource propertySource) {
        List> existing = new ArrayList<>(this.propertySources);
        this.propertySources.clear();
        this.propertySources.add(propertySource);
        this.propertySources.addAll(existing);
    }

    public Collection> getPropertySources() {
        return this.propertySources;
    }

    @Override
    public String toString() {
        return String.format("%s [name='%s', propertySources=%s]",
                getClass().getSimpleName(), this.name, this.propertySources);
    }

}