View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15   */
16  package net.sf.eos.config;
17  
18  
19  import static java.lang.Boolean.FALSE;
20  import static java.lang.Boolean.TRUE;
21  
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * A simple name value configuration handler.
32   * @author Sascha Kohlmann
33   */
34  public class Configuration implements Iterable<Entry<String, String>> {
35  
36      static final Log LOG = LogFactory.getLog(Configuration.class.getName());
37  
38      private final Map<String, String> config = new HashMap<String, String>();
39  
40      /** Creates a new instance. */
41      public Configuration() {
42          ;
43      }
44  
45      /** 
46       * Copy constructor.
47       * @param toCopy the {@code Configuration} to copy.
48       */
49      public Configuration(final Configuration toCopy) {
50          for (final Entry<String, String> entry : toCopy.config.entrySet()) {
51              final String key = entry.getKey();
52              final String value = entry.getValue();
53              this.config.put(key, value);
54          }
55      }
56  
57      /**
58       * Puts the configuration for the name and the value
59       * @param name the name of the configuration value
60       * @param value the value of the configuration name.
61       */
62      public void set(final String name, final String value) {
63          assert this.config != null;
64          this.config.put(name, value);
65      }
66  
67      /**
68       * Returns a value for the given name.
69       * @param name the name to look up for a value
70       * @return a value or {@code null}
71       */
72      public String get(final String name) {
73          assert this.config != null;
74          return this.config.get(name);
75      }
76  
77      /**
78       * Returns a value for the given name.
79       * @param name the name to look up for a value
80       * @param defaultValue a default value
81       * @return a value or {@code null}
82       */
83      public String get(final String name, final String defaultValue) {
84          assert this.config != null;
85          final String retval = this.config.get(name);
86          return retval != null ? retval : defaultValue;
87      }
88  
89      /**
90       * Returns an iterator for the configuration entries.
91       * @return an iterator for the configuration entries
92       */
93      public Iterator<Entry<String, String>> iterator() {
94          assert this.config != null;
95          return this.config.entrySet().iterator();
96      }
97  
98      @Override
99      public String toString() {
100         final StringBuilder sb = new StringBuilder("Configuration[\n");
101         for (final Entry<String, String> entry : this) {
102             sb.append("        ");
103             sb.append(entry.getKey());
104             sb.append("  =  ");
105             sb.append(entry.getValue());
106             sb.append("\n");
107         }
108         sb.append("]");
109 
110         return sb.toString();
111     }
112 
113     /**
114      * Returns a value for the given name as {@code boolean}
115      * @param name the name to look up for a value
116      * @param defaultValue a default value
117      * @return the value
118      * @see ConfigurationKey.Type#BOOLEAN
119      */
120     public boolean getBoolean(final String name, final boolean defaultValue) {
121         assert this.config != null;
122         final String retval = get(name);
123 
124         if (TRUE.toString().equalsIgnoreCase(retval)) {
125             return true;
126         } else if (FALSE.toString().equalsIgnoreCase(retval)) {
127             return false;
128         }
129 
130         if (LOG.isDebugEnabled()) {
131             LOG.debug("Use default value " + defaultValue + " for key " + name);
132         }
133         return defaultValue;
134     }
135 
136     /**
137      * Returns a value for the given name as {@code int}
138      * @param name the name to look up for a value
139      * @param defaultValue a default value
140      * @return the value
141      * @see ConfigurationKey.Type#INTEGER
142      */
143     public int getInt(final String name, final int defaultValue) {
144         assert this.config != null;
145         final String retval = get(name);
146 
147         if (retval == null) {
148             if (LOG.isDebugEnabled()) {
149                 LOG.debug("Null value for key " + name + " - use default "
150                           + defaultValue);
151             }
152             return defaultValue;
153         }
154 
155         try {
156             return Integer.parseInt(retval);
157         } catch (final NumberFormatException e) {
158             if (LOG.isDebugEnabled()) {
159                 LOG.debug("Unable to parse value '" + retval + "' of key " + name 
160                           + " - use default " + defaultValue);
161             }
162             return defaultValue;
163         }
164     }
165 
166     /**
167      * Returns a value for the given name as {@code float}
168      * @param name the name to look up for a value
169      * @param defaultValue a default value
170      * @return the value
171      * @see ConfigurationKey.Type#FLOAT
172      */
173     public float getFloat(final String name, final float defaultValue) {
174         assert this.config != null;
175         final String retval = get(name);
176 
177         if (retval == null) {
178             if (LOG.isDebugEnabled()) {
179                 LOG.debug("Null value for key " + name + " - use default "
180                           + defaultValue);
181              }
182             return defaultValue;
183         }
184 
185         try {
186             return Float.parseFloat(retval);
187         } catch (final NumberFormatException e) {
188             if (LOG.isDebugEnabled()) {
189                 LOG.debug("Unable to parse value '" + retval + "' of key " + name
190                           + " - use default " + defaultValue);
191             }
192             return defaultValue;
193         }
194     }
195 }