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