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 javax.measure;
031
032/**
033 * <p>
034 * This class provides support for the 20 prefixes used in the metric system (decimal multiples and submultiples of units). For example:
035 * 
036 * <pre>
037 * <code>
038 *     import static tech.units.indriya.unit.Units.*;  // Static import.
039 *     import static javax.measure.MetricPrefix.*; // Static import.
040 *     import javax.measure.*;
041 *     import javax.measure.quantity.*;
042 *     ...
043 *     Unit<Pressure> HECTOPASCAL = HECTO(PASCAL);
044 *     Unit<Length> KILOMETRE = KILO(METRE);
045 *     </code>
046 * </pre>
047 * 
048 * </p>
049 *
050 * @see <a href="http://en.wikipedia.org/wiki/Metric_prefix">Wikipedia: Metric Prefix</a>
051 * @author <a href="mailto:[email protected]">Jean-Marie Dautelle</a>
052 * @author <a href="mailto:[email protected]">Werner Keil</a>
053 * @version 1.9, 2018-08-08
054 * @since 2.0
055 */
056public enum MetricPrefix implements Prefix {
057  YOTTA("Y", 10, 24), //
058  ZETTA("Z", 10, 21), //
059  EXA("E", 10, 18), //
060  PETA("P", 10, 15), //
061  TERA("T", 10, 12), //
062  GIGA("G", 10, 9), //
063  MEGA("M", 10, 6), //
064  KILO("k", 10, 3), //
065  HECTO("h", 10, 2), //
066  DEKA("da", 10, 1), //
067  DECI("d", 10, -1), //
068  CENTI("c", 10, -2), //
069  MILLI("m", 10, -3), //
070  MICRO("ยต", 10, -6), //
071  NANO("n", 10, -9), //
072  PICO("p", 10, -12), //
073  FEMTO("f", 10, -15), //
074  ATTO("a", 10, -18), //
075  ZEPTO("z", 10, -21), //
076  YOCTO("y", 10, -24);
077
078  /**
079   * The symbol of this prefix, as returned by {@link #getSymbol}.
080   *
081   * @serial
082   * @see #getSymbol()
083   */
084  private final String symbol;
085
086  /**
087   * Base part of the associated factor in base^exponent representation.
088   */
089  private int base;
090
091  /**
092   * Exponent part of the associated factor in base^exponent representation.
093   */
094  private int exponent;
095
096  /**
097   * Creates a new prefix.
098   *
099   * @param symbol
100   *          the symbol of this prefix.
101   * @param base
102   *          part of the associated factor in base^exponent representation.
103   * @param exponent
104   *          part of the associated factor in base^exponent representation.
105   */
106  private MetricPrefix(String symbol, int base, int exponent) {
107    this.symbol = symbol;
108    this.base = base;
109    this.exponent = exponent;
110  }
111
112  /**
113   * Returns the specified unit multiplied by the factor <code>10<sup>24</sup></code>
114   *
115   * @param <Q>
116   *          The type of the quantity measured by the unit.
117   * @param unit
118   *          any unit.
119   * @return <code>unit.times(1e24)</code>.
120   */
121  public static <Q extends Quantity<Q>> Unit<Q> YOTTA(Unit<Q> unit) {
122    return unit.prefix(YOTTA);
123  }
124
125  /**
126   * Returns the specified unit multiplied by the factor <code>10<sup>21</sup></code>
127   *
128   * @param <Q>
129   *          The type of the quantity measured by the unit.
130   * @param unit
131   *          any unit.
132   * @return <code>unit.times(1e21)</code>.
133   */
134  public static <Q extends Quantity<Q>> Unit<Q> ZETTA(Unit<Q> unit) {
135    return unit.prefix(ZETTA);
136  }
137
138  /**
139   * Returns the specified unit multiplied by the factor <code>10<sup>18</sup></code>
140   *
141   * @param <Q>
142   *          The type of the quantity measured by the unit.
143   * @param unit
144   *          any unit.
145   * @return <code>unit.times(1e18)</code>.
146   */
147  public static <Q extends Quantity<Q>> Unit<Q> EXA(Unit<Q> unit) {
148    return unit.prefix(EXA);
149  }
150
151  /**
152   * Returns the specified unit multiplied by the factor <code>10<sup>15</sup></code>
153   *
154   * @param <Q>
155   *          The type of the quantity measured by the unit.
156   * @param unit
157   *          any unit.
158   * @return <code>unit.times(1e15)</code>.
159   */
160  public static <Q extends Quantity<Q>> Unit<Q> PETA(Unit<Q> unit) {
161    return unit.prefix(PETA);
162  }
163
164  /**
165   * Returns the specified unit multiplied by the factor <code>10<sup>12</sup></code>
166   *
167   * @param <Q>
168   *          The type of the quantity measured by the unit.
169   * @param unit
170   *          any unit.
171   * @return <code>unit.times(1e12)</code>.
172   */
173  public static <Q extends Quantity<Q>> Unit<Q> TERA(Unit<Q> unit) {
174    return unit.prefix(TERA);
175  }
176
177  /**
178   * Returns the specified unit multiplied by the factor <code>10<sup>9</sup></code>
179   *
180   * @param <Q>
181   *          The type of the quantity measured by the unit.
182   * @param unit
183   *          any unit.
184   * @return <code>unit.times(1e9)</code>.
185   */
186  public static <Q extends Quantity<Q>> Unit<Q> GIGA(Unit<Q> unit) {
187    return unit.prefix(GIGA);
188  }
189
190  /**
191   * Returns the specified unit multiplied by the factor <code>10<sup>6</sup></code>
192   *
193   * @param <Q>
194   *          The type of the quantity measured by the unit.
195   * @param unit
196   *          any unit.
197   * @return <code>unit.times(1e6)</code>.
198   */
199  public static <Q extends Quantity<Q>> Unit<Q> MEGA(Unit<Q> unit) {
200    return unit.prefix(MEGA);
201  }
202
203  /**
204   * Returns the specified unit multiplied by the factor <code>10<sup>3</sup></code>
205   *
206   * @param <Q>
207   *          The type of the quantity measured by the unit.
208   * @param unit
209   *          any unit.
210   * @return <code>unit.times(1e3)</code>.
211   */
212  public static <Q extends Quantity<Q>> Unit<Q> KILO(Unit<Q> unit) {
213    return unit.prefix(KILO);
214  }
215
216  /**
217   * Returns the specified unit multiplied by the factor <code>10<sup>2</sup></code>
218   *
219   * @param <Q>
220   *          The type of the quantity measured by the unit.
221   * @param unit
222   *          any unit.
223   * @return <code>unit.times(1e2)</code>.
224   */
225  public static <Q extends Quantity<Q>> Unit<Q> HECTO(Unit<Q> unit) {
226    return unit.prefix(HECTO);
227  }
228
229  /**
230   * Returns the specified unit multiplied by the factor <code>10<sup>1</sup></code>
231   *
232   * @param <Q>
233   *          The type of the quantity measured by the unit.
234   * @param unit
235   *          any unit.
236   * @return <code>unit.times(1e1)</code>.
237   */
238  public static <Q extends Quantity<Q>> Unit<Q> DEKA(Unit<Q> unit) {
239    return unit.prefix(DEKA);
240  }
241
242  /**
243   * Returns the specified unit multiplied by the factor <code>10<sup>-1</sup></code>
244   *
245   * @param <Q>
246   *          The type of the quantity measured by the unit.
247   * @param unit
248   *          any unit.
249   * @return <code>unit.times(1e-1)</code>.
250   */
251  public static <Q extends Quantity<Q>> Unit<Q> DECI(Unit<Q> unit) {
252    return unit.prefix(DECI);
253  }
254
255  /**
256   * Returns the specified unit multiplied by the factor <code>10<sup>-2</sup></code>
257   *
258   * @param <Q>
259   *          The type of the quantity measured by the unit.
260   * @param unit
261   *          any unit.
262   * @return <code>unit.times(1e-2)</code>.
263   */
264  public static <Q extends Quantity<Q>> Unit<Q> CENTI(Unit<Q> unit) {
265    return unit.prefix(CENTI);
266  }
267
268  /**
269   * Returns the specified unit multiplied by the factor <code>10<sup>-3</sup></code>
270   *
271   * @param <Q>
272   *          The type of the quantity measured by the unit.
273   * @param unit
274   *          any unit.
275   * @return <code>unit.times(1e-3)</code>.
276   */
277  public static <Q extends Quantity<Q>> Unit<Q> MILLI(Unit<Q> unit) {
278    return unit.prefix(MILLI);
279  }
280
281  /**
282   * Returns the specified unit multiplied by the factor <code>10<sup>-6</sup></code>
283   *
284   * @param <Q>
285   *          The type of the quantity measured by the unit.
286   * @param unit
287   *          any unit.
288   * @return <code>unit.times(1e-6)</code>.
289   */
290  public static <Q extends Quantity<Q>> Unit<Q> MICRO(Unit<Q> unit) {
291    return unit.prefix(MICRO);
292  }
293
294  /**
295   * Returns the specified unit multiplied by the factor <code>10<sup>-9</sup></code>
296   *
297   * @param <Q>
298   *          The type of the quantity measured by the unit.
299   * @param unit
300   *          any unit.
301   * @return <code>unit.times(1e-9)</code>.
302   */
303  public static <Q extends Quantity<Q>> Unit<Q> NANO(Unit<Q> unit) {
304    return unit.prefix(NANO);
305  }
306
307  /**
308   * Returns the specified unit multiplied by the factor <code>10<sup>-12</sup></code>
309   *
310   * @param <Q>
311   *          The type of the quantity measured by the unit.
312   * @param unit
313   *          any unit.
314   * @return <code>unit.times(1e-12)</code>.
315   */
316  public static <Q extends Quantity<Q>> Unit<Q> PICO(Unit<Q> unit) {
317    return unit.prefix(PICO);
318  }
319
320  /**
321   * Returns the specified unit multiplied by the factor <code>10<sup>-15</sup></code>
322   *
323   * @param <Q>
324   *          The type of the quantity measured by the unit.
325   * @param unit
326   *          any unit.
327   * @return <code>unit.times(1e-15)</code>.
328   */
329  public static <Q extends Quantity<Q>> Unit<Q> FEMTO(Unit<Q> unit) {
330    return unit.prefix(FEMTO);
331  }
332
333  /**
334   * Returns the specified unit multiplied by the factor <code>10<sup>-18</sup></code>
335   *
336   * @param <Q>
337   *          The type of the quantity measured by the unit.
338   * @param unit
339   *          any unit.
340   * @return <code>unit.times(1e-18)</code>.
341   */
342  public static <Q extends Quantity<Q>> Unit<Q> ATTO(Unit<Q> unit) {
343    return unit.prefix(ATTO);
344  }
345
346  /**
347   * Returns the specified unit multiplied by the factor <code>10<sup>-21</sup></code>
348   *
349   * @param <Q>
350   *          The type of the quantity measured by the unit.
351   * @param unit
352   *          any unit.
353   * @return <code>unit.times(1e-21)</code>.
354   */
355  public static <Q extends Quantity<Q>> Unit<Q> ZEPTO(Unit<Q> unit) {
356    return unit.prefix(ZEPTO);
357  }
358
359  /**
360   * Returns the specified unit multiplied by the factor <code>10<sup>-24</sup></code>
361   *
362   * @param <Q>
363   *          The type of the quantity measured by the unit.
364   * @param unit
365   *          any unit.
366   * @return <code>unit.times(1e-24)</code>.
367   */
368  public static <Q extends Quantity<Q>> Unit<Q> YOCTO(Unit<Q> unit) {
369    return unit.prefix(YOCTO);
370  }
371
372  /**
373   * Returns the symbol of this prefix.
374   *
375   * @return this prefix symbol, not {@code null}.
376   */
377  @Override
378  public String getSymbol() {
379    return symbol;
380  }
381
382  /**
383   * Base part of the associated factor in base^exponent representation.
384   */
385  @Override
386  public int getBase() {
387    return base;
388  }
389
390  /**
391   * Exponent part of the associated factor in base^exponent representation.
392   */
393  @Override
394  public int getExponent() {
395    return exponent;
396  }
397
398  /**
399   * Returns the name of this prefix.
400   *
401   * @return this prefix name, not {@code null}.
402   */
403  @Override
404  public String getName() {
405    return name();
406  }
407}