View Javadoc

1   package org.lcsim.units.clhep;
2   
3   import java.lang.reflect.Field;
4   import java.util.HashMap;
5   import java.util.Map;
6   import java.util.Set;
7   import java.util.Map.Entry;
8   
9   /**
10   * Statically accessible version of CLHEP units and 
11   * physical constants.  Two methods are forwarded
12   * from Map.
13   * 
14   * @see #get(String)
15   * @see #entrySet()
16   * 
17   * The method 
18   * 
19   * @see #getInstance()
20   * 
21   * should be used to retrieve a static reference
22   * to this class.
23   *  
24   * @author Jeremy McCormick <jeremym@slac.stanford.edu>
25   *
26   */
27  public class Constants 
28  {
29      Map<String, Double> constantsMap = new HashMap<String, Double>();
30      
31      private static Constants instance = new Constants();
32      
33      public double get(String key)
34      {
35          return constantsMap.get(key);
36      }
37      
38      public Set<Entry<String,Double>> entrySet()
39      {
40          return constantsMap.entrySet();
41      }
42      
43      public static Constants getInstance()
44      {
45          return instance;
46      }
47      
48      private Constants()
49      {
50          setupSystemOfUnits();
51          setupPhysicalConstants();
52      }
53      
54      private void setupSystemOfUnits()
55      {
56          SystemOfUnits units = new SystemOfUnits();      
57          Class<SystemOfUnits> klass = SystemOfUnits.class;
58          Field[] fields = klass.getFields();
59          for (Field f : fields)
60          {
61              try {
62                  constantsMap.put(f.getName(), f.getDouble(units));
63              }
64              catch ( IllegalAccessException x )
65              {
66                  throw new RuntimeException(x);
67              }
68          }
69      }
70      
71      private void setupPhysicalConstants()
72      {
73          PhysicalConstants physc = new PhysicalConstants();
74          Class<PhysicalConstants> klass = PhysicalConstants.class;
75          Field[] fields = klass.getFields();
76          for (Field f : fields)
77          {
78              try {
79                  constantsMap.put(f.getName(), f.getDouble(physc));
80              }
81              catch ( IllegalAccessException x )
82              {
83                  throw new RuntimeException(x);
84              }
85          }
86      }    
87  }