org.grails.config.PrefixedConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grails-core Show documentation
Show all versions of grails-core Show documentation
Grails Web Application Framework
/*
* 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