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

org.grails.config.PrefixedConfig Maven / Gradle / Ivy

There is a newer version: 6.2.0
Show newest version
/*
 * Copyright 2014 original authors
 *
 * 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 org.grails.config;

import grails.config.Config;

import java.util.*;

/**
 * A config that accepts a prefix
 *
 * @author Graeme Rocher
 * @since 3.0
 */
public class PrefixedConfig implements Config {

    protected String prefix;
    protected String[] prefixTokens;
    protected Config delegate;

    public PrefixedConfig(String prefix, Config delegate) {
        this.prefix = prefix;
        this.prefixTokens = prefix.split("\\.");
        this.delegate = delegate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PrefixedConfig entries = (PrefixedConfig) o;

        if (delegate != null ? !delegate.equals(entries.delegate) : entries.delegate != null) return false;
        if (prefix != null ? !prefix.equals(entries.prefix) : entries.prefix != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = prefix != null ? prefix.hashCode() : 0;
        result = 31 * result + (delegate != null ? delegate.hashCode() : 0);
        return result;
    }

    @Override
    public Map flatten() {
        Map flattened = delegate.flatten();
        Map map = new LinkedHashMap(flattened.size());
        for (String key : flattened.keySet()) {
            map.put(formulateKey(key), flattened.get(key));
        }
        return map;
    }

    @Override
    public Properties toProperties() {
        Map flattened = flatten();
        Properties properties = new Properties();
        properties.putAll(flattened);
        return properties;
    }


    @Override
    public Object getAt(Object key) {
        return get(key);
    }

    @Override
    public Object navigate(String... path) {
        List tokens = new ArrayList();
        tokens.addAll(Arrays.asList(prefixTokens));
        tokens.addAll(Arrays.asList(path));
        return delegate.navigate(tokens.toArray(new String[tokens.size()]));
    }

    @Override
    public Iterator> iterator() {
        return entrySet().iterator();
    }

    @Override
    public int size() {
        return delegate.size();
    }

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

    @Override
    public boolean containsKey(Object key) {
        return containsProperty(key.toString());
    }

    @Override
    public boolean containsValue(Object value) {
        return values().contains(value);
    }

    @Override
    public Object get(Object key) {
        return getProperty(key.toString(), Object.class);
    }

    @Override
    public Set keySet() {
        Set keys = delegate.keySet();
        Set newKeys = new HashSet();
        for (String key : keys) {
            newKeys.add(formulateKey(key));
        }
        return newKeys;
    }

    @Override
    public Collection values() {
        return delegate.values();
    }

    @Override
    public Set> entrySet() {
        final Set> entries = delegate.entrySet();
        Set> newEntries = new HashSet>();
        for (final Entry entry : entries) {
            newEntries.add(new Entry() {
                @Override
                public String getKey() {
                    return formulateKey(entry.getKey());
                }

                @Override
                public Object getValue() {
                    return entry.getValue();
                }

                @Override
                public Object setValue(Object value) {
                    return entry.setValue(value);
                }
            });
        }

        return newEntries;
    }

    @Override
    public boolean containsProperty(String key) {
        return delegate.containsProperty(formulateKey(key));
    }

    @Override
    public String getProperty(String key) {
        return delegate.getProperty(formulateKey(key));
    }

    @Override
    public String getProperty(String key, String defaultValue) {
        return delegate.getProperty(formulateKey(key), defaultValue);
    }

    @Override
    public  T getProperty(String key, Class targetType) {
        return delegate.getProperty(formulateKey(key), targetType);
    }

    @Override
    public  T getProperty(String key, Class targetType, T defaultValue) {
        return delegate.getProperty(formulateKey(key), targetType, defaultValue);
    }

    @Override
    public  Class getPropertyAsClass(String key, Class targetType) {
        return delegate.getPropertyAsClass(formulateKey(key), targetType);
    }

    @Override
    public String getRequiredProperty(String key) throws IllegalStateException {
        return delegate.getRequiredProperty(formulateKey(key));
    }

    @Override
    public  T getRequiredProperty(String key, Class targetType) throws IllegalStateException {
        return delegate.getRequiredProperty(formulateKey(key), targetType);
    }

    protected String formulateKey(String key) {
        return prefix + '.' + key;
    }

    @Override
    public String resolvePlaceholders(String text) {
        throw new UnsupportedOperationException("Resolving placeholders not supported");
    }

    @Override
    public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
        throw new UnsupportedOperationException("Resolving placeholders not supported");
    }

    @Override
    public Object put(String key, Object value) {
        throw new UnsupportedOperationException("Config cannot be modified");
    }

    @Override
    public Object remove(Object key) {
        throw new UnsupportedOperationException("Config cannot be modified");
    }

    @Override
    public void putAll(Map m) {
        throw new UnsupportedOperationException("Config cannot be modified");
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException("Config cannot be modified");
    }


    @Override
    public Config merge(Map toMerge) {
        throw new UnsupportedOperationException("Config cannot be modified");
    }

    @Override
    public  T getProperty(String key, Class targetType, T defaultValue, List allowedValues) {
        return delegate.getProperty(key,targetType,defaultValue,allowedValues);
    }

    @Override
    public void setAt(Object key, Object value) {
        throw new UnsupportedOperationException("Config cannot be modified");
    }
}