comparison common/src/main/java/digilib/util/ParameterMap.java @ 903:7779b37d1d05

refactored into maven modules per servlet type. can build servlet-api 2.3 and 3.0 via profile now!
author robcast
date Tue, 26 Apr 2011 20:24:31 +0200
parents servlet/src/main/java/digilib/util/ParameterMap.java@ba1eb2d821a2
children
comparison
equal deleted inserted replaced
902:89ba3ffcf552 903:7779b37d1d05
1 /* ParameterMap.java -- HashMap of Parameters.
2
3 Digital Image Library servlet components
4
5 Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de)
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 Please read license.txt for the full details. A copy of the GPL
13 may be found at http://www.gnu.org/copyleft/lgpl.html
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 * Created on 02.09.2003 by casties
20 *
21 */
22 package digilib.util;
23
24 import java.util.HashMap;
25
26
27 /** HashMap of digilib.servlet.Parameter's.
28 *
29 * Keys are Strings. Values are Parameters.
30 *
31 * @author casties
32 *
33 */
34 public class ParameterMap {
35
36 protected HashMap<String, Parameter> params;
37
38 protected OptionsSet options;
39
40 /** Default constructor.
41 *
42 */
43 public ParameterMap() {
44 params = new HashMap<String, Parameter>();
45 options = new OptionsSet();
46 initParams();
47 }
48
49 /** Constructor with initial size.
50 * @param size
51 */
52 public ParameterMap(int size) {
53 params = new HashMap<String, Parameter>(size);
54 options = new OptionsSet();
55 initParams();
56 }
57
58 /** Shallow copy constructor.
59 * Be warned that the maps are only cloned i.e. keys and values are shared!
60 * @param pm
61 */
62 @SuppressWarnings("unchecked")
63 public static ParameterMap cloneInstance(ParameterMap pm) {
64 ParameterMap newPm = new ParameterMap();
65 // clone params to this map
66 newPm.params = (HashMap<String, Parameter>) pm.params.clone();
67 newPm.options = (OptionsSet) pm.options.clone();
68 return newPm;
69 }
70
71
72 /** Creates new ParameterMap by merging Parameters from another ParameterMap.
73 * @param pm
74 * @return
75 */
76 public static ParameterMap getInstance(ParameterMap pm) {
77 ParameterMap newPm = new ParameterMap();
78 // add all params to this map
79 newPm.params.putAll(pm.params);
80 newPm.initOptions();
81 return newPm;
82 }
83
84 /** set up parameters
85 *
86 */
87 protected void initParams() {
88 // no default parameters
89 }
90
91 /** set up options
92 *
93 */
94 protected void initOptions() {
95 // no default options
96 }
97
98 /** Get the Parameter with the corresponding key.
99 *
100 * Returns null if no element is associated with key.
101 *
102 * @param key
103 * @return
104 */
105 public Parameter get(String key) {
106 return params.get(key);
107 }
108
109 /** Get the Parameter with the corresponding key.
110 *
111 * Returns null if no element is associated with key.
112 *
113 * @param key
114 * @return
115 */
116 public Object getValue(String key) {
117 Parameter p = params.get(key);
118 return (p != null) ? p.getValue() : null;
119 }
120
121 /** Get the Parameter with the corresponding key.
122 *
123 * Returns null if no element is associated with key.
124 *
125 * @param key
126 * @return
127 */
128 public String getAsString(String key) {
129 Parameter p = params.get(key);
130 return (p != null) ? p.getAsString() : "";
131 }
132
133 /** Get the Parameter with the corresponding key.
134 *
135 * Returns null if no element is associated with key.
136 *
137 * @param key
138 * @return
139 */
140 public int getAsInt(String key) {
141 Parameter p = params.get(key);
142 return (p != null) ? p.getAsInt() : 0;
143 }
144
145 /** Get the Parameter with the corresponding key.
146 *
147 * Returns null if no element is associated with key.
148 *
149 * @param key
150 * @return
151 */
152 public float getAsFloat(String key) {
153 Parameter p = params.get(key);
154 return (p != null) ? p.getAsFloat() : 0f;
155 }
156
157 /** Get the Parameter with the corresponding key.
158 *
159 * Returns null if no element is associated with key.
160 *
161 * @param key
162 * @return
163 */
164 public boolean getAsBoolean(String key) {
165 Parameter p = params.get(key);
166 return (p != null) ? p.getAsBoolean() : false;
167 }
168
169 /** Returns if the Parameter's value has been set.
170 *
171 * @param key
172 * @return
173 */
174 public boolean hasValue(String key) {
175 Parameter p = params.get(key);
176 return (p != null) ? p.hasValue() : false;
177 }
178
179 /** Add the Parameter to the map with a certain key.
180 *
181 * Returns the value that was previously associated with key.
182 *
183 * @param key
184 * @param val
185 * @return
186 */
187 public Parameter put(String key, Parameter val) {
188 return params.put(key, val);
189 }
190
191 /** Add the Parameter val to the map, using val's name.
192 *
193 * Returns the value that was previously associated with val's name.
194 *
195 * @param val
196 * @return
197 */
198 public Parameter put(Parameter val) {
199 return params.put(val.getName(), val);
200 }
201
202 /** Add a new Parameter with name, default and value.
203 *
204 * Returns the key that was previously associated with name.
205 *
206 * @param name
207 * @param def
208 * @param val
209 * @return
210 */
211 public Parameter newParameter(String name, Object def, Object val) {
212 Parameter p = new Parameter(name, def, val);
213 return params.put(name, p);
214 }
215
216 /** Add a new Parameter with name, default, value and type.
217 *
218 * Returns the key that was previously associated with name.
219 *
220 * @param name
221 * @param def
222 * @param val
223 * @param type
224 * @return
225 */
226 public Parameter newParameter(String name, Object def, Object val, int type) {
227 Parameter p = new Parameter(name, def, val, type);
228 return params.put(name, p);
229 }
230
231 /** Set the value of an existing parameter.
232 *
233 * Sets the value and returns true if the parameter exists.
234 *
235 * @param key
236 * @param val
237 * @return
238 */
239 public boolean setValue(String key, Object val) {
240 Parameter p = params.get(key);
241 if (p != null) {
242 p.setValue(val);
243 return true;
244 }
245 return false;
246 }
247
248 /** Set the value of an existing parameter.
249 *
250 * Sets the value and returns true if the parameter exists.
251 *
252 * @param key
253 * @param val
254 * @return
255 */
256 public boolean setValue(String key, int val) {
257 Parameter p = params.get(key);
258 if (p != null) {
259 p.setValue(val);
260 return true;
261 }
262 return false;
263 }
264
265 /** Set the value of an existing parameter.
266 *
267 * Sets the value and returns true if the parameter exists.
268 *
269 * @param key
270 * @param val
271 * @return
272 */
273 public boolean setValue(String key, float val) {
274 Parameter p = params.get(key);
275 if (p != null) {
276 p.setValue(val);
277 return true;
278 }
279 return false;
280 }
281
282 /** Set the value of an existing parameter.
283 *
284 * Sets the value and returns true if the parameter exists.
285 *
286 * @param key
287 * @param val
288 * @return
289 */
290 public boolean setValueFromString(String key, String val) {
291 Parameter p = params.get(key);
292 if (p != null) {
293 p.setValueFromString(val);
294 return true;
295 }
296 return false;
297 }
298
299 /** Returns of the option has been set.
300 * @param opt
301 * @return
302 */
303 public boolean hasOption(String opt) {
304 return options.hasOption(opt);
305 }
306
307 public HashMap<String, Parameter> getParams() {
308 return params;
309 }
310
311 public void setParams(HashMap<String, Parameter> params) {
312 this.params = params;
313 }
314
315 public OptionsSet getOptions() {
316 return options;
317 }
318
319 public void setOptions(OptionsSet options) {
320 this.options = options;
321 }
322 }