Parameters.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.base.io;
017 
018 import java.util.HashMap;
019 import java.util.Iterator;
020 import java.util.Map;
021 import java.util.Set;
022 
023 
024 /**
025  * Provides convenience methods for getting parameters out of a {@link Map}.
026  *
027  @author  Michael Meyling
028  */
029 public class Parameters {
030 
031     /** In this map our values are stored. */
032     private final Map map;
033 
034     /**
035      * Constructs parameter access object.
036      *
037      @param   map     Herein are the parameters.
038      */
039     public Parameters(final Map map) {
040         this.map = map;
041     }
042 
043     public Parameters() {
044         this.map = new HashMap();
045     }
046 
047     /**
048      * Searches for the value with the specified key.
049      * If the key has no String value an empty String is returned.
050      *
051      @param   key   The key we want a value for.
052      @return  The value for the specified key value.
053      */
054     public String getString(final String key) {
055         Object oval = map.get(key);
056         return (oval instanceof String(Stringoval : "";
057     }
058 
059 
060     /**
061      * Searches for the value with the specified key.
062      * If the key has no String value <code>def</code> is returned.
063      *
064      @param   key   The key we want a value for.
065      @param   def   The default value we get if we have no String value.
066      @return  The value for the specified key value.
067      */
068     public String getString(final String key, final String def) {
069         Object oval = map.get(key);
070         return (oval instanceof String(Stringoval : def;
071     }
072 
073 
074     /**
075      * Searches for the value with the specified key.
076      * If the key has no int value 0 is returned.
077      *
078      @param   key   The key we want a value for.
079      @return  The value for the specified key value.
080      */
081     public int getInt(final String key) {
082         final Object oval = map.get(key);
083         if (oval instanceof String) {
084             try {
085                 return Integer.parseInt(oval.toString().trim());
086             catch (NumberFormatException ex) {
087                 // ignore
088             }
089         }
090         return 0;
091     }
092 
093 
094     /**
095      * Searches for the value with the specified key.
096      * If the key has no int value <code>def</code> is returned.
097      *
098      @param   key   The key we want a value for.
099      @param   def   The default value we get if we have no String value.
100      @return  The value for the specified key value.
101      */
102     public int getInt(final String key, final int def) {
103         final Object oval = map.get(key);
104         if (oval instanceof String) {
105             try {
106                 return Integer.parseInt(oval.toString().trim());
107             catch (NumberFormatException ex) {
108                 // ignore
109             }
110         }
111         return def;
112     }
113 
114     /**
115      * Searches for the value with the specified key.
116      * If the key has no boolean value <code>false</code> is returned.
117      *
118      @param   key   The key we want a value for.
119      @return  The value for the specified key value.
120      */
121     public boolean getBoolean(final String key) {
122         final Object oval = map.get(key);
123         if (oval instanceof String) {
124             return "true".equalsIgnoreCase(oval.toString());
125         }
126         return false;
127     }
128 
129     /**
130      * Searches for the value with the specified key.
131      * If the key has no boolean value <code>def</code> is returned.
132      *
133      @param   key   The key we want a value for.
134      @param   def   The default value we get if we have no String value.
135      @return  The value for the specified key value.
136      */
137     public boolean getBoolean(final String key, final boolean def) {
138         final Object oval = map.get(key);
139         if (oval instanceof String) {
140             if ("true".equalsIgnoreCase((Stringoval)) {
141                 return true;
142             }
143             if ("false".equalsIgnoreCase((Stringoval)) {
144                 return false;
145             }
146         }
147         return def;
148     }
149 
150     /**
151      * Get all parameters as a long string.
152      *
153      @return  String in form "a=b, c=d" and so on.
154      */
155     public String getParameterString() {
156         final StringBuffer buffer = new StringBuffer(30);
157         if (map != null) {
158             Iterator e = map.entrySet().iterator();
159             boolean notFirst = false;
160             while (e.hasNext()) {
161                 final Map.Entry entry = (Map.Entrye.next();
162                 String key = String.valueOf(entry.getKey());
163                 if (notFirst) {
164                     buffer.append(", ");
165                 else {
166                     notFirst = true;
167                 }
168                 buffer.append(key);
169                 buffer.append("=");
170                 buffer.append(String.valueOf(entry.getValue()));
171             }
172         }
173         return buffer.toString();
174     }
175 
176     /**
177      * Set default configuration parameter if the key has still no value.
178      *
179      @param   key         Key we want to check.
180      @param   value       Default value.
181      */
182     public void setDefault(final String key, final int value) {
183         if (!map.containsKey(key)) {
184             map.put(key, "" + value);
185         }
186     }
187 
188     /**
189      * Set default configuration parameter if the key has still no value.
190      *
191      @param   key         Key we want to check.
192      @param   value       Default value.
193      */
194     public void setDefault(final String key, final String value) {
195         if (!map.containsKey(key)) {
196             map.put(key, value);
197         }
198     }
199 
200     /**
201      * Set default configuration parameter if the key has still no value.
202      *
203      @param   key         Key we want to check.
204      @param   value       Default value.
205      */
206     public void setDefault(final String key, final boolean value) {
207         if (!map.containsKey(key)) {
208             map.put(key, "" + value);
209         }
210     }
211 
212     public Set keySet() {
213         return map.keySet();
214     }
215 }