1
2
3
4
5
6
7
8
9
10
11
12
13
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
26
27
28
29
30 public class Parameters {
31
32
33 private final Map map;
34
35
36 public static final Parameters EMPTY = new Parameters();
37
38
39
40
41
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
52
53 public Parameters() {
54 this.map = new HashMap();
55 }
56
57
58
59
60
61
62
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
72
73
74
75
76
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
86
87
88
89
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
98 }
99 }
100 return 0;
101 }
102
103
104
105
106
107
108
109
110
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
119 }
120 }
121 return def;
122 }
123
124
125
126
127
128
129
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
141
142
143
144
145
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
162
163
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
186
187
188
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
198
199
200
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
210
211
212
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
222
223
224
225 public Set keySet() {
226 return map.keySet();
227 }
228 }