Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
49   228   32   3.77
28   101   0.65   13
13     2.46  
1    
 
  Parameters       Line # 30 49 32 100% 1.0
 
  (63)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.io;
17   
18    import java.util.HashMap;
19    import java.util.Iterator;
20    import java.util.Map;
21    import java.util.Set;
22   
23   
24    /**
25    * Provides convenience methods for getting parameters out of a {@link Map}.
26    * The keys and the values are stored as Strings.
27    *
28    * @author Michael Meyling
29    */
 
30    public class Parameters {
31   
32    /** In this map our values are stored. */
33    private final Map map;
34   
35    /** Empty parameters. */
36    public static final Parameters EMPTY = new Parameters();
37   
38    /**
39    * Constructs parameter access object.
40    *
41    * @param map Herein are the parameters. Must not be <code>null</code>.
42    */
 
43  6809 toggle public Parameters(final Map map) {
44  6809 if (map == null) {
45  1 throw new NullPointerException("Map must not be null.");
46    }
47  6808 this.map = map;
48    }
49   
50    /**
51    * Constructs parameter access object.
52    */
 
53  46 toggle public Parameters() {
54  46 this.map = new HashMap();
55    }
56   
57    /**
58    * Searches for the value with the specified key.
59    * If the key has no String value an empty String is returned.
60    *
61    * @param key The key we want a value for.
62    * @return The value for the specified key value.
63    */
 
64  15935 toggle public String getString(final String key) {
65  15935 Object oval = map.get(key);
66  15935 return (oval instanceof String) ? (String) oval : "";
67    }
68   
69   
70    /**
71    * Searches for the value with the specified key.
72    * If the key has no String value <code>def</code> is returned.
73    *
74    * @param key The key we want a value for.
75    * @param def The default value we get if we have no String value.
76    * @return The value for the specified key value.
77    */
 
78  7 toggle public String getString(final String key, final String def) {
79  7 Object oval = map.get(key);
80  7 return (oval instanceof String) ? (String) oval : def;
81    }
82   
83   
84    /**
85    * Searches for the value with the specified key.
86    * If the key has no int value 0 is returned.
87    *
88    * @param key The key we want a value for.
89    * @return The value for the specified key value.
90    */
 
91  161 toggle public int getInt(final String key) {
92  161 final Object oval = map.get(key);
93  161 if (oval instanceof String) {
94  151 try {
95  151 return Integer.parseInt(oval.toString().trim());
96    } catch (NumberFormatException ex) {
97    // ignore
98    }
99    }
100  11 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  7 toggle public int getInt(final String key, final int def) {
113  7 final Object oval = map.get(key);
114  7 if (oval instanceof String) {
115  3 try {
116  3 return Integer.parseInt(oval.toString().trim());
117    } catch (NumberFormatException ex) {
118    // ignore
119    }
120    }
121  4 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  134 toggle public boolean getBoolean(final String key) {
132  134 final Object oval = map.get(key);
133  134 if (oval instanceof String) {
134  91 return "true".equalsIgnoreCase(oval.toString());
135    }
136  43 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  10 toggle public boolean getBoolean(final String key, final boolean def) {
148  10 final Object oval = map.get(key);
149  10 if (oval instanceof String) {
150  5 if ("true".equalsIgnoreCase((String) oval)) {
151  3 return true;
152    }
153  2 if ("false".equalsIgnoreCase((String) oval)) {
154  1 return false;
155    }
156    }
157  6 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  7 toggle public String getParameterString() {
166  7 final StringBuffer buffer = new StringBuffer(30);
167  7 Iterator e = map.entrySet().iterator();
168  7 boolean notFirst = false;
169  14 while (e.hasNext()) {
170  7 final Map.Entry entry = (Map.Entry) e.next();
171  7 String key = String.valueOf(entry.getKey());
172  7 if (notFirst) {
173  2 buffer.append(", ");
174    } else {
175  5 notFirst = true;
176    }
177  7 buffer.append(key);
178  7 buffer.append("=");
179  7 buffer.append(String.valueOf(entry.getValue()));
180    }
181  7 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  8953 toggle public void setDefault(final String key, final int value) {
191  8953 if (!map.containsKey(key) || !(map.get(key) instanceof String)) {
192  22 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  3303 toggle public void setDefault(final String key, final String value) {
203  3303 if (!map.containsKey(key) || !(map.get(key) instanceof String)) {
204  12 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  3300 toggle public void setDefault(final String key, final boolean value) {
215  3300 if (!map.containsKey(key) || !(map.get(key) instanceof String)) {
216  10 map.put(key, "" + value);
217    }
218    }
219   
220    /**
221    * Return key set for internal map.
222    *
223    * @return Key set.
224    */
 
225  4722 toggle public Set keySet() {
226  4722 return map.keySet();
227    }
228    }