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-2024 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      /** The map that translates an SI prefix power to the SI Prefix. */
45      public static final Map<Integer, SIPrefix> FACTORS;
46  
47      static
48      {
49          Map<String, SIPrefix> unitPrefixes = new LinkedHashMap<>();
50          unitPrefixes.put("y", new SIPrefix("y", "yocto", 1.0E-24));
51          unitPrefixes.put("z", new SIPrefix("z", "zepto", 1.0E-21));
52          unitPrefixes.put("a", new SIPrefix("a", "atto", 1.0E-18));
53          unitPrefixes.put("f", new SIPrefix("f", "femto", 1.0E-15));
54          unitPrefixes.put("p", new SIPrefix("p", "pico", 1.0E-12));
55          unitPrefixes.put("n", new SIPrefix("n", "nano", 1.0E-9));
56          unitPrefixes.put("mu", new SIPrefix("mu", "micro", 1.0E-6, "\u03BC"));
57          unitPrefixes.put("m", new SIPrefix("m", "milli", 1.0E-3));
58          unitPrefixes.put("c", new SIPrefix("c", "centi", 1.0E-2));
59          unitPrefixes.put("d", new SIPrefix("d", "deci", 1.0E-1));
60  
61          unitPrefixes.put("da", new SIPrefix("da", "deca", 1.0E1));
62          unitPrefixes.put("h", new SIPrefix("h", "hecto", 1.0E2));
63          unitPrefixes.put("k", new SIPrefix("k", "kilo", 1.0E3));
64          unitPrefixes.put("M", new SIPrefix("M", "mega", 1.0E6));
65          unitPrefixes.put("G", new SIPrefix("G", "giga", 1.0E9));
66          unitPrefixes.put("T", new SIPrefix("T", "tera", 1.0E12));
67          unitPrefixes.put("P", new SIPrefix("P", "peta", 1.0E15));
68          unitPrefixes.put("E", new SIPrefix("E", "exa", 1.0E18));
69          unitPrefixes.put("Z", new SIPrefix("Z", "zetta", 1.0E21));
70          unitPrefixes.put("Y", new SIPrefix("Y", "yotta", 1.0E24));
71          UNIT_PREFIXES = Collections.unmodifiableMap(unitPrefixes);
72  
73          Map<Integer, SIPrefix> factorMap = new LinkedHashMap<>();
74          for (SIPrefix siPrefix : UNIT_PREFIXES.values())
75          {
76              String s = String.format("%e", siPrefix.getFactor());
77              // System.out.println(String.format("%s gets formatted as %s", siPrefix.getFactor(), s));
78              Integer key = Integer.parseInt(s.substring(s.indexOf("e") + 1));
79              factorMap.put(key, siPrefix);
80          }
81          factorMap.put(0, new SIPrefix("", "", 1.0));
82          FACTORS = factorMap;
83  
84          Map<String, SIPrefix> perUnitPrefixes = new LinkedHashMap<>();
85          perUnitPrefixes.put("/y", new SIPrefix("/y", "per yocto", 1.0E24));
86          perUnitPrefixes.put("/z", new SIPrefix("/z", "per zepto", 1.0E21));
87          perUnitPrefixes.put("/a", new SIPrefix("/a", "per atto", 1.0E18));
88          perUnitPrefixes.put("/f", new SIPrefix("/f", "per femto", 1.0E15));
89          perUnitPrefixes.put("/p", new SIPrefix("/p", "per pico", 1.0E12));
90          perUnitPrefixes.put("/n", new SIPrefix("/n", "per nano", 1.0E9));
91          perUnitPrefixes.put("/mu", new SIPrefix("/mu", "per micro", 1.0E6, "/\u03BC"));
92          perUnitPrefixes.put("/m", new SIPrefix("/m", "per milli", 1.0E3));
93          perUnitPrefixes.put("/c", new SIPrefix("/c", "per centi", 1.0E2));
94          perUnitPrefixes.put("/d", new SIPrefix("/d", "per deci", 1.0E1));
95  
96          perUnitPrefixes.put("/da", new SIPrefix("/da", "per deca", 1.0E-1));
97          perUnitPrefixes.put("/h", new SIPrefix("/h", "per hecto", 1.0E-2));
98          perUnitPrefixes.put("/k", new SIPrefix("/k", "per kilo", 1.0E-3));
99          perUnitPrefixes.put("/M", new SIPrefix("/M", "per mega", 1.0E-6));
100         perUnitPrefixes.put("/G", new SIPrefix("/G", "per giga", 1.0E-9));
101         perUnitPrefixes.put("/T", new SIPrefix("/T", "per tera", 1.0E-12));
102         perUnitPrefixes.put("/P", new SIPrefix("/P", "per peta", 1.0E-15));
103         perUnitPrefixes.put("/E", new SIPrefix("/E", "per exa", 1.0E-18));
104         perUnitPrefixes.put("/Z", new SIPrefix("/Z", "per zetta", 1.0E-21));
105         perUnitPrefixes.put("/Y", new SIPrefix("/Y", "per yotta", 1.0E-24));
106         PER_UNIT_PREFIXES = Collections.unmodifiableMap(perUnitPrefixes);
107 
108         Map<String, SIPrefix> unitPosPrefixes = new LinkedHashMap<>();
109         unitPosPrefixes.put("da", new SIPrefix("da", "deca", 1.0E1));
110         unitPosPrefixes.put("h", new SIPrefix("h", "hecto", 1.0E2));
111         unitPosPrefixes.put("k", new SIPrefix("k", "kilo", 1.0E3));
112         unitPosPrefixes.put("M", new SIPrefix("M", "mega", 1.0E6));
113         unitPosPrefixes.put("G", new SIPrefix("G", "giga", 1.0E9));
114         unitPosPrefixes.put("T", new SIPrefix("T", "tera", 1.0E12));
115         unitPosPrefixes.put("P", new SIPrefix("P", "peta", 1.0E15));
116         unitPosPrefixes.put("E", new SIPrefix("E", "exa", 1.0E18));
117         unitPosPrefixes.put("Z", new SIPrefix("Z", "zetta", 1.0E21));
118         unitPosPrefixes.put("Y", new SIPrefix("Y", "yotta", 1.0E24));
119         UNIT_POS_PREFIXES = Collections.unmodifiableMap(unitPosPrefixes);
120 
121         Map<String, SIPrefix> kiloPrefixes = new LinkedHashMap<>();
122         kiloPrefixes.put("y", new SIPrefix("y", "yocto", 1.0E-27));
123         kiloPrefixes.put("z", new SIPrefix("z", "zepto", 1.0E-24));
124         kiloPrefixes.put("a", new SIPrefix("a", "atto", 1.0E-21));
125         kiloPrefixes.put("f", new SIPrefix("f", "femto", 1.0E-18));
126         kiloPrefixes.put("p", new SIPrefix("p", "pico", 1.0E-15));
127         kiloPrefixes.put("n", new SIPrefix("n", "nano", 1.0E-12));
128         kiloPrefixes.put("mu", new SIPrefix("mu", "micro", 1.0E-9, "\u03BC"));
129         kiloPrefixes.put("m", new SIPrefix("m", "milli", 1.0E-6));
130         kiloPrefixes.put("c", new SIPrefix("c", "centi", 1.0E-5));
131         kiloPrefixes.put("d", new SIPrefix("d", "deci", 1.0E-4));
132         kiloPrefixes.put("", new SIPrefix("", "", 1.0E-3));
133         kiloPrefixes.put("da", new SIPrefix("da", "deca", 1.0E-2));
134         kiloPrefixes.put("h", new SIPrefix("h", "hecto", 1.0E-1));
135 
136         kiloPrefixes.put("M", new SIPrefix("M", "mega", 1.0E3));
137         kiloPrefixes.put("G", new SIPrefix("G", "giga", 1.0E6));
138         kiloPrefixes.put("T", new SIPrefix("T", "tera", 1.0E9));
139         kiloPrefixes.put("P", new SIPrefix("P", "peta", 1.0E12));
140         kiloPrefixes.put("E", new SIPrefix("E", "exa", 1.0E15));
141         kiloPrefixes.put("Z", new SIPrefix("Z", "zetta", 1.0E18));
142         kiloPrefixes.put("Y", new SIPrefix("Y", "yotta", 1.0E21));
143         KILO_PREFIXES = Collections.unmodifiableMap(kiloPrefixes);
144     }
145 
146     /**
147      * Look up and return the prefix information for the given prefix key (e.g., "G" for "giga").
148      * @param prefixKey String; the prefix key, e.g., "G" for "giga"
149      * @return SIPrefix; the SIPrefix information, or null if the <code>prefixKey</code> does not exist
150      */
151     public static SIPrefix getUnit(final String prefixKey)
152     {
153         return UNIT_PREFIXES.get(prefixKey);
154     }
155 
156     /**
157      * Look up and return the prefix information for the given prefix key (e.g., "/n" for "per nano").
158      * @param prefixKey String; the prefix key, e.g., "/n" for "per nano"
159      * @return SIPrefix; the SIPrefix information, or null if the <code>prefixKey</code> does not exist
160      */
161     public static SIPrefix getPerUnit(final String prefixKey)
162     {
163         return PER_UNIT_PREFIXES.get(prefixKey);
164     }
165 
166     /**
167      * Return the prefix information for the given prefix key (e.g., "G" for "giga"), with an offset of a factor 1000 for units
168      * that have "kilo" as the default.
169      * @param prefixKey String; the prefix key, e.g., "G" for "giga"
170      * @return SIPrefix; the SIPrefix information, with an offset of 1000. So "k" will return 1, and "" will return 1.0E-3.
171      */
172     public static SIPrefix getKiloUnit(final String prefixKey)
173     {
174         return KILO_PREFIXES.get(prefixKey);
175     }
176 
177 }