Configuration.java
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 griffon.core;
017 
018 import javax.annotation.Nonnull;
019 import javax.annotation.Nullable;
020 import java.util.Map;
021 import java.util.Properties;
022 import java.util.ResourceBundle;
023 
024 /**
025  @author Andres Almiray
026  @since 2.0.0
027  */
028 public interface Configuration {
029     /**
030      * Searches for the key in this configuration.
031      *
032      @param key the key to search
033      @return true if the context (or its parent) contains the given key, false otherwise
034      */
035     boolean containsKey(@Nonnull String key);
036 
037     @Nonnull
038     Map<String, Object> asFlatMap();
039 
040     @Nonnull
041     ResourceBundle asResourceBundle();
042 
043     @Nonnull
044     Properties asProperties();
045 
046     /**
047      * Returns the value associated with the given key.
048      *
049      @param key the key to search
050      @return the value associated with the key or <tt>null<</tt> if not found.
051      */
052     @Nullable
053     Object get(@Nonnull String key);
054 
055     /**
056      * Returns the value associated with the given key.
057      *
058      @param key          the key to search
059      @param defaultValue the value to be returned if the key was not found
060      @param <T>          the type of the value
061      @return returns the value associated with the key, <tt>defaultValue</tt> if the key was not found
062      */
063     @Nullable
064     <T> T get(@Nonnull String key, @Nullable T defaultValue);
065 
066     /**
067      * Returns the value associated with the given key.
068      * Convenience method to use in Groovy aware environments.
069      *
070      @param key the key to search
071      @return the value associated with the key or <tt>null<</tt> if not found.
072      */
073     @Nullable
074     Object getAt(@Nonnull String key);
075 
076     /**
077      * Returns the value associated with the given key.
078      *
079      @param key          the key to search
080      @param defaultValue the value to be returned if the key was not found
081      @param <T>          the type of the value
082      @return returns the value associated with the key, <tt>defaultValue</tt> if the key was not found
083      */
084     @Nullable
085     <T> T getAt(@Nonnull String key, @Nullable T defaultValue);
086 
087     /**
088      * Finds a value associated with the given key. The value is
089      * converted to a <tt>boolean</tt> if found.
090      *
091      @param key the key to search
092      */
093     boolean getAsBoolean(@Nonnull String key);
094 
095     /**
096      * Finds a value associated with the given key. The value is
097      * converted to a <tt>boolean</tt> if found. If not found then the
098      * supplied <tt>defaultValue</tt> will be returned.
099      *
100      @param key          the key to search
101      @param defaultValue the value to be returned if the key is not found
102      */
103     boolean getAsBoolean(@Nonnull String key, boolean defaultValue);
104 
105     /**
106      * Finds a value associated with the given key. The value is
107      * converted to an <tt>int</tt> if found.
108      *
109      @param key the key to search
110      */
111     int getAsInt(@Nonnull String key);
112 
113     /**
114      * Finds a value associated with the given key. The value is
115      * converted to an <tt>int</tt> if found. If not found then the
116      * supplied <tt>defaultValue</tt> will be returned.
117      *
118      @param key          the key to search
119      @param defaultValue the value to be returned if the key is not found
120      */
121     int getAsInt(@Nonnull String key, int defaultValue);
122 
123     /**
124      * Finds a value associated with the given key. The value is
125      * converted to a <tt>long</tt> if found.
126      *
127      @param key the key to search
128      */
129     long getAsLong(@Nonnull String key);
130 
131     /**
132      * Finds a value associated with the given key. The value is
133      * converted to a <tt>long</tt> if found. If not found then the
134      * supplied <tt>defaultValue</tt> will be returned.
135      *
136      @param key          the key to search
137      @param defaultValue the value to be returned if the key is not found
138      */
139     long getAsLong(@Nonnull String key, long defaultValue);
140 
141     /**
142      * Finds a value associated with the given key. The value is
143      * converted to a <tt>float</tt> if found.
144      *
145      @param key the key to search
146      */
147     float getAsFloat(@Nonnull String key);
148 
149     /**
150      * Finds a value associated with the given key. The value is
151      * converted to a <tt>float</tt> if found. If not found then the
152      * supplied <tt>defaultValue</tt> will be returned.
153      *
154      @param key          the key to search
155      @param defaultValue the value to be returned if the key is not found
156      */
157     float getAsFloat(@Nonnull String key, float defaultValue);
158 
159     /**
160      * Finds a value associated with the given key. The value is
161      * converted to a <tt>double</tt> if found.
162      *
163      @param key the key to search
164      */
165     double getAsDouble(@Nonnull String key);
166 
167     /**
168      * Finds a value associated with the given key. The value is
169      * converted to a <tt>double</tt> if found. If not found then the
170      * supplied <tt>defaultValue</tt> will be returned.
171      *
172      @param key          the key to search
173      @param defaultValue the value to be returned if the key is not found
174      */
175     double getAsDouble(@Nonnull String key, double defaultValue);
176 
177     /**
178      * Finds a value associated with the given key. The value is
179      * converted to a <tt>String</tt> if found.
180      *
181      @param key the key to search
182      */
183     @Nullable
184     String getAsString(@Nonnull String key);
185 
186     /**
187      * Finds a value associated with the given key. The value is
188      * converted to a <tt>String</tt> if found. If not found then the
189      * supplied <tt>defaultValue</tt> will be returned.
190      *
191      @param key          the key to search
192      @param defaultValue the value to be returned if the key is not found
193      */
194     @Nullable
195     String getAsString(@Nonnull String key, @Nullable String defaultValue);
196 
197     /**
198      * /**
199      * Finds a value associated with the given key. The value is
200      * blindly cast to type <tt>T</tt> if found.
201      *
202      @param key the key to search
203      @since 2.5.0
204      */
205     @Nullable
206     <T> T getAs(@Nonnull String key);
207 
208     /**
209      * Finds a value associated with the given key. The value is
210      * blindly cast to type <tt>T</tt> if found. If not found then the
211      * supplied <tt>defaultValue</tt> will be returned.
212      *
213      @param key          the key to search
214      @param defaultValue the value to be returned if the key is not found
215      @since 2.5.0
216      */
217     @Nullable
218     <T> T getAs(@Nonnull String key, @Nullable T defaultValue);
219 
220     /**
221      * Finds a value associated with the given key. The value is
222      * converted to type <tt>T</tt> if found using a {@code PropertyEditor}.
223      *
224      @param key  the key to search
225      @param type the type to be returned
226      @since 2.5.0
227      */
228     @Nullable
229     <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type);
230 
231     /**
232      * Finds a value associated with the given key. The value is
233      * converted to type <tt>T</tt> if found using a {@code PropertyEditor}.
234      * If not found then the supplied <tt>defaultValue</tt> will be returned.
235      *
236      @param key          the key to search
237      @param type         the type to be returned
238      @param defaultValue the value to be returned if the key is not found
239      @since 2.5.0
240      */
241     @Nullable
242     <T> T getConverted(@Nonnull String key, @Nonnull Class<T> type, @Nullable T defaultValue);
243 }