Parameters.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2014,  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  * The keys and the values are stored as Strings.
027  *
028  @author  Michael Meyling
029  */
030 public class Parameters {
031 
032     /** In this map our values are stored. */
033     private final Map map;
034 
035     /** Empty parameters. */
036     public static final Parameters EMPTY = new Parameters();
037 
038     /**
039      * Constructs parameter access object.
040      *
041      @param   map     Herein are the parameters. Must not be <code>null</code>.
042      */
043     public Parameters(final Map map) {
044         if (map == null) {
045             throw new NullPointerException("Map must not be null.");
046         }
047         this.map = map;
048     }
049 
050     /**
051      * Constructs parameter access object.
052      */
053     public Parameters() {
054         this.map = new HashMap();
055     }
056 
057     /**
058      * Searches for the value with the specified key.
059      * If the key has no String value an empty String is returned.
060      *
061      @param   key   The key we want a value for.
062      @return  The value for the specified key value.
063      */
064     public String getString(final String key) {
065         Object oval = map.get(key);
066         return (oval instanceof String(Stringoval : "";
067     }
068 
069 
070     /**
071      * Searches for the value with the specified key.
072      * If the key has no String value <code>def</code> is returned.
073      *
074      @param   key   The key we want a value for.
075      @param   def   The default value we get if we have no String value.
076      @return  The value for the specified key value.
077      */
078     public String getString(final String key, final String def) {
079         Object oval = map.get(key);
080         return (oval instanceof String(Stringoval : def;
081     }
082 
083 
084     /**
085      * Searches for the value with the specified key.
086      * If the key has no int value 0 is returned.
087      *
088      @param   key   The key we want a value for.
089      @return  The value for the specified key value.
090      */
091     public int getInt(final String key) {
092         final Object oval = map.get(key);
093         if (oval instanceof String) {
094             try {
095                 return Integer.parseInt(oval.toString().trim());
096             catch (NumberFormatException ex) {
097                 // ignore
098             }
099         }
100         return 0;
101     }
102 
103 
104     /**
105      * Searches for the value with the specified key.
106      * If the key has no int value <code>def</code> is returned.
107      *
108      @param   key   The key we want a value for.
109      @param   def   The default value we get if we have no String value.
110      @return  The value for the specified key value.
111      */
112     public int getInt(final String key, final int def) {
113         final Object oval = map.get(key);
114         if (oval instanceof String) {
115             try {
116                 return Integer.parseInt(oval.toString().trim());
117             catch (NumberFormatException ex) {
118                 // ignore
119             }
120         }
121         return def;
122     }
123 
124     /**
125      * Searches for the value with the specified key.
126      * If the key has no boolean value <code>false</code> is returned.
127      *
128      @param   key   The key we want a value for.
129      @return  The value for the specified key value.
130      */
131     public boolean getBoolean(final String key) {
132         final Object oval = map.get(key);
133         if (oval instanceof String) {
134             return "true".equalsIgnoreCase(oval.toString());
135         }
136         return false;
137     }
138 
139     /**
140      * Searches for the value with the specified key.
141      * If the key has no boolean value <code>def</code> is returned.
142      *
143      @param   key   The key we want a value for.
144      @param   def   The default value we get if we have no String value.
145      @return  The value for the specified key value.
146      */
147     public boolean getBoolean(final String key, final boolean def) {
148         final Object oval = map.get(key);
149         if (oval instanceof String) {
150             if ("true".equalsIgnoreCase((Stringoval)) {
151                 return true;
152             }
153             if ("false".equalsIgnoreCase((Stringoval)) {
154                 return false;
155             }
156         }
157         return def;
158     }
159 
160     /**
161      * Get all parameters as a long string. Strings are not quoted.
162      *
163      @return  String in form "a=b, c=d" and so on.
164      */
165     public String getParameterString() {
166         final StringBuffer buffer = new StringBuffer(30);
167         Iterator e = map.entrySet().iterator();
168         boolean notFirst = false;
169         while (e.hasNext()) {
170             final Map.Entry entry = (Map.Entrye.next();
171             String key = String.valueOf(entry.getKey());
172             if (notFirst) {
173                 buffer.append(", ");
174             else {
175                 notFirst = true;
176             }
177             buffer.append(key);
178             buffer.append("=");
179             buffer.append(String.valueOf(entry.getValue()));
180         }
181         return buffer.toString();
182     }
183 
184     /**
185      * Set default configuration parameter if the key has still no value.
186      *
187      @param   key         Key we want to check.
188      @param   value       Default value.
189      */
190     public void setDefault(final String key, final int value) {
191         if (!map.containsKey(key|| !(map.get(keyinstanceof String)) {
192             map.put(key, "" + value);
193         }
194     }
195 
196     /**
197      * Set default configuration parameter if the key has still no value.
198      *
199      @param   key         Key we want to check.
200      @param   value       Default value.
201      */
202     public void setDefault(final String key, final String value) {
203         if (!map.containsKey(key|| !(map.get(keyinstanceof String)) {
204             map.put(key, value);
205         }
206     }
207 
208     /**
209      * Set default configuration parameter if the key has still no value.
210      *
211      @param   key         Key we want to check.
212      @param   value       Default value.
213      */
214     public void setDefault(final String key, final boolean value) {
215         if (!map.containsKey(key|| !(map.get(keyinstanceof String)) {
216             map.put(key, "" + value);
217         }
218     }
219 
220     /**
221      * Return key set for internal map.
222      *
223      @return  Key set.
224      */
225     public Set keySet() {
226         return map.keySet();
227     }
228 }