|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.misc; |
|
27 |
|
28 import java.awt.*; |
|
29 import java.util.*; |
|
30 import java.util.List; |
|
31 import com.oracle.solaris.vp.util.misc.converter.*; |
|
32 |
|
33 @SuppressWarnings({"serial"}) |
|
34 public class TypedPropertiesWrapper { |
|
35 // |
|
36 // Instance data |
|
37 // |
|
38 |
|
39 private List<Properties> pList = new ArrayList<Properties>(); |
|
40 |
|
41 // |
|
42 // Constructors |
|
43 // |
|
44 |
|
45 public TypedPropertiesWrapper(Properties... properties) { |
|
46 for (Properties p : properties) { |
|
47 pList.add(p); |
|
48 } |
|
49 } |
|
50 |
|
51 // |
|
52 // TypedPropertiesWrapper methods |
|
53 // |
|
54 |
|
55 /** |
|
56 * Retrieves the first property of the given key from the given list and |
|
57 * converts it to a {@code boolean}. |
|
58 * |
|
59 * @param key |
|
60 * the property key |
|
61 * |
|
62 * @return a {@code boolean} |
|
63 * |
|
64 * @exception NoSuchElementException |
|
65 * if none of the given {@code Properties} objects contains a |
|
66 * property of the given key |
|
67 */ |
|
68 public boolean getBoolean(String key) { |
|
69 return getValue(key, BooleanStringConverter.INSTANCE); |
|
70 } |
|
71 |
|
72 /** |
|
73 * Retrieves the first property of the given key from the given list and |
|
74 * converts it to a {@code byte}. |
|
75 * |
|
76 * @param key |
|
77 * the property key |
|
78 * |
|
79 * @return a {@code byte} |
|
80 * |
|
81 * @exception NoSuchElementException |
|
82 * if none of the given {@code Properties} objects contains a |
|
83 * property of the given key |
|
84 * |
|
85 * @exception NumberFormatException |
|
86 * thrown by {@link ByteStringConverter#convert}. |
|
87 */ |
|
88 public byte getByte(String key) { |
|
89 return getValue(key, ByteStringConverter.INSTANCE); |
|
90 } |
|
91 |
|
92 /** |
|
93 * Retrieves the first property of the given key from the given list and |
|
94 * converts it to a {@code char}. |
|
95 * |
|
96 * @param key |
|
97 * the property key |
|
98 * |
|
99 * @return a {@code char} |
|
100 * |
|
101 * @exception NoSuchElementException |
|
102 * if none of the given {@code Properties} objects contains a |
|
103 * property of the given key |
|
104 * |
|
105 * @exception IndexOutOfBoundsException |
|
106 * thrown by {@link CharacterStringConverter#convert}. |
|
107 * |
|
108 * @exception NumberFormatException |
|
109 * thrown by {@link CharacterStringConverter#convert}. |
|
110 */ |
|
111 public char getChar(String key) { |
|
112 return getValue(key, CharacterStringConverter.INSTANCE); |
|
113 } |
|
114 |
|
115 /** |
|
116 * Retrieves the first property of the given key from the given list and |
|
117 * converts it to a {@code Color}. |
|
118 * |
|
119 * @param key |
|
120 * the property key |
|
121 * |
|
122 * @return a {@code Color} |
|
123 * |
|
124 * @exception NoSuchElementException |
|
125 * if none of the given {@code Properties} objects contains a |
|
126 * property of the given key |
|
127 * |
|
128 * @exception IllegalArgumentException |
|
129 * thrown by {@link ColorStringConverter#convert}. |
|
130 */ |
|
131 public Color getColor(String key) { |
|
132 return getValue(key, ColorStringConverter.INSTANCE); |
|
133 } |
|
134 |
|
135 /** |
|
136 * Retrieves the first property of the given key from the given list and |
|
137 * converts it to a {@code Dimension}. |
|
138 * |
|
139 * @param key |
|
140 * the property key |
|
141 * |
|
142 * @return a {@code Dimension} |
|
143 * |
|
144 * @exception NoSuchElementException |
|
145 * if none of the given {@code Properties} objects contains a |
|
146 * property of the given key |
|
147 * |
|
148 * @exception IllegalArgumentException |
|
149 * thrown by {@link DimensionStringConverter#convert}. |
|
150 */ |
|
151 public Dimension getDimension(String key) { |
|
152 return getValue(key, DimensionStringConverter.INSTANCE); |
|
153 } |
|
154 |
|
155 /** |
|
156 * Retrieves the first property of the given key from the given list and |
|
157 * converts it to a {@code double}. |
|
158 * |
|
159 * @param key |
|
160 * the property key |
|
161 * |
|
162 * @return a {@code double} |
|
163 * |
|
164 * @exception NoSuchElementException |
|
165 * if none of the given {@code Properties} objects contains a |
|
166 * property of the given key |
|
167 * |
|
168 * @exception NumberFormatException |
|
169 * thrown by {@link DoubleStringConverter#convert}. |
|
170 */ |
|
171 public double getDouble(String key) { |
|
172 return getValue(key, DoubleStringConverter.INSTANCE); |
|
173 } |
|
174 |
|
175 /** |
|
176 * Retrieves the first property of the given key from the given list and |
|
177 * converts it to a {@code float}. |
|
178 * |
|
179 * @param key |
|
180 * the property key |
|
181 * |
|
182 * @return a {@code float} |
|
183 * |
|
184 * @exception NoSuchElementException |
|
185 * if none of the given {@code Properties} objects contains a |
|
186 * property of the given key |
|
187 * |
|
188 * @exception NumberFormatException |
|
189 * thrown by {@link FloatStringConverter#convert}. |
|
190 */ |
|
191 public float getFloat(String key) { |
|
192 return getValue(key, FloatStringConverter.INSTANCE); |
|
193 } |
|
194 |
|
195 /** |
|
196 * Retrieves the first property of the given key from the given list and |
|
197 * converts it to a {@code int}. |
|
198 * |
|
199 * @param key |
|
200 * the property key |
|
201 * |
|
202 * @return a {@code int} |
|
203 * |
|
204 * @exception NoSuchElementException |
|
205 * if none of the given {@code Properties} objects contains a |
|
206 * property of the given key |
|
207 * |
|
208 * @exception NumberFormatException |
|
209 * thrown by {@link IntegerStringConverter#convert}. |
|
210 */ |
|
211 public int getInt(String key) { |
|
212 return getValue(key, IntegerStringConverter.INSTANCE); |
|
213 } |
|
214 |
|
215 /** |
|
216 * Retrieves the first property of the given key from the given list and |
|
217 * converts it to a {@code long}. |
|
218 * |
|
219 * @param key |
|
220 * the property key |
|
221 * |
|
222 * @return a {@code long} |
|
223 * |
|
224 * @exception NoSuchElementException |
|
225 * if none of the given {@code Properties} objects contains a |
|
226 * property of the given key |
|
227 * |
|
228 * @exception NumberFormatException |
|
229 * thrown by {@link LongStringConverter#convert}. |
|
230 */ |
|
231 public long getLong(String key) { |
|
232 return getValue(key, LongStringConverter.INSTANCE); |
|
233 } |
|
234 |
|
235 /** |
|
236 * Gets the top-most {@code Properties} in the list. |
|
237 */ |
|
238 public Properties getProperties() { |
|
239 return pList.get(pList.size() - 1); |
|
240 } |
|
241 |
|
242 /** |
|
243 * Gets the list of {@code Properties}, which are searched in <b>reverse</b> |
|
244 * order by the various <i>getXXX</i> methods. Access to this list is not |
|
245 * thread safe; users are responsible for synchronization if it is required. |
|
246 */ |
|
247 public List<Properties> getPropertiesList() { |
|
248 return pList; |
|
249 } |
|
250 |
|
251 /** |
|
252 * Retrieves the first property of the given key from the given list and |
|
253 * converts it to a {@code short}. |
|
254 * |
|
255 * @param key |
|
256 * the property key |
|
257 * |
|
258 * @return a {@code short} |
|
259 * |
|
260 * @exception NoSuchElementException |
|
261 * if none of the given {@code Properties} objects contains a |
|
262 * property of the given key |
|
263 * |
|
264 * @exception NumberFormatException |
|
265 * thrown by {@link ShortStringConverter#convert}. |
|
266 */ |
|
267 public short getShort(String key) { |
|
268 return getValue(key, ShortStringConverter.INSTANCE); |
|
269 } |
|
270 |
|
271 /** |
|
272 * Sets the given property in the top-most {@code Properties} in the list. |
|
273 * |
|
274 * @param key |
|
275 * the property key |
|
276 * |
|
277 * @param value |
|
278 * the property value |
|
279 * |
|
280 * @exception NullPointerException |
|
281 * if {@code key} is {@code null} |
|
282 * |
|
283 * @exception IndexOutOfBoundsException |
|
284 * if the {@code Properties} list is empty |
|
285 */ |
|
286 public void setBoolean(String key, boolean value) { |
|
287 setValue(key, value, BooleanStringConverter.INSTANCE); |
|
288 } |
|
289 |
|
290 /** |
|
291 * Sets the given property in the top-most {@code Properties} in the list. |
|
292 * |
|
293 * @param key |
|
294 * the property key |
|
295 * |
|
296 * @param value |
|
297 * the property value |
|
298 * |
|
299 * @exception NullPointerException |
|
300 * if {@code key} is {@code null} |
|
301 * |
|
302 * @exception IndexOutOfBoundsException |
|
303 * if the {@code Properties} list is empty |
|
304 */ |
|
305 public void setByte(String key, byte value) { |
|
306 setValue(key, value, ByteStringConverter.INSTANCE); |
|
307 } |
|
308 |
|
309 /** |
|
310 * Sets the given property in the top-most {@code Properties} in the list. |
|
311 * |
|
312 * @param key |
|
313 * the property key |
|
314 * |
|
315 * @param value |
|
316 * the property value |
|
317 * |
|
318 * @exception NullPointerException |
|
319 * if {@code key} is {@code null} |
|
320 * |
|
321 * @exception IndexOutOfBoundsException |
|
322 * if the {@code Properties} list is empty |
|
323 */ |
|
324 public void setChar(String key, char value) { |
|
325 setValue(key, value, CharacterStringConverter.INSTANCE); |
|
326 } |
|
327 |
|
328 /** |
|
329 * Sets the given property in the top-most {@code Properties} in the list. |
|
330 * |
|
331 * @param key |
|
332 * the property key |
|
333 * |
|
334 * @param value |
|
335 * the property value |
|
336 * |
|
337 * @exception NullPointerException |
|
338 * if {@code key} or {@code value} is {@code null} |
|
339 * |
|
340 * @exception IndexOutOfBoundsException |
|
341 * if the {@code Properties} list is empty |
|
342 */ |
|
343 public void setColor(String key, Color value) { |
|
344 setValue(key, value, ColorStringConverter.INSTANCE); |
|
345 } |
|
346 |
|
347 /** |
|
348 * Sets the given property in the top-most {@code Properties} in the list. |
|
349 * |
|
350 * @param key |
|
351 * the property key |
|
352 * |
|
353 * @param value |
|
354 * the property value |
|
355 * |
|
356 * @exception NullPointerException |
|
357 * if {@code key} or {@code value} is {@code null} |
|
358 * |
|
359 * @exception IndexOutOfBoundsException |
|
360 * if the {@code Properties} list is empty |
|
361 */ |
|
362 public void setDimension(String key, Dimension value) { |
|
363 setValue(key, value, DimensionStringConverter.INSTANCE); |
|
364 } |
|
365 |
|
366 /** |
|
367 * Sets the given property in the top-most {@code Properties} in the list. |
|
368 * |
|
369 * @param key |
|
370 * the property key |
|
371 * |
|
372 * @param value |
|
373 * the property value |
|
374 * |
|
375 * @exception NullPointerException |
|
376 * if {@code key} is {@code null} |
|
377 * |
|
378 * @exception IndexOutOfBoundsException |
|
379 * if the {@code Properties} list is empty |
|
380 */ |
|
381 public void setDouble(String key, double value) { |
|
382 setValue(key, value, DoubleStringConverter.INSTANCE); |
|
383 } |
|
384 |
|
385 /** |
|
386 * Sets the given property in the top-most {@code Properties} in the list. |
|
387 * |
|
388 * @param key |
|
389 * the property key |
|
390 * |
|
391 * @param value |
|
392 * the property value |
|
393 * |
|
394 * @exception NullPointerException |
|
395 * if {@code key} is {@code null} |
|
396 * |
|
397 * @exception IndexOutOfBoundsException |
|
398 * if the {@code Properties} list is empty |
|
399 */ |
|
400 public void setFloat(String key, float value) { |
|
401 setValue(key, value, FloatStringConverter.INSTANCE); |
|
402 } |
|
403 |
|
404 /** |
|
405 * Sets the given property in the top-most {@code Properties} in the list. |
|
406 * |
|
407 * @param key |
|
408 * the property key |
|
409 * |
|
410 * @param value |
|
411 * the property value |
|
412 * |
|
413 * @exception NullPointerException |
|
414 * if {@code key} is {@code null} |
|
415 * |
|
416 * @exception IndexOutOfBoundsException |
|
417 * if the {@code Properties} list is empty |
|
418 */ |
|
419 public void setInt(String key, int value) { |
|
420 setValue(key, value, IntegerStringConverter.INSTANCE); |
|
421 } |
|
422 |
|
423 /** |
|
424 * Sets the given property in the top-most {@code Properties} in the list. |
|
425 * |
|
426 * @param key |
|
427 * the property key |
|
428 * |
|
429 * @param value |
|
430 * the property value |
|
431 * |
|
432 * @exception NullPointerException |
|
433 * if {@code key} is {@code null} |
|
434 * |
|
435 * @exception IndexOutOfBoundsException |
|
436 * if the {@code Properties} list is empty |
|
437 */ |
|
438 public void setLong(String key, long value) { |
|
439 setValue(key, value, LongStringConverter.INSTANCE); |
|
440 } |
|
441 |
|
442 /** |
|
443 * Sets the given property in the top-most {@code Properties} in the list. |
|
444 * |
|
445 * @param key |
|
446 * the property key |
|
447 * |
|
448 * @param value |
|
449 * the property value |
|
450 * |
|
451 * @exception NullPointerException |
|
452 * if {@code key} is {@code null} |
|
453 * |
|
454 * @exception IndexOutOfBoundsException |
|
455 * if the {@code Properties} list is empty |
|
456 */ |
|
457 public void setShort(String key, short value) { |
|
458 setValue(key, value, ShortStringConverter.INSTANCE); |
|
459 } |
|
460 |
|
461 // |
|
462 // Private methods |
|
463 // |
|
464 |
|
465 private <T> T getValue(String key, StringConverter<T> converter) { |
|
466 for (int i = pList.size() - 1; i >= 0; i--) { |
|
467 Properties p = pList.get(i); |
|
468 if (p != null) { |
|
469 String sValue = p.getProperty(key); |
|
470 if (sValue != null) { |
|
471 return converter.convert(sValue); |
|
472 } |
|
473 } |
|
474 } |
|
475 throw new NoSuchElementException(key); |
|
476 } |
|
477 |
|
478 private <T> void setValue(String key, T value, |
|
479 StringConverter<T> converter) { |
|
480 |
|
481 String sValue = converter.revert(value); |
|
482 getProperties().put(key, sValue); |
|
483 } |
|
484 } |