EMMA Coverage Report (generated Fri Feb 14 08:28:31 UTC 2014)
[all classes][org.qedeq.base.io]

COVERAGE SUMMARY FOR SOURCE FILE [Parameters.java]

nameclass, %method, %block, %line, %
Parameters.java100% (1/1)100% (14/14)100% (244/245)98%  (57/58)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Parameters100% (1/1)100% (14/14)100% (244/245)98%  (57/58)
getInt (String, int): int 100% (1/1)94%  (15/16)80%  (4/5)
<static initializer> 100% (1/1)100% (5/5)100% (1/1)
Parameters (): void 100% (1/1)100% (8/8)100% (3/3)
Parameters (Map): void 100% (1/1)100% (13/13)100% (5/5)
getBoolean (String): boolean 100% (1/1)100% (15/15)100% (4/4)
getBoolean (String, boolean): boolean 100% (1/1)100% (24/24)100% (7/7)
getInt (String): int 100% (1/1)100% (16/16)100% (5/5)
getParameterString (): String 100% (1/1)100% (50/50)100% (14/14)
getString (String): String 100% (1/1)100% (13/13)100% (2/2)
getString (String, String): String 100% (1/1)100% (13/13)100% (2/2)
keySet (): Set 100% (1/1)100% (4/4)100% (1/1)
setDefault (String, String): void 100% (1/1)100% (18/18)100% (3/3)
setDefault (String, boolean): void 100% (1/1)100% (25/25)100% (3/3)
setDefault (String, int): void 100% (1/1)100% (25/25)100% (3/3)

1/* This file is part of the project "Hilbert II" - http://www.qedeq.org
2 *
3 * Copyright 2000-2014,  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 
16package org.qedeq.base.io;
17 
18import java.util.HashMap;
19import java.util.Iterator;
20import java.util.Map;
21import 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 */
30public 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    public Parameters(final Map map) {
44        if (map == null) {
45            throw new NullPointerException("Map must not be null.");
46        }
47        this.map = map;
48    }
49 
50    /**
51     * Constructs parameter access object.
52     */
53    public Parameters() {
54        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    public String getString(final String key) {
65        Object oval = map.get(key);
66        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    public String getString(final String key, final String def) {
79        Object oval = map.get(key);
80        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    public int getInt(final String key) {
92        final Object oval = map.get(key);
93        if (oval instanceof String) {
94            try {
95                return Integer.parseInt(oval.toString().trim());
96            } catch (NumberFormatException ex) {
97                // ignore
98            }
99        }
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((String) oval)) {
151                return true;
152            }
153            if ("false".equalsIgnoreCase((String) oval)) {
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.Entry) e.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(key) instanceof 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(key) instanceof 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(key) instanceof 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}

[all classes][org.qedeq.base.io]
EMMA 2.1.5320 (stable) (C) Vladimir Roubtsov