View Javadoc

1   package org.lcsim.conditions;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.util.ArrayList;
6   import java.util.List;
7   import java.util.Properties;
8   import java.util.Set;
9   import java.util.StringTokenizer;
10  
11  /**
12   * 
13   * @author Tony Johnson $Id: ConditionsSetImplementation.java,v 1.1.1.1
14   *         2010/01/25 22:23:07 jeremy Exp $
15   */
16  class ConditionsSetImplementation extends ConditionsImplementation implements ConditionsSet {
17      ConditionsSetImplementation(ConditionsManagerImplementation manager, String name) throws IOException {
18          super(manager, name);
19          // We assume this points to a properties file
20          InputStream in = manager.open(name, "properties");
21          try {
22              props = new Properties();
23              props.load(in);
24          } finally {
25              in.close();
26          }
27      }
28  
29      public int getInt(String name) {
30          String value = props.getProperty(name);
31          if (value == null)
32              throw new IllegalArgumentException("Missing value for " + name);
33          return parseInt(value, name);
34      }
35  
36      public int getInt(String name, int defaultValue) {
37          String value = props.getProperty(name);
38          if (value == null)
39              return defaultValue;
40          return parseInt(value, name);
41      }
42  
43      public double getDouble(String name) {
44          String value = props.getProperty(name);
45          if (value == null)
46              throw new IllegalArgumentException("Missing value for " + name);
47          return parseDouble(value, name);
48      }
49  
50      public double getDouble(String name, double defaultValue) {
51          String value = props.getProperty(name);
52          if (value == null)
53              return defaultValue;
54          return parseDouble(value, name);
55      }
56  
57      public double[] getDoubleArray(String name) {
58          String value = props.getProperty(name);
59          if (value == null)
60              throw new IllegalArgumentException("Missing value for " + name);
61          return parseDoubleArray(value, name);
62      }
63  
64      private int parseInt(String value, String name) {
65          try {
66              return Integer.parseInt(value);
67          } catch (NumberFormatException e) {
68              throw new RuntimeException("Error reading conditions: Illegal value " + value + " for " + name);
69          }
70      }
71  
72      private double parseDouble(String value, String name) {
73          try {
74              return Double.parseDouble(value);
75          } catch (NumberFormatException e) {
76              throw new RuntimeException("Error reading conditions: Illegal value " + value + " for " + name);
77          }
78      }
79  
80      private double[] parseDoubleArray(String value, String name) {
81          List<Double> doubles = new ArrayList<Double>();
82          StringTokenizer st = new StringTokenizer(value, ",");
83          while (st.hasMoreTokens()) {
84              String doubleValue = st.nextToken();
85              try {
86                  doubles.add(Double.parseDouble(doubleValue));
87              } catch (NumberFormatException e) {
88                  throw new RuntimeException("Error reading conditions: Illegal value " + value + " for " + name);
89              }
90          }
91          // should be an easier way to do this...
92          double[] returnDoubles = new double[doubles.size()];
93          int i = 0;
94          for (Double d : doubles) {
95              returnDoubles[i++] = d;
96          }
97          return returnDoubles;
98      }
99  
100     public String getString(String name) {
101         String value = props.getProperty(name);
102         if (value == null)
103             throw new IllegalArgumentException("Missing value for " + name);
104         return value;
105     }
106 
107     public String getString(String name, String defValue) {
108         return props.getProperty(name, defValue);
109     }
110 
111     public boolean getBoolean(String name) throws IllegalArgumentException {
112         String value = props.getProperty(name);
113         if (value == null)
114             throw new IllegalArgumentException("Missing value for " + name);
115         return parseBoolean(value, name);
116     }
117 
118     public boolean getBoolean(String name, boolean defaultValue) {
119         String value = props.getProperty(name);
120         if (value == null)
121             return defaultValue;
122         return parseBoolean(value, name);
123     }
124 
125     private boolean parseBoolean(String value, String name) {
126         if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value))
127             return true;
128         if ("false".equalsIgnoreCase(value) || "no".equalsIgnoreCase(value))
129             return false;
130         throw new RuntimeException("Error reading conditions: Illegal value " + value + " for " + name);
131     }
132 
133     public Set keySet() {
134         return props.keySet();
135     }
136 
137     public Class getType(String key) {
138         String value = props.getProperty(key);
139         if (value == null)
140             throw new IllegalArgumentException("Missing value for " + key);
141         try {
142             Integer.parseInt(value);
143             return Integer.TYPE;
144         } catch (NumberFormatException x) {
145             try {
146                 Double.parseDouble(value);
147                 return Double.TYPE;
148             } catch (NumberFormatException xx) {
149                 return String.class;
150             }
151         }
152     }
153 
154     public int size() {
155         return props.size();
156     }
157 
158     public boolean containsKey(String key) {
159         return props.containsKey(key);
160     }
161 
162     private Properties props;
163 }