Mercurial > hg > digilib-old
annotate common/src/main/java/digilib/util/ParameterMap.java @ 1158:2ee261676828 default tip
better out-of-the box experience:
* digilib works without config files using sensible defaults
* new sample images folder used by default
* config files moved to templates
| author | robcast |
|---|---|
| date | Tue, 19 Feb 2013 17:32:25 +0100 |
| parents | 7779b37d1d05 |
| children |
| rev | line source |
|---|---|
| 153 | 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 */ | |
| 557 | 22 package digilib.util; |
| 153 | 23 |
| 24 import java.util.HashMap; | |
| 557 | 25 |
| 153 | 26 |
| 27 /** HashMap of digilib.servlet.Parameter's. | |
| 28 * | |
| 29 * Keys are Strings. Values are Parameters. | |
| 30 * | |
| 31 * @author casties | |
| 32 * | |
| 33 */ | |
| 547 | 34 public class ParameterMap { |
| 153 | 35 |
| 547 | 36 protected HashMap<String, Parameter> params; |
| 37 | |
| 38 protected OptionsSet options; | |
| 39 | |
| 153 | 40 /** Default constructor. |
| 41 * | |
| 42 */ | |
| 43 public ParameterMap() { | |
| 547 | 44 params = new HashMap<String, Parameter>(); |
| 45 options = new OptionsSet(); | |
| 552 | 46 initParams(); |
| 153 | 47 } |
| 48 | |
| 547 | 49 /** Constructor with initial size. |
| 552 | 50 * @param size |
| 153 | 51 */ |
| 552 | 52 public ParameterMap(int size) { |
| 53 params = new HashMap<String, Parameter>(size); | |
| 547 | 54 options = new OptionsSet(); |
| 552 | 55 initParams(); |
| 547 | 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") | |
| 557 | 63 public static ParameterMap cloneInstance(ParameterMap pm) { |
| 551 | 64 ParameterMap newPm = new ParameterMap(); |
| 552 | 65 // clone params to this map |
| 551 | 66 newPm.params = (HashMap<String, Parameter>) pm.params.clone(); |
| 67 newPm.options = (OptionsSet) pm.options.clone(); | |
| 68 return newPm; | |
| 153 | 69 } |
| 70 | |
| 552 | 71 |
| 72 /** Creates new ParameterMap by merging Parameters from another ParameterMap. | |
| 73 * @param pm | |
| 74 * @return | |
| 75 */ | |
| 557 | 76 public static ParameterMap getInstance(ParameterMap pm) { |
| 552 | 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 | |
| 153 | 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) { | |
| 547 | 106 return params.get(key); |
| 153 | 107 } |
| 108 | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
109 /** Get the Parameter with the corresponding key. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
110 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
111 * Returns null if no element is associated with key. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
112 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
113 * @param key |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
114 * @return |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
115 */ |
| 153 | 116 public Object getValue(String key) { |
| 547 | 117 Parameter p = params.get(key); |
| 153 | 118 return (p != null) ? p.getValue() : null; |
| 119 } | |
| 120 | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
121 /** Get the Parameter with the corresponding key. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
122 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
123 * Returns null if no element is associated with key. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
124 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
125 * @param key |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
126 * @return |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
127 */ |
| 153 | 128 public String getAsString(String key) { |
| 547 | 129 Parameter p = params.get(key); |
| 555 | 130 return (p != null) ? p.getAsString() : ""; |
| 153 | 131 } |
| 132 | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
133 /** Get the Parameter with the corresponding key. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
134 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
135 * Returns null if no element is associated with key. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
136 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
137 * @param key |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
138 * @return |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
139 */ |
| 153 | 140 public int getAsInt(String key) { |
| 547 | 141 Parameter p = params.get(key); |
| 153 | 142 return (p != null) ? p.getAsInt() : 0; |
| 143 } | |
| 144 | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
145 /** Get the Parameter with the corresponding key. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
146 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
147 * Returns null if no element is associated with key. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
148 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
149 * @param key |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
150 * @return |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
151 */ |
| 153 | 152 public float getAsFloat(String key) { |
| 547 | 153 Parameter p = params.get(key); |
| 153 | 154 return (p != null) ? p.getAsFloat() : 0f; |
| 155 } | |
| 156 | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
157 /** Get the Parameter with the corresponding key. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
158 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
159 * Returns null if no element is associated with key. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
160 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
161 * @param key |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
162 * @return |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
163 */ |
| 153 | 164 public boolean getAsBoolean(String key) { |
| 547 | 165 Parameter p = params.get(key); |
| 153 | 166 return (p != null) ? p.getAsBoolean() : false; |
| 167 } | |
| 168 | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
169 /** Returns if the Parameter's value has been set. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
170 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
171 * @param key |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
172 * @return |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
173 */ |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
174 public boolean hasValue(String key) { |
| 547 | 175 Parameter p = params.get(key); |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
176 return (p != null) ? p.hasValue() : false; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
177 } |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
178 |
| 153 | 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) { | |
| 547 | 188 return params.put(key, val); |
| 153 | 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) { | |
| 547 | 199 return params.put(val.getName(), val); |
| 153 | 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 */ | |
| 185 | 211 public Parameter newParameter(String name, Object def, Object val) { |
| 153 | 212 Parameter p = new Parameter(name, def, val); |
| 547 | 213 return params.put(name, p); |
| 153 | 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 */ | |
| 185 | 226 public Parameter newParameter(String name, Object def, Object val, int type) { |
| 153 | 227 Parameter p = new Parameter(name, def, val, type); |
| 547 | 228 return params.put(name, p); |
| 153 | 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) { | |
| 547 | 240 Parameter p = params.get(key); |
| 153 | 241 if (p != null) { |
| 242 p.setValue(val); | |
| 243 return true; | |
| 244 } | |
| 245 return false; | |
| 246 } | |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
247 |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
248 /** Set the value of an existing parameter. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
249 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
250 * Sets the value and returns true if the parameter exists. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
251 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
252 * @param key |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
253 * @param val |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
254 * @return |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
255 */ |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
256 public boolean setValue(String key, int val) { |
| 547 | 257 Parameter p = params.get(key); |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
258 if (p != null) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
259 p.setValue(val); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
260 return true; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
261 } |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
262 return false; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
263 } |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
264 |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
265 /** Set the value of an existing parameter. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
266 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
267 * Sets the value and returns true if the parameter exists. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
268 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
269 * @param key |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
270 * @param val |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
271 * @return |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
272 */ |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
273 public boolean setValue(String key, float val) { |
| 547 | 274 Parameter p = params.get(key); |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
275 if (p != null) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
276 p.setValue(val); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
277 return true; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
278 } |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
279 return false; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
280 } |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
281 |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
282 /** Set the value of an existing parameter. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
283 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
284 * Sets the value and returns true if the parameter exists. |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
285 * |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
286 * @param key |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
287 * @param val |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
288 * @return |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
289 */ |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
290 public boolean setValueFromString(String key, String val) { |
| 547 | 291 Parameter p = params.get(key); |
|
170
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
292 if (p != null) { |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
293 p.setValueFromString(val); |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
294 return true; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
295 } |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
296 return false; |
|
d40922628e4a
Servlet Version 1.16b2 with new DigilibParameter code.
robcast
parents:
153
diff
changeset
|
297 } |
| 547 | 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 } | |
| 548 | 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 } | |
| 153 | 322 } |
