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