View Javadoc

1   package org.lcsim.units;
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   public class Constants 
10  {
11  	Map<String, Double> constantsMap = new HashMap<String, Double>();
12  
13  	private static Constants instance = new Constants();
14  
15  	public double get(String key)
16  	{
17  		return constantsMap.get(key);
18  	}
19  
20  	public Set<Entry<String,Double>> entrySet()
21  	{
22  		return constantsMap.entrySet();
23  	}
24  
25  	public static Constants getInstance()
26  	{
27  		return instance;
28  	}
29  
30  	private Constants()
31  	{
32  		setupSystemOfUnits();
33  	}
34  
35  	private void setupSystemOfUnits()
36  	{
37  		SystemOfUnits units = new SystemOfUnits();      
38  		Class<SystemOfUnits> klass = SystemOfUnits.class;
39  		Field[] fields = klass.getFields();
40  		for (Field f : fields)
41  		{
42  			try {
43  				constantsMap.put(f.getName(), f.getDouble(units));
44  			}
45  			catch ( IllegalAccessException x )
46  			{
47  				throw new RuntimeException(x);
48  			}
49  		}
50  	}
51  }