Debian Codegraph @

src/uk/me/parabola/util/EnhancedProperties.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: May 17, 2008
16 */
17 package uk.me.parabola.util;
18
19 import java.util.Enumeration;
20 import java.util.Properties;
21
22 /**
23 * Wrapper that behaves as an enhanced properties class that has getProperty
24 * calls for different data types.
25 *
26 * @author Steve Ratcliffe
27 */
28 public class EnhancedProperties extends Properties {
29
30 public EnhancedProperties() {
31 }
32
33 public EnhancedProperties(Properties defaults) {
34 // We copy values, rather than making them default values so that
35 // we can enumerate all the options.
36 Enumeration<?> en = defaults.propertyNames();
37 while (en.hasMoreElements()) {
38 String key = (String) en.nextElement();
39 setProperty(key, defaults.getProperty(key));
40 }
41 }
42
43 /**
44 * Get a property as an integer value. If the property does not exist
45 * or the value is not a valid integer, then the default value is returned
46 * instead.
47 * @param key The property name to retrieve.
48 * @param def The Default value to use if the property does not exist or
49 * if the value is not a valid integer.
50 * @return The value of the property as an integer, or the default value.
51 */
52 public int getProperty(String key, int def) {
53 try {
54 String s = getProperty(key);
55 return s == null ? def : Integer.parseInt(s);
56 } catch (NumberFormatException e) {
57 return def;
58 }
59 }
60
61 /**
62 * Get a property as a boolean value. If the value of the property
63 * begins with a '1', a 'y' or a 't' (for 1, yes, true) then true is
64 * returned. If the property does not exist then the given default
65 * value is returned.
66 *
67 * @param key The property name to get.
68 * @param def The default value that is returned if property does not
69 * exist.
70 * @return The value of the property as a boolean. If the property does
71 * not exist then the value of 'def'.
72 */
73 public boolean getProperty(String key, boolean def) {
74 String s = getProperty(key);
75 if (s != null) {
76 if (s.isEmpty())
77 return true;
78 char c = s.toLowerCase().charAt(0);
79 return (c == '1' || c == 'y' || c == 't');
80 }
81 return def;
82 }
83
84 /**
85 * Return a property as a double value. If the property does not
86 * exist or is not a valid double, then the given default value
87 * is returned instead.
88 * @param key The property name.
89 * @param def The default value to return if no valid value.
90 * @return The value of the property as a double.
91 */
92 public double getProperty(String key, double def) {
93 try {
94 String s = getProperty(key);
95 return s == null ? def : Double.parseDouble(s);
96 } catch (NumberFormatException e) {
97 return def;
98 }
99 }
100 }