1 19 package jcckit.util; 20 21 import java.awt.Color ; 22 import java.util.StringTokenizer ; 23 24 43 public class ConfigParameters { 44 private final ConfigData _configData; 45 46 47 public ConfigParameters(ConfigData configData) { 48 _configData = configData; 49 } 50 51 56 public String getFullKey(String key) { 57 return _configData.getFullKey(key); 58 } 59 60 67 public String get(String key) { 68 String result = _configData.get(key); 69 if (result == null) { 70 throw new IllegalArgumentException (getFullKey(key)); 71 } 72 return result; 73 } 74 75 82 public String get(String key, String defaultValue) { 83 String result = _configData.get(key); 84 if (result == null) { 85 result = defaultValue; 86 } 87 return result; 88 } 89 90 98 public boolean getBoolean(String key) { 99 return parseBoolean(get(key), key); 100 } 101 102 109 public boolean getBoolean(String key, boolean defaultValue) { 110 String value = _configData.get(key); 111 return value == null ? defaultValue : parseBoolean(value, key); 112 } 113 114 private boolean parseBoolean(String value, String key) { 115 if (value.equals("true")) { 116 return true; 117 } else if (value.equals("false")) { 118 return false; 119 } else { 120 throw createNumberFormatException("boolean", value, key); 121 } 122 } 123 124 private NumberFormatException createNumberFormatException(String text, 125 String value, 126 String key) { 127 return new NumberFormatException ("Not a " + text + ": " + getFullKey(key) 128 + " = " + value); 129 } 130 131 145 public int getInt(String key) { 146 return parseInt(get(key), key); 147 } 148 149 163 public int getInt(String key, int defaultValue) { 164 String value = _configData.get(key); 165 return value == null ? defaultValue : parseInt(value, key); 166 } 167 168 private int parseInt(String value, String key) { 169 try { 170 return Integer.decode(value).intValue(); 171 } catch (NumberFormatException e) { 172 throw createNumberFormatException("number", value, key); 173 } 174 } 175 176 185 public double getDouble(String key) { 186 return parseDouble(get(key), key); 187 } 188 189 199 public double getDouble(String key, double defaultValue) { 200 String value = _configData.get(key); 201 return value == null ? defaultValue : parseDouble(value, key); 202 } 203 204 private double parseDouble(String value, String key) { 205 try { 206 return new Double (value).doubleValue(); 207 } catch (NumberFormatException e) { 208 throw createNumberFormatException("number", value, key); 209 } 210 } 211 212 223 public double[] getDoubleArray(String key) { 224 return parseDoubleArray(get(key), key); 225 } 226 227 238 public double[] getDoubleArray(String key, double[] defaultValue) { 239 String value = _configData.get(key); 240 return value == null ? defaultValue : parseDoubleArray(value, key); 241 } 242 243 private double[] parseDoubleArray(String value, String key) { 244 try { 245 StringTokenizer tokenizer = new StringTokenizer (value); 246 double[] result = new double[tokenizer.countTokens()]; 247 for (int i = 0; i < result.length; i++) { 248 result[i] = new Double (tokenizer.nextToken()).doubleValue(); 249 } 250 return result; 251 } catch (NumberFormatException e) { 252 throw createNumberFormatException("sequence of numbers", value, key); 253 } 254 } 255 256 268 public Color getColor(String key) { 269 return parseColor(get(key), key); 270 } 271 272 286 public Color getColor(String key, Color defaultValue) { 287 String value = _configData.get(key); 288 return value == null ? defaultValue : parseColor(value, key); 289 } 290 291 private Color parseColor(String value, String key) { 292 try { 293 return value.length() == 0 ? null : Color.decode(value); 294 } catch (NumberFormatException e) { 295 throw createNumberFormatException("number", value, key); 296 } 297 } 298 299 305 public ConfigParameters getNode(String key) { 306 return new ConfigParameters(_configData.getNode(key)); 307 } 308 } 309 | Popular Tags |