AbstractConfiguration.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 import griffon.core.editors.ExtendedPropertyEditor;
022 
023 import javax.annotation.Nonnull;
024 import javax.annotation.Nullable;
025 import java.beans.PropertyEditor;
026 import java.util.Properties;
027 
028 import static griffon.core.editors.PropertyEditorResolver.findEditor;
029 import static griffon.util.CollectionUtils.toProperties;
030 import static griffon.util.GriffonNameUtils.requireNonBlank;
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.0.0
041  */
042 public abstract class AbstractConfiguration implements Configuration {
043     private static final String ERROR_TYPE_NULL = "Argument 'type' must not be null";
044     private static final String ERROR_FORMAT_BLANK = "Argument 'format' must not be blank";
045 
046     @Nullable
047     @Override
048     @SuppressWarnings("unchecked")
049     public <T> T get(@Nonnull String key, @Nullable T defaultValue) {
050         T value = (Tget(key);
051         return value != null ? value : defaultValue;
052     }
053 
054     @Nullable
055     @Override
056     public Object getAt(@Nonnull String key) {
057         return get(key);
058     }
059 
060     @Nullable
061     @Override
062     public <T> T getAt(@Nonnull String key, @Nullable T defaultValue) {
063         return get(key, defaultValue);
064     }
065 
066     @Override
067     public boolean getAsBoolean(@Nonnull String key) {
068         return getAsBoolean(key, false);
069     }
070 
071     @Override
072     public boolean getAsBoolean(@Nonnull String key, boolean defaultValue) {
073         return castToBoolean(get(key), defaultValue);
074     }
075 
076     @Override
077     public int getAsInt(@Nonnull String key) {
078         return getAsInt(key, 0);
079     }
080 
081     @Override
082     public int getAsInt(@Nonnull String key, int defaultValue) {
083         return castToInt(get(key), defaultValue);
084     }
085 
086     @Override
087     public long getAsLong(@Nonnull String key) {
088         return getAsLong(key, 0L);
089     }
090 
091     @Override
092     public long getAsLong(@Nonnull String key, long defaultValue) {
093         return castToLong(get(key), defaultValue);
094     }
095 
096     @Override
097     public float getAsFloat(@Nonnull String key) {
098         return getAsFloat(key, 0f);
099     }
100 
101     @Override
102     public float getAsFloat(@Nonnull String key, float defaultValue) {
103         return castToFloat(get(key), defaultValue);
104     }
105 
106     @Override
107     public double getAsDouble(@Nonnull String key) {
108         return getAsDouble(key, 0d);
109     }
110 
111     @Override
112     public double getAsDouble(@Nonnull String key, double defaultValue) {
113         return castToDouble(get(key), defaultValue);
114     }
115 
116     @Nullable
117     @Override
118     public String getAsString(@Nonnull String key) {
119         return getAsString(key, null);
120     }
121 
122     @Nullable
123     @Override
124     @SuppressWarnings("unchecked")
125     public <T> T getAs(@Nonnull String key) {
126         return (Tget(key);
127     }
128 
129     @Nullable
130     @Override
131     @SuppressWarnings("unchecked")
132     public <T> T getAs(@Nonnull String key, @Nullable T defaultValue) {
133         Object value = get(key);
134         return (T) (value != null ? value : defaultValue);
135     }
136 
137     @Nullable
138     @Override
139     public <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type) {
140         requireNonNull(type, ERROR_TYPE_NULL);
141         return convertValue(get(key), type);
142     }
143 
144     @Nullable
145     @Override
146     public <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type, @Nullable T defaultValue) {
147         T value = getConverted(key, type);
148         return type.cast(value != null ? value : defaultValue);
149     }
150 
151     @Nullable
152     @Override
153     public <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type, @Nonnull String format) {
154         requireNonNull(type, ERROR_TYPE_NULL);
155         return convertValue(get(key), type, format);
156     }
157 
158     @Nullable
159     @Override
160     public <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type, @Nonnull String format, @Nullable T defaultValue) {
161         T value = getConverted(key, type, format);
162         return type.cast(value != null ? value : defaultValue);
163     }
164 
165     @SuppressWarnings("unchecked")
166     protected <T> T convertValue(@Nullable Object value, @Nonnull Class<T> type) {
167         if (value != null) {
168             if (type.isAssignableFrom(value.getClass())) {
169                 return (Tvalue;
170             else {
171                 PropertyEditor editor = findEditor(type);
172                 editor.setValue(value);
173                 return (Teditor.getValue();
174             }
175         }
176         return null;
177     }
178 
179     @SuppressWarnings("unchecked")
180     protected <T> T convertValue(@Nullable Object value, @Nonnull Class<T> type, @Nonnull String format) {
181         if (value != null) {
182             if (type.isAssignableFrom(value.getClass())) {
183                 return (Tvalue;
184             else {
185                 PropertyEditor editor = findEditor(type);
186                 if (editor instanceof ExtendedPropertyEditor) {
187                     requireNonBlank(format, ERROR_FORMAT_BLANK);
188                     ((ExtendedPropertyEditoreditor).setFormat(format);
189                 }
190                 editor.setValue(value);
191                 return (Teditor.getValue();
192             }
193         }
194         return null;
195     }
196 
197     @Nullable
198     @Override
199     public String getAsString(@Nonnull String key, @Nullable String defaultValue) {
200         Object value = get(key);
201         return value != null ? String.valueOf(value: defaultValue;
202     }
203 
204     @Nonnull
205     @Override
206     public Properties asProperties() {
207         return toProperties(asFlatMap());
208     }
209 }