View Javadoc
1   package org.djunits.unit.si;
2   
3   import java.util.Collections;
4   import java.util.LinkedHashMap;
5   import java.util.Map;
6   
7   /**
8    * Useful sets of SI prefixes.
9    * <p>
10   * Copyright (c) 2019-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>
12   * </p>
13   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
14   */
15  public enum SIPrefixes
16  {
17      /** No SI prefixes allowed. E.g., for the "inch". */
18      NONE,
19  
20      /** All standard SI prefixes allowed. E.g., for the "meter". */
21      UNIT,
22  
23      /** All SI prefixes indicating larger than 1. E.g., for the electronVolt to avoid underflow with float values. */
24      UNIT_POS,
25  
26      /** All standard SI prefixes allowed for "per unit". E.g., for the "per second". */
27      PER_UNIT,
28  
29      /** SI prefixes allowed, but default starts with "kilo" / "k", e.g., for the "kilogram". */
30      KILO;
31  
32      /** The SI prefixes and their values for the "UNIT" settings. */
33      public static final Map<String, SIPrefix> UNIT_PREFIXES;
34  
35      /** The SI prefixes and their values for the "PER_UNIT" settings. */
36      public static final Map<String, SIPrefix> PER_UNIT_PREFIXES;
37  
38      /** The larger than 1 SI prefixes and their values for the "UNIT_POS" settings. */
39      public static final Map<String, SIPrefix> UNIT_POS_PREFIXES;
40  
41      /** The SI prefixes and their values for the "KILO" settings. */
42      public static final Map<String, SIPrefix> KILO_PREFIXES;
43  
44      static
45      {
46          Map<String, SIPrefix> unitPrefixes = new LinkedHashMap<>();
47          unitPrefixes.put("y", new SIPrefix("y", "yocto", 1.0E-24));
48          unitPrefixes.put("z", new SIPrefix("z", "zepto", 1.0E-21));
49          unitPrefixes.put("a", new SIPrefix("a", "atto", 1.0E-18));
50          unitPrefixes.put("f", new SIPrefix("f", "femto", 1.0E-15));
51          unitPrefixes.put("p", new SIPrefix("p", "pico", 1.0E-12));
52          unitPrefixes.put("n", new SIPrefix("n", "nano", 1.0E-9));
53          unitPrefixes.put("mu", new SIPrefix("mu", "micro", 1.0E-6, "\u03BC"));
54          unitPrefixes.put("m", new SIPrefix("m", "milli", 1.0E-3));
55          unitPrefixes.put("c", new SIPrefix("c", "centi", 1.0E-2));
56          unitPrefixes.put("d", new SIPrefix("d", "deci", 1.0E-1));
57  
58          unitPrefixes.put("da", new SIPrefix("da", "deca", 1.0E1));
59          unitPrefixes.put("h", new SIPrefix("h", "hecto", 1.0E2));
60          unitPrefixes.put("k", new SIPrefix("k", "kilo", 1.0E3));
61          unitPrefixes.put("M", new SIPrefix("M", "mega", 1.0E6));
62          unitPrefixes.put("G", new SIPrefix("G", "giga", 1.0E9));
63          unitPrefixes.put("T", new SIPrefix("T", "tera", 1.0E12));
64          unitPrefixes.put("P", new SIPrefix("P", "peta", 1.0E15));
65          unitPrefixes.put("E", new SIPrefix("E", "exa", 1.0E18));
66          unitPrefixes.put("Z", new SIPrefix("Z", "zetta", 1.0E21));
67          unitPrefixes.put("Y", new SIPrefix("Y", "yotta", 1.0E24));
68          UNIT_PREFIXES = Collections.unmodifiableMap(unitPrefixes);
69  
70          Map<String, SIPrefix> perUnitPrefixes = new LinkedHashMap<>();
71          perUnitPrefixes.put("/y", new SIPrefix("/y", "per yocto", 1.0E24));
72          perUnitPrefixes.put("/z", new SIPrefix("/z", "per zepto", 1.0E21));
73          perUnitPrefixes.put("/a", new SIPrefix("/a", "per atto", 1.0E18));
74          perUnitPrefixes.put("/f", new SIPrefix("/f", "per femto", 1.0E15));
75          perUnitPrefixes.put("/p", new SIPrefix("/p", "per pico", 1.0E12));
76          perUnitPrefixes.put("/n", new SIPrefix("/n", "per nano", 1.0E9));
77          perUnitPrefixes.put("/mu", new SIPrefix("/mu", "per micro", 1.0E6, "/\u03BC"));
78          perUnitPrefixes.put("/m", new SIPrefix("/m", "per milli", 1.0E3));
79          perUnitPrefixes.put("/c", new SIPrefix("/c", "per centi", 1.0E2));
80          perUnitPrefixes.put("/d", new SIPrefix("/d", "per deci", 1.0E1));
81  
82          perUnitPrefixes.put("/da", new SIPrefix("/da", "per deca", 1.0E-1));
83          perUnitPrefixes.put("/h", new SIPrefix("/h", "per hecto", 1.0E-2));
84          perUnitPrefixes.put("/k", new SIPrefix("/k", "per kilo", 1.0E-3));
85          perUnitPrefixes.put("/M", new SIPrefix("/M", "per mega", 1.0E-6));
86          perUnitPrefixes.put("/G", new SIPrefix("/G", "per giga", 1.0E-9));
87          perUnitPrefixes.put("/T", new SIPrefix("/T", "per tera", 1.0E-12));
88          perUnitPrefixes.put("/P", new SIPrefix("/P", "per peta", 1.0E-15));
89          perUnitPrefixes.put("/E", new SIPrefix("/E", "per exa", 1.0E-18));
90          perUnitPrefixes.put("/Z", new SIPrefix("/Z", "per zetta", 1.0E-21));
91          perUnitPrefixes.put("/Y", new SIPrefix("/Y", "per yotta", 1.0E-24));
92          PER_UNIT_PREFIXES = Collections.unmodifiableMap(perUnitPrefixes);
93  
94          Map<String, SIPrefix> unitPosPrefixes = new LinkedHashMap<>();
95          unitPosPrefixes.put("da", new SIPrefix("da", "deca", 1.0E1));
96          unitPosPrefixes.put("h", new SIPrefix("h", "hecto", 1.0E2));
97          unitPosPrefixes.put("k", new SIPrefix("k", "kilo", 1.0E3));
98          unitPosPrefixes.put("M", new SIPrefix("M", "mega", 1.0E6));
99          unitPosPrefixes.put("G", new SIPrefix("G", "giga", 1.0E9));
100         unitPosPrefixes.put("T", new SIPrefix("T", "tera", 1.0E12));
101         unitPosPrefixes.put("P", new SIPrefix("P", "peta", 1.0E15));
102         unitPosPrefixes.put("E", new SIPrefix("E", "exa", 1.0E18));
103         unitPosPrefixes.put("Z", new SIPrefix("Z", "zetta", 1.0E21));
104         unitPosPrefixes.put("Y", new SIPrefix("Y", "yotta", 1.0E24));
105         UNIT_POS_PREFIXES = Collections.unmodifiableMap(unitPosPrefixes);
106 
107         Map<String, SIPrefix> kiloPrefixes = new LinkedHashMap<>();
108         kiloPrefixes.put("y", new SIPrefix("y", "yocto", 1.0E-27));
109         kiloPrefixes.put("z", new SIPrefix("z", "zepto", 1.0E-24));
110         kiloPrefixes.put("a", new SIPrefix("a", "atto", 1.0E-21));
111         kiloPrefixes.put("f", new SIPrefix("f", "femto", 1.0E-18));
112         kiloPrefixes.put("p", new SIPrefix("p", "pico", 1.0E-15));
113         kiloPrefixes.put("n", new SIPrefix("n", "nano", 1.0E-12));
114         kiloPrefixes.put("mu", new SIPrefix("mu", "micro", 1.0E-9, "\u03BC"));
115         kiloPrefixes.put("m", new SIPrefix("m", "milli", 1.0E-6));
116         kiloPrefixes.put("c", new SIPrefix("c", "centi", 1.0E-5));
117         kiloPrefixes.put("d", new SIPrefix("d", "deci", 1.0E-4));
118         kiloPrefixes.put("", new SIPrefix("", "", 1.0E-3));
119         kiloPrefixes.put("da", new SIPrefix("da", "deca", 1.0E-2));
120         kiloPrefixes.put("h", new SIPrefix("h", "hecto", 1.0E-1));
121 
122         kiloPrefixes.put("M", new SIPrefix("M", "mega", 1.0E3));
123         kiloPrefixes.put("G", new SIPrefix("G", "giga", 1.0E6));
124         kiloPrefixes.put("T", new SIPrefix("T", "tera", 1.0E9));
125         kiloPrefixes.put("P", new SIPrefix("P", "peta", 1.0E12));
126         kiloPrefixes.put("E", new SIPrefix("E", "exa", 1.0E15));
127         kiloPrefixes.put("Z", new SIPrefix("Z", "zetta", 1.0E18));
128         kiloPrefixes.put("Y", new SIPrefix("Y", "yotta", 1.0E21));
129         KILO_PREFIXES = Collections.unmodifiableMap(kiloPrefixes);
130     }
131 
132     /**
133      * Look up and return the prefix information for the given prefix key (e.g., "G" for "giga").
134      * @param prefixKey String; the prefix key, e.g., "G" for "giga"
135      * @return SIPrefix; the SIPrefix information, or null if the <code>prefixKey</code> does not exist
136      */
137     public static SIPrefix getUnit(final String prefixKey)
138     {
139         return UNIT_PREFIXES.get(prefixKey);
140     }
141 
142     /**
143      * Look up and return the prefix information for the given prefix key (e.g., "/n" for "per nano").
144      * @param prefixKey String; the prefix key, e.g., "/n" for "per nano"
145      * @return SIPrefix; the SIPrefix information, or null if the <code>prefixKey</code> does not exist
146      */
147     public static SIPrefix getPerUnit(final String prefixKey)
148     {
149         return PER_UNIT_PREFIXES.get(prefixKey);
150     }
151 
152     /**
153      * Return the prefix information for the given prefix key (e.g., "G" for "giga"), with an offset of a factor 1000 for units
154      * that have "kilo" as the default.
155      * @param prefixKey String; the prefix key, e.g., "G" for "giga"
156      * @return SIPrefix; the SIPrefix information, with an offset of 1000. So "k" will return 1, and "" will return 1.0E-3.
157      */
158     public static SIPrefix getKiloUnit(final String prefixKey)
159     {
160         return KILO_PREFIXES.get(prefixKey);
161     }
162 
163 }