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