ConfigurationDecorator.java
001 /*
002  * Copyright 2008-2016 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.beans.PropertyEditor;
023 import java.util.Map;
024 import java.util.Properties;
025 import java.util.ResourceBundle;
026 
027 import static griffon.core.editors.PropertyEditorResolver.findEditor;
028 import static griffon.util.CollectionUtils.toProperties;
029 import static griffon.util.TypeUtils.castToBoolean;
030 import static griffon.util.TypeUtils.castToDouble;
031 import static griffon.util.TypeUtils.castToFloat;
032 import static griffon.util.TypeUtils.castToInt;
033 import static griffon.util.TypeUtils.castToLong;
034 import static java.util.Objects.requireNonNull;
035 
036 /**
037  @author Andres Almiray
038  @since 2.2.0
039  */
040 public class ConfigurationDecorator implements Configuration {
041     protected final Configuration delegate;
042 
043     public ConfigurationDecorator(@Nonnull Configuration delegate) {
044         this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null.");
045     }
046 
047     @Override
048     @Nullable
049     public Object get(@Nonnull String key) {
050         return delegate.get(key);
051     }
052 
053     @Nullable
054     @Override
055     @SuppressWarnings("unchecked")
056     public <T> T get(@Nonnull String key, @Nullable T defaultValue) {
057         return delegate.get(key, defaultValue);
058     }
059 
060     @Nullable
061     @Override
062     public Object getAt(@Nonnull String key) {
063         return get(key);
064     }
065 
066     @Nullable
067     @Override
068     public <T> T getAt(@Nonnull String key, @Nullable T defaultValue) {
069         return get(key, defaultValue);
070     }
071 
072     @Override
073     public boolean getAsBoolean(@Nonnull String key) {
074         return getAsBoolean(key, false);
075     }
076 
077     @Override
078     public boolean getAsBoolean(@Nonnull String key, boolean defaultValue) {
079         return castToBoolean(get(key), defaultValue);
080     }
081 
082     @Override
083     public int getAsInt(@Nonnull String key) {
084         return getAsInt(key, 0);
085     }
086 
087     @Override
088     public int getAsInt(@Nonnull String key, int defaultValue) {
089         return castToInt(get(key), defaultValue);
090     }
091 
092     @Override
093     public long getAsLong(@Nonnull String key) {
094         return getAsLong(key, 0L);
095     }
096 
097     @Override
098     public long getAsLong(@Nonnull String key, long defaultValue) {
099         return castToLong(get(key), defaultValue);
100     }
101 
102     @Override
103     public float getAsFloat(@Nonnull String key) {
104         return getAsFloat(key, 0f);
105     }
106 
107     @Override
108     public float getAsFloat(@Nonnull String key, float defaultValue) {
109         return castToFloat(get(key), defaultValue);
110     }
111 
112     @Override
113     public double getAsDouble(@Nonnull String key) {
114         return getAsDouble(key, 0d);
115     }
116 
117     @Override
118     public double getAsDouble(@Nonnull String key, double defaultValue) {
119         return castToDouble(get(key), defaultValue);
120     }
121 
122     @Nullable
123     @Override
124     public String getAsString(@Nonnull String key) {
125         return getAsString(key, null);
126     }
127 
128     @Nullable
129     @Override
130     public String getAsString(@Nonnull String key, @Nullable String defaultValue) {
131         return delegate.getAsString(key, defaultValue);
132     }
133 
134     @Nullable
135     @Override
136     public <T> T getAs(@Nonnull String key) {
137         return delegate.getAs(key);
138     }
139 
140     @Nullable
141     @Override
142     public <T> T getAs(@Nonnull String key, @Nullable T defaultValue) {
143         return delegate.getAs(key, defaultValue);
144     }
145 
146     @Nullable
147     @Override
148     public <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type) {
149         return delegate.getConverted(key, type);
150     }
151 
152     @Nullable
153     @Override
154     public <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type, @Nullable T defaultValue) {
155         return delegate.getConverted(key, type, defaultValue);
156     }
157 
158     @Nonnull
159     @Override
160     public Properties asProperties() {
161         return toProperties(asFlatMap());
162     }
163 
164     @Override
165     public boolean containsKey(@Nonnull String key) {
166         return delegate.containsKey(key);
167     }
168 
169     @Nonnull
170     @Override
171     public Map<String, Object> asFlatMap() {
172         return delegate.asFlatMap();
173     }
174 
175     @Nonnull
176     @Override
177     public ResourceBundle asResourceBundle() {
178         return delegate.asResourceBundle();
179     }
180 
181     @SuppressWarnings("unchecked")
182     protected <T> T convertValue(@Nullable Object value, @Nonnull Class<T> type) {
183         if (value != null) {
184             if (type.isAssignableFrom(value.getClass())) {
185                 return (Tvalue;
186             else {
187                 PropertyEditor editor = findEditor(type);
188                 editor.setValue(value);
189                 return (Teditor.getValue();
190             }
191         }
192         return null;
193     }
194 }