001/* 002 * Units of Measurement API 003 * Copyright (c) 2014-2018, Jean-Marie Dautelle, Werner Keil, Otavio Santana. 004 * 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * 1. Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 014 * and the following disclaimer in the documentation and/or other materials provided with the distribution. 015 * 016 * 3. Neither the name of JSR-385 nor the names of its contributors may be used to endorse or promote products 017 * derived from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package tech.units.indriya.unit; 031 032import javax.measure.Quantity; 033import javax.measure.Unit; 034 035import tech.units.indriya.AbstractUnit; 036 037/** 038 * <p> 039 * This class provides support for the 20 prefixes used in the metric system (decimal multiples and submultiples of units). For example: 040 * 041 * <pre> 042 * <code> 043 * import static tech.units.indriya.unit.Units.*; // Static import. 044 * import static tech.units.indriya.unit.MetricPrefix.*; // Static import. 045 * import javax.measure.*; 046 * import javax.measure.quantity.*; 047 * ... 048 * Unit<Pressure> HECTOPASCAL = HECTO(PASCAL); 049 * Unit<Length> KILOMETRE = KILO(METRE); 050 * </code> 051 * </pre> 052 * 053 * </p> 054 * 055 * @see <a href="http://en.wikipedia.org/wiki/Metric_prefix">Wikipedia: Metric Prefix</a> 056 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 057 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 058 * @version 1.8, 2018-04-20 059 * @since 2.0 060 */ 061public enum MetricPrefix implements Prefix { 062 YOTTA("Y", 10, 24), // 063 ZETTA("Z", 10, 21), // 064 EXA("E", 10, 18), // 065 PETA("P", 10, 15), // 066 TERA("T", 10, 12), // 067 GIGA("G", 10, 9), // 068 MEGA("M", 10, 6), // 069 KILO("k", 10, 3), // 070 HECTO("h", 10, 2), // 071 DEKA("da", 10, 1), // 072 DECI("d", 10, -1), // 073 CENTI("c", 10, -2), // 074 MILLI("m", 10, -3), // 075 MICRO("ยต", 10, -6), // 076 NANO("n", 10, -9), // 077 PICO("p", 10, -12), // 078 FEMTO("f", 10, -15), // 079 ATTO("a", 10, -18), // 080 ZEPTO("z", 10, -21), // 081 YOCTO("y", 10, -24); 082 083 /** 084 * The symbol of this prefix, as returned by {@link #getSymbol}. 085 * 086 * @serial 087 * @see #getSymbol() 088 */ 089 private final String symbol; 090 091 /** 092 * Base part of the associated factor in base^exponent representation. 093 */ 094 private int base; 095 096 /** 097 * Exponent part of the associated factor in base^exponent representation. 098 */ 099 private int exponent; 100 101 /** 102 * Creates a new prefix. 103 * 104 * @param symbol 105 * the symbol of this prefix. 106 * @param base 107 * part of the associated factor in base^exponent representation. 108 * @param exponent 109 * part of the associated factor in base^exponent representation. 110 */ 111 private MetricPrefix(String symbol, int base, int exponent) { 112 this.symbol = symbol; 113 this.base = base; 114 this.exponent = exponent; 115 } 116 117 /** 118 * Returns the specified unit multiplied by the factor <code>10<sup>24</sup></code> 119 * 120 * @param <Q> 121 * The type of the quantity measured by the unit. 122 * @param unit 123 * any unit. 124 * @return <code>unit.times(1e24)</code>. 125 */ 126 public static <Q extends Quantity<Q>> Unit<Q> YOTTA(Unit<Q> unit) { 127 return ((AbstractUnit)unit).prefix(YOTTA); 128 } 129 130 /** 131 * Returns the specified unit multiplied by the factor <code>10<sup>21</sup></code> 132 * 133 * @param <Q> 134 * The type of the quantity measured by the unit. 135 * @param unit 136 * any unit. 137 * @return <code>unit.times(1e21)</code>. 138 */ 139 public static <Q extends Quantity<Q>> Unit<Q> ZETTA(Unit<Q> unit) { 140 return ((AbstractUnit)unit).prefix(ZETTA); 141 } 142 143 /** 144 * Returns the specified unit multiplied by the factor <code>10<sup>18</sup></code> 145 * 146 * @param <Q> 147 * The type of the quantity measured by the unit. 148 * @param unit 149 * any unit. 150 * @return <code>unit.times(1e18)</code>. 151 */ 152 public static <Q extends Quantity<Q>> Unit<Q> EXA(Unit<Q> unit) { 153 return ((AbstractUnit)unit).prefix(EXA); 154 } 155 156 /** 157 * Returns the specified unit multiplied by the factor <code>10<sup>15</sup></code> 158 * 159 * @param <Q> 160 * The type of the quantity measured by the unit. 161 * @param unit 162 * any unit. 163 * @return <code>unit.times(1e15)</code>. 164 */ 165 public static <Q extends Quantity<Q>> Unit<Q> PETA(Unit<Q> unit) { 166 return ((AbstractUnit)unit).prefix(PETA); 167 } 168 169 /** 170 * Returns the specified unit multiplied by the factor <code>10<sup>12</sup></code> 171 * 172 * @param <Q> 173 * The type of the quantity measured by the unit. 174 * @param unit 175 * any unit. 176 * @return <code>unit.times(1e12)</code>. 177 */ 178 public static <Q extends Quantity<Q>> Unit<Q> TERA(Unit<Q> unit) { 179 return ((AbstractUnit)unit).prefix(TERA); 180 } 181 182 /** 183 * Returns the specified unit multiplied by the factor <code>10<sup>9</sup></code> 184 * 185 * @param <Q> 186 * The type of the quantity measured by the unit. 187 * @param unit 188 * any unit. 189 * @return <code>unit.times(1e9)</code>. 190 */ 191 public static <Q extends Quantity<Q>> Unit<Q> GIGA(Unit<Q> unit) { 192 return ((AbstractUnit)unit).prefix(GIGA); 193 } 194 195 /** 196 * Returns the specified unit multiplied by the factor <code>10<sup>6</sup></code> 197 * 198 * @param <Q> 199 * The type of the quantity measured by the unit. 200 * @param unit 201 * any unit. 202 * @return <code>unit.times(1e6)</code>. 203 */ 204 public static <Q extends Quantity<Q>> Unit<Q> MEGA(Unit<Q> unit) { 205 return ((AbstractUnit)unit).prefix(MEGA); 206 } 207 208 /** 209 * Returns the specified unit multiplied by the factor <code>10<sup>3</sup></code> 210 * 211 * @param <Q> 212 * The type of the quantity measured by the unit. 213 * @param unit 214 * any unit. 215 * @return <code>unit.times(1e3)</code>. 216 */ 217 public static <Q extends Quantity<Q>> Unit<Q> KILO(Unit<Q> unit) { 218 return ((AbstractUnit)unit).prefix(KILO); 219 } 220 221 /** 222 * Returns the specified unit multiplied by the factor <code>10<sup>2</sup></code> 223 * 224 * @param <Q> 225 * The type of the quantity measured by the unit. 226 * @param unit 227 * any unit. 228 * @return <code>unit.times(1e2)</code>. 229 */ 230 public static <Q extends Quantity<Q>> Unit<Q> HECTO(Unit<Q> unit) { 231 return ((AbstractUnit)unit).prefix(HECTO); 232 } 233 234 /** 235 * Returns the specified unit multiplied by the factor <code>10<sup>1</sup></code> 236 * 237 * @param <Q> 238 * The type of the quantity measured by the unit. 239 * @param unit 240 * any unit. 241 * @return <code>unit.times(1e1)</code>. 242 */ 243 public static <Q extends Quantity<Q>> Unit<Q> DEKA(Unit<Q> unit) { 244 return ((AbstractUnit)unit).prefix(DEKA); 245 } 246 247 /** 248 * Returns the specified unit multiplied by the factor <code>10<sup>-1</sup></code> 249 * 250 * @param <Q> 251 * The type of the quantity measured by the unit. 252 * @param unit 253 * any unit. 254 * @return <code>unit.times(1e-1)</code>. 255 */ 256 public static <Q extends Quantity<Q>> Unit<Q> DECI(Unit<Q> unit) { 257 return ((AbstractUnit)unit).prefix(DECI); 258 } 259 260 /** 261 * Returns the specified unit multiplied by the factor <code>10<sup>-2</sup></code> 262 * 263 * @param <Q> 264 * The type of the quantity measured by the unit. 265 * @param unit 266 * any unit. 267 * @return <code>unit.times(1e-2)</code>. 268 */ 269 public static <Q extends Quantity<Q>> Unit<Q> CENTI(Unit<Q> unit) { 270 return ((AbstractUnit)unit).prefix(CENTI); 271 } 272 273 /** 274 * Returns the specified unit multiplied by the factor <code>10<sup>-3</sup></code> 275 * 276 * @param <Q> 277 * The type of the quantity measured by the unit. 278 * @param unit 279 * any unit. 280 * @return <code>unit.times(1e-3)</code>. 281 */ 282 public static <Q extends Quantity<Q>> Unit<Q> MILLI(Unit<Q> unit) { 283 return ((AbstractUnit)unit).prefix(MILLI); 284 } 285 286 /** 287 * Returns the specified unit multiplied by the factor <code>10<sup>-6</sup></code> 288 * 289 * @param <Q> 290 * The type of the quantity measured by the unit. 291 * @param unit 292 * any unit. 293 * @return <code>unit.times(1e-6)</code>. 294 */ 295 public static <Q extends Quantity<Q>> Unit<Q> MICRO(Unit<Q> unit) { 296 return ((AbstractUnit)unit).prefix(MICRO); 297 } 298 299 /** 300 * Returns the specified unit multiplied by the factor <code>10<sup>-9</sup></code> 301 * 302 * @param <Q> 303 * The type of the quantity measured by the unit. 304 * @param unit 305 * any unit. 306 * @return <code>unit.times(1e-9)</code>. 307 */ 308 public static <Q extends Quantity<Q>> Unit<Q> NANO(Unit<Q> unit) { 309 return ((AbstractUnit)unit).prefix(NANO); 310 } 311 312 /** 313 * Returns the specified unit multiplied by the factor <code>10<sup>-12</sup></code> 314 * 315 * @param <Q> 316 * The type of the quantity measured by the unit. 317 * @param unit 318 * any unit. 319 * @return <code>unit.times(1e-12)</code>. 320 */ 321 public static <Q extends Quantity<Q>> Unit<Q> PICO(Unit<Q> unit) { 322 return ((AbstractUnit)unit).prefix(PICO); 323 } 324 325 /** 326 * Returns the specified unit multiplied by the factor <code>10<sup>-15</sup></code> 327 * 328 * @param <Q> 329 * The type of the quantity measured by the unit. 330 * @param unit 331 * any unit. 332 * @return <code>unit.times(1e-15)</code>. 333 */ 334 public static <Q extends Quantity<Q>> Unit<Q> FEMTO(Unit<Q> unit) { 335 return ((AbstractUnit)unit).prefix(FEMTO); 336 } 337 338 /** 339 * Returns the specified unit multiplied by the factor <code>10<sup>-18</sup></code> 340 * 341 * @param <Q> 342 * The type of the quantity measured by the unit. 343 * @param unit 344 * any unit. 345 * @return <code>unit.times(1e-18)</code>. 346 */ 347 public static <Q extends Quantity<Q>> Unit<Q> ATTO(Unit<Q> unit) { 348 return ((AbstractUnit)unit).prefix(ATTO); 349 } 350 351 /** 352 * Returns the specified unit multiplied by the factor <code>10<sup>-21</sup></code> 353 * 354 * @param <Q> 355 * The type of the quantity measured by the unit. 356 * @param unit 357 * any unit. 358 * @return <code>unit.times(1e-21)</code>. 359 */ 360 public static <Q extends Quantity<Q>> Unit<Q> ZEPTO(Unit<Q> unit) { 361 return ((AbstractUnit)unit).prefix(ZEPTO); 362 } 363 364 /** 365 * Returns the specified unit multiplied by the factor <code>10<sup>-24</sup></code> 366 * 367 * @param <Q> 368 * The type of the quantity measured by the unit. 369 * @param unit 370 * any unit. 371 * @return <code>unit.times(1e-24)</code>. 372 */ 373 public static <Q extends Quantity<Q>> Unit<Q> YOCTO(Unit<Q> unit) { 374 return ((AbstractUnit)unit).prefix(YOCTO); 375 } 376 377 /** 378 * Returns the symbol of this prefix. 379 * 380 * @return this prefix symbol, not {@code null}. 381 */ 382 public String getSymbol() { 383 return symbol; 384 } 385 386 /** 387 * Base part of the associated factor in base^exponent representation. 388 */ 389 public int getBase() { 390 return base; 391 } 392 393 /** 394 * Exponent part of the associated factor in base^exponent representation. 395 */ 396 public int getExponent() { 397 return exponent; 398 } 399 400}