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