ConfigurationDecorator.java
001 /*
002  * Copyright 2008-2015 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package org.codehaus.griffon.runtime.core;
017 
018 import griffon.core.Configuration;
019 
020 import javax.annotation.Nonnull;
021 import javax.annotation.Nullable;
022 import java.util.Properties;
023 
024 import static griffon.util.CollectionUtils.toProperties;
025 import static griffon.util.TypeUtils.castToBoolean;
026 import static griffon.util.TypeUtils.castToDouble;
027 import static griffon.util.TypeUtils.castToFloat;
028 import static griffon.util.TypeUtils.castToInt;
029 import static griffon.util.TypeUtils.castToLong;
030 import static java.util.Objects.requireNonNull;
031 
032 /**
033  @author Andres Almiray
034  @since 2.2.0
035  */
036 public abstract class ConfigurationDecorator implements Configuration {
037     protected final Configuration delegate;
038 
039     public ConfigurationDecorator(@Nonnull Configuration delegate) {
040         this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null.");
041     }
042 
043     @Override
044     @Nullable
045     public Object get(@Nonnull String key) {
046         return delegate.get(key);
047     }
048 
049     @Nullable
050     @Override
051     @SuppressWarnings("unchecked")
052     public <T> T get(@Nonnull String key, @Nullable T defaultValue) {
053         T value = (Tget(key);
054         return value != null ? value : defaultValue;
055     }
056 
057     @Nullable
058     @Override
059     public Object getAt(@Nonnull String key) {
060         return get(key);
061     }
062 
063     @Nullable
064     @Override
065     public <T> T getAt(@Nonnull String key, @Nullable T defaultValue) {
066         return get(key, defaultValue);
067     }
068 
069     @Override
070     public boolean getAsBoolean(@Nonnull String key) {
071         return getAsBoolean(key, false);
072     }
073 
074     @Override
075     public boolean getAsBoolean(@Nonnull String key, boolean defaultValue) {
076         return castToBoolean(get(key), defaultValue);
077     }
078 
079     @Override
080     public int getAsInt(@Nonnull String key) {
081         return getAsInt(key, 0);
082     }
083 
084     @Override
085     public int getAsInt(@Nonnull String key, int defaultValue) {
086         return castToInt(get(key), defaultValue);
087     }
088 
089     @Override
090     public long getAsLong(@Nonnull String key) {
091         return getAsLong(key, 0L);
092     }
093 
094     @Override
095     public long getAsLong(@Nonnull String key, long defaultValue) {
096         return castToLong(get(key), defaultValue);
097     }
098 
099     @Override
100     public float getAsFloat(@Nonnull String key) {
101         return getAsFloat(key, 0f);
102     }
103 
104     @Override
105     public float getAsFloat(@Nonnull String key, float defaultValue) {
106         return castToFloat(get(key), defaultValue);
107     }
108 
109     @Override
110     public double getAsDouble(@Nonnull String key) {
111         return getAsDouble(key, 0d);
112     }
113 
114     @Override
115     public double getAsDouble(@Nonnull String key, double defaultValue) {
116         return castToDouble(get(key), defaultValue);
117     }
118 
119     @Nullable
120     @Override
121     public String getAsString(@Nonnull String key) {
122         return getAsString(key, null);
123     }
124 
125     @Nullable
126     @Override
127     public String getAsString(@Nonnull String key, @Nullable String defaultValue) {
128         Object value = get(key);
129         return value != null ? String.valueOf(value: defaultValue;
130     }
131 
132     @Nonnull
133     @Override
134     public Properties asProperties() {
135         return toProperties(asFlatMap());
136     }
137 }