AbstractConfiguration.java
001 /*
002  * Copyright 2008-2015 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.util.Properties;
023 
024 import static griffon.util.CollectionUtils.toProperties;
025 import static griffon.util.TypeUtils.castToBoolean;
026 import static griffon.util.TypeUtils.castToDouble;
027 import static griffon.util.TypeUtils.castToFloat;
028 import static griffon.util.TypeUtils.castToInt;
029 import static griffon.util.TypeUtils.castToLong;
030 
031 /**
032  @author Andres Almiray
033  @since 2.0.0
034  */
035 public abstract class AbstractConfiguration implements Configuration {
036     @Nullable
037     @Override
038     @SuppressWarnings("unchecked")
039     public <T> T get(@Nonnull String key, @Nullable T defaultValue) {
040         T value = (Tget(key);
041         return value != null ? value : defaultValue;
042     }
043 
044     @Nullable
045     @Override
046     public Object getAt(@Nonnull String key) {
047         return get(key);
048     }
049 
050     @Nullable
051     @Override
052     public <T> T getAt(@Nonnull String key, @Nullable T defaultValue) {
053         return get(key, defaultValue);
054     }
055 
056     @Override
057     public boolean getAsBoolean(@Nonnull String key) {
058         return getAsBoolean(key, false);
059     }
060 
061     @Override
062     public boolean getAsBoolean(@Nonnull String key, boolean defaultValue) {
063         return castToBoolean(get(key), defaultValue);
064     }
065 
066     @Override
067     public int getAsInt(@Nonnull String key) {
068         return getAsInt(key, 0);
069     }
070 
071     @Override
072     public int getAsInt(@Nonnull String key, int defaultValue) {
073         return castToInt(get(key), defaultValue);
074     }
075 
076     @Override
077     public long getAsLong(@Nonnull String key) {
078         return getAsLong(key, 0L);
079     }
080 
081     @Override
082     public long getAsLong(@Nonnull String key, long defaultValue) {
083         return castToLong(get(key), defaultValue);
084     }
085 
086     @Override
087     public float getAsFloat(@Nonnull String key) {
088         return getAsFloat(key, 0f);
089     }
090 
091     @Override
092     public float getAsFloat(@Nonnull String key, float defaultValue) {
093         return castToFloat(get(key), defaultValue);
094     }
095 
096     @Override
097     public double getAsDouble(@Nonnull String key) {
098         return getAsDouble(key, 0d);
099     }
100 
101     @Override
102     public double getAsDouble(@Nonnull String key, double defaultValue) {
103         return castToDouble(get(key), defaultValue);
104     }
105 
106     @Nullable
107     @Override
108     public String getAsString(@Nonnull String key) {
109         return getAsString(key, null);
110     }
111 
112     @Nullable
113     @Override
114     public String getAsString(@Nonnull String key, @Nullable String defaultValue) {
115         Object value = get(key);
116         return value != null ? String.valueOf(value: defaultValue;
117     }
118 
119     @Nonnull
120     @Override
121     public Properties asProperties() {
122         return toProperties(asFlatMap());
123     }
124 }