ConfigurationDecorator.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package org.codehaus.griffon.runtime.core.configuration;
019 
020 import griffon.core.Configuration;
021 
022 import javax.annotation.Nonnull;
023 import javax.annotation.Nullable;
024 import java.beans.PropertyEditor;
025 import java.util.Map;
026 import java.util.Properties;
027 import java.util.ResourceBundle;
028 
029 import static griffon.core.editors.PropertyEditorResolver.findEditor;
030 import static griffon.util.CollectionUtils.toProperties;
031 import static griffon.util.TypeUtils.castToBoolean;
032 import static griffon.util.TypeUtils.castToDouble;
033 import static griffon.util.TypeUtils.castToFloat;
034 import static griffon.util.TypeUtils.castToInt;
035 import static griffon.util.TypeUtils.castToLong;
036 import static java.util.Objects.requireNonNull;
037 
038 /**
039  @author Andres Almiray
040  @since 2.2.0
041  */
042 public class ConfigurationDecorator implements Configuration {
043     protected final Configuration delegate;
044 
045     public ConfigurationDecorator(@Nonnull Configuration delegate) {
046         this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null.");
047     }
048 
049     @Override
050     @Nullable
051     public Object get(@Nonnull String key) {
052         return delegate.get(key);
053     }
054 
055     @Nullable
056     @Override
057     @SuppressWarnings("unchecked")
058     public <T> T get(@Nonnull String key, @Nullable T defaultValue) {
059         return delegate.get(key, defaultValue);
060     }
061 
062     @Nullable
063     @Override
064     public Object getAt(@Nonnull String key) {
065         return get(key);
066     }
067 
068     @Nullable
069     @Override
070     public <T> T getAt(@Nonnull String key, @Nullable T defaultValue) {
071         return get(key, defaultValue);
072     }
073 
074     @Override
075     public boolean getAsBoolean(@Nonnull String key) {
076         return getAsBoolean(key, false);
077     }
078 
079     @Override
080     public boolean getAsBoolean(@Nonnull String key, boolean defaultValue) {
081         return castToBoolean(get(key), defaultValue);
082     }
083 
084     @Override
085     public int getAsInt(@Nonnull String key) {
086         return getAsInt(key, 0);
087     }
088 
089     @Override
090     public int getAsInt(@Nonnull String key, int defaultValue) {
091         return castToInt(get(key), defaultValue);
092     }
093 
094     @Override
095     public long getAsLong(@Nonnull String key) {
096         return getAsLong(key, 0L);
097     }
098 
099     @Override
100     public long getAsLong(@Nonnull String key, long defaultValue) {
101         return castToLong(get(key), defaultValue);
102     }
103 
104     @Override
105     public float getAsFloat(@Nonnull String key) {
106         return getAsFloat(key, 0f);
107     }
108 
109     @Override
110     public float getAsFloat(@Nonnull String key, float defaultValue) {
111         return castToFloat(get(key), defaultValue);
112     }
113 
114     @Override
115     public double getAsDouble(@Nonnull String key) {
116         return getAsDouble(key, 0d);
117     }
118 
119     @Override
120     public double getAsDouble(@Nonnull String key, double defaultValue) {
121         return castToDouble(get(key), defaultValue);
122     }
123 
124     @Nullable
125     @Override
126     public String getAsString(@Nonnull String key) {
127         return getAsString(key, null);
128     }
129 
130     @Nullable
131     @Override
132     public String getAsString(@Nonnull String key, @Nullable String defaultValue) {
133         return delegate.getAsString(key, defaultValue);
134     }
135 
136     @Nullable
137     @Override
138     public <T> T getAs(@Nonnull String key) {
139         return delegate.getAs(key);
140     }
141 
142     @Nullable
143     @Override
144     public <T> T getAs(@Nonnull String key, @Nullable T defaultValue) {
145         return delegate.getAs(key, defaultValue);
146     }
147 
148     @Nullable
149     @Override
150     public <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type) {
151         return delegate.getConverted(key, type);
152     }
153 
154     @Nullable
155     @Override
156     public <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type, @Nullable T defaultValue) {
157         return delegate.getConverted(key, type, defaultValue);
158     }
159 
160     @Override
161     @Nullable
162     public <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type, @Nonnull String format) {
163         return delegate.getConverted(key, type, format);
164     }
165 
166     @Override
167     @Nullable
168     public <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type, @Nonnull String format, @Nullable T defaultValue) {
169         return delegate.getConverted(key, type, format, defaultValue);
170     }
171 
172     @Nonnull
173     @Override
174     public Properties asProperties() {
175         return toProperties(asFlatMap());
176     }
177 
178     @Override
179     public boolean containsKey(@Nonnull String key) {
180         return delegate.containsKey(key);
181     }
182 
183     @Nonnull
184     @Override
185     public Map<String, Object> asFlatMap() {
186         return delegate.asFlatMap();
187     }
188 
189     @Nonnull
190     @Override
191     public ResourceBundle asResourceBundle() {
192         return delegate.asResourceBundle();
193     }
194 
195     @SuppressWarnings("unchecked")
196     protected <T> T convertValue(@Nullable Object value, @Nonnull Class<T> type) {
197         if (value != null) {
198             if (type.isAssignableFrom(value.getClass())) {
199                 return (Tvalue;
200             else {
201                 PropertyEditor editor = findEditor(type);
202                 editor.setValue(value);
203                 return (Teditor.getValue();
204             }
205         }
206         return null;
207     }
208 }