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