001/*
002 * Units of Measurement API
003 * Copyright (c) 2014-2023, 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 * Provides support for the 24 prefixes used in the metric system (decimal multiples and submultiples of units). For example:
034 *
035 * <pre>
036 * {@code import static tech.units.indriya.unit.Units.*;  // Static import (from the RI).
037 * import static javax.measure.MetricPrefix.*; // Static import.
038 * import javax.measure.*;
039 * import javax.measure.quantity.*;
040 * ...
041 * Unit<Pressure> HECTOPASCAL = HECTO(PASCAL);
042 * Unit<Length> KILOMETRE = KILO(METRE);} 
043 * </pre>
044 * You could also apply <code>Unit.prefix</code>:
045 * <pre>
046 * {@code ...
047 * Unit<Pressure> HECTOPASCAL = PASCAL.prefix(HECTO);
048 * Unit<Length> KILOMETRE = METRE.prefix(KILO);}
049 * </pre>
050 * 
051 * <p>
052 * <b>Do not use ordinal() to obtain the numeric representation of MetricPrefix. Use getValue() and getExponent() instead.</b>
053 * </p>
054 * 
055 * <dl>
056 * <dt><span class="strong">Implementation Requirements</span></dt><dd>This is an immutable and thread-safe enum.</dd>
057 * </dl>
058 * 
059 * @see <a href="https://en.wikipedia.org/wiki/Metric_prefix">Wikipedia: Metric Prefix</a>
060 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
061 * @author <a href="mailto:werner@units.tech">Werner Keil</a>
062 * @version 2.4, October 31, 2023
063 * @since 2.0
064 */
065public enum MetricPrefix implements Prefix {
066        /** Prefix for 10<sup>30</sup>. */
067    QUETTA("Q", 30),
068        /** Prefix for 10<sup>27</sup>. */
069    RONNA("R", 27),
070        /** Prefix for 10<sup>24</sup>. */
071    YOTTA("Y", 24),
072    /** Prefix for 10<sup>21</sup>. */
073    ZETTA("Z", 21),
074    /** Prefix for 10<sup>18</sup>. */
075    EXA("E", 18),
076    /** Prefix for 10<sup>15</sup>. */
077    PETA("P", 15),
078    /** Prefix for 10<sup>12</sup>. */
079    TERA("T", 12),
080    /** Prefix for 10<sup>9</sup>.
081     * @see <a href="https://en.wikipedia.org/wiki/Giga-">Wikipedia: Giga</a>  
082     */
083    GIGA("G", 9),
084    /** Prefix for 10<sup>6</sup>. 
085     * @see <a href="https://en.wikipedia.org/wiki/Mega-">Wikipedia: Mega</a> */
086    MEGA("M", 6),
087    /** Prefix for 10<sup>3</sup>.
088     * @see <a href="https://en.wikipedia.org/wiki/Kilo-">Wikipedia: Kilo</a> 
089     */
090    KILO("k", 3),
091    /** Prefix for 10<sup>2</sup>. 
092     * @see <a href="https://en.wikipedia.org/wiki/Hecto-">Wikipedia: Hecto</a> */
093    HECTO("h", 2),
094    /** Prefix for 10<sup>1</sup>. 
095     * @see <a href="https://en.wikipedia.org/wiki/Deca-">Wikipedia: Deca</a> */
096    DECA("da", 1),
097    /** Prefix for 10<sup>-1</sup>. 
098     * @see <a href="https://en.wikipedia.org/wiki/Deci-">Wikipedia: Deci</a> */
099    DECI("d", -1),
100    /** Prefix for 10<sup>-2</sup>.
101     * @see <a href="https://en.wikipedia.org/wiki/Centi-">Wikipedia: Centi</a> */     
102    CENTI("c", -2),
103    /** Prefix for 10<sup>-3</sup>.
104     * @see <a href="https://en.wikipedia.org/wiki/Milli-">Wikipedia: Milli</a> */
105    MILLI("m", -3),
106    /** Prefix for 10<sup>-6</sup>.
107     * @see <a href="https://en.wikipedia.org/wiki/Micro-">Wikipedia: Micro</a> */
108    MICRO("\u00b5", -6),
109    /** Prefix for 10<sup>-9</sup>.
110     * @see <a href="https://en.wikipedia.org/wiki/Nano-">Wikipedia: Nano</a> */
111    NANO("n", -9),
112    /** Prefix for 10<sup>-12</sup>. */
113    PICO("p", -12),
114    /** Prefix for 10<sup>-15</sup>. */
115    FEMTO("f", -15),
116    /** Prefix for 10<sup>-18</sup>. */
117    ATTO("a", -18),
118    /** Prefix for 10<sup>-21</sup>. */
119    ZEPTO("z", -21),
120    /** Prefix for 10<sup>-24</sup>. */
121    YOCTO("y", -24),
122        /** Prefix for 10<sup>-27</sup>. */
123    RONTO("r", -27),
124    /** Prefix for 10<sup>-30</sup>. */
125    QUECTO("q", -30);
126
127    /**
128     * The symbol of this prefix, as returned by {@link #getSymbol}.
129     *
130     * @serial
131     * @see #getSymbol()
132     */
133    private final String symbol;
134
135    /**
136     * Exponent part of the associated factor in base^exponent representation.
137     */
138    private final int exponent;
139
140    /**
141     * Creates a new prefix.
142     *
143     * @param symbol
144     *          the symbol of this prefix.
145     * @param exponent
146     *          part of the associated factor in base^exponent representation.
147     */
148    private MetricPrefix(String symbol, int exponent) {
149        this.symbol = symbol;
150        this.exponent = exponent;
151    }
152    
153    /**
154     * Returns the specified unit multiplied by the factor <code>10<sup>30</sup></code>
155     *
156     * @param <Q>
157     *          type of the quantity measured by the unit.
158     * @param unit
159     *          any unit.
160     * @return <code>unit.prefix(1e30)</code>.
161     * @see #QUETTA
162     */
163    public static <Q extends Quantity<Q>> Unit<Q> QUETTA(Unit<Q> unit) {
164        return unit.prefix(QUETTA);
165    }
166    
167    /**
168     * Returns the specified unit multiplied by the factor <code>10<sup>27</sup></code>
169     *
170     * @param <Q>
171     *          type of the quantity measured by the unit.
172     * @param unit
173     *          any unit.
174     * @return <code>unit.prefix(1e27)</code>.
175     * @see #RONNA
176     */
177    public static <Q extends Quantity<Q>> Unit<Q> RONNA(Unit<Q> unit) {
178        return unit.prefix(RONNA);
179    }
180
181    /**
182     * Returns the specified unit multiplied by the factor <code>10<sup>24</sup></code>
183     *
184     * @param <Q>
185     *          type of the quantity measured by the unit.
186     * @param unit
187     *          any unit.
188     * @return <code>unit.prefix(1e24)</code>.
189     * @see #YOTTA
190     */
191    public static <Q extends Quantity<Q>> Unit<Q> YOTTA(Unit<Q> unit) {
192        return unit.prefix(YOTTA);
193    }
194
195    /**
196     * Returns the specified unit multiplied by the factor <code>10<sup>21</sup></code>
197     *
198     * @param <Q>
199     *          type of the quantity measured by the unit.
200     * @param unit
201     *          any unit.
202     * @return <code>unit.prefix(1e21)</code>.
203     * @see #ZETTA
204     */
205    public static <Q extends Quantity<Q>> Unit<Q> ZETTA(Unit<Q> unit) {
206        return unit.prefix(ZETTA);
207    }
208
209    /**
210     * Returns the specified unit multiplied by the factor <code>10<sup>18</sup></code>
211     *
212     * @param <Q>
213     *          type of the quantity measured by the unit.
214     * @param unit
215     *          any unit.
216     * @return <code>unit.prefix(1e18)</code>.
217     * @see #EXA
218     */
219    public static <Q extends Quantity<Q>> Unit<Q> EXA(Unit<Q> unit) {
220        return unit.prefix(EXA);
221    }
222
223    /**
224     * Returns the specified unit multiplied by the factor <code>10<sup>15</sup></code>
225     *
226     * @param <Q>
227     *          type of the quantity measured by the unit.
228     * @param unit
229     *          any unit.
230     * @return <code>unit.prefix(1e15)</code>.
231     * @see #PETA
232     */
233    public static <Q extends Quantity<Q>> Unit<Q> PETA(Unit<Q> unit) {
234        return unit.prefix(PETA);
235    }
236
237    /**
238     * Returns the specified unit multiplied by the factor <code>10<sup>12</sup></code>
239     *
240     * @param <Q>
241     *          type of the quantity measured by the unit.
242     * @param unit
243     *          any unit.
244     * @return <code>unit.prefix(1e12)</code>.
245     * @see #TERA
246     */
247    public static <Q extends Quantity<Q>> Unit<Q> TERA(Unit<Q> unit) {
248        return unit.prefix(TERA);
249    }
250
251    /**
252     * Returns the specified unit multiplied by the factor <code>10<sup>9</sup></code>
253     *
254     * @param <Q>
255     *          type of the quantity measured by the unit.
256     * @param unit
257     *          any unit.
258     * @return <code>unit.prefix(1e9)</code>.
259     * @see #GIGA
260     */
261    public static <Q extends Quantity<Q>> Unit<Q> GIGA(Unit<Q> unit) {
262        return unit.prefix(GIGA);
263    }
264
265    /**
266     * Returns the specified unit multiplied by the factor <code>10<sup>6</sup></code>
267     *
268     * @param <Q>
269     *          type of the quantity measured by the unit.
270     * @param unit
271     *          any unit.
272     * @return <code>unit.prefix(1e6)</code>.
273     * @see #MEGA
274     */
275    public static <Q extends Quantity<Q>> Unit<Q> MEGA(Unit<Q> unit) {
276        return unit.prefix(MEGA);
277    }
278
279    /**
280     * Returns the specified unit multiplied by the factor <code>10<sup>3</sup></code>
281     *
282     * @param <Q>
283     *          type of the quantity measured by the unit.
284     * @param unit
285     *          any unit.
286     * @return <code>unit.prefix(1e3)</code>.
287     * @see #KILO
288     */
289    public static <Q extends Quantity<Q>> Unit<Q> KILO(Unit<Q> unit) {
290        return unit.prefix(KILO);
291    }
292
293    /**
294     * Returns the specified unit multiplied by the factor <code>10<sup>2</sup></code>
295     *
296     * @param <Q>
297     *          type of the quantity measured by the unit.
298     * @param unit
299     *          any unit.
300     * @return <code>unit.prefix(1e2)</code>.
301     * @see #HECTO
302     */
303    public static <Q extends Quantity<Q>> Unit<Q> HECTO(Unit<Q> unit) {
304        return unit.prefix(HECTO);
305    }
306
307    /**
308     * Returns the specified unit multiplied by the factor <code>10<sup>1</sup></code>
309     *
310     * @param <Q>
311     *          type of the quantity measured by the unit.
312     * @param unit
313     *          any unit.
314     * @return <code>unit.prefix(1e1)</code>.
315     * @see #DECA
316     */
317    public static <Q extends Quantity<Q>> Unit<Q> DECA(Unit<Q> unit) {
318        return unit.prefix(DECA);
319    }
320    
321    /**
322     * US alias for <code>DECA</code>.
323     *
324     * @param <Q>
325     *          type of the quantity measured by the unit.
326     * @param unit
327     *          any unit.
328     * @return <code>unit.prefix(1e1)</code>.
329     * @see #DECA
330     */
331    public static <Q extends Quantity<Q>> Unit<Q> DEKA(Unit<Q> unit) {
332        return unit.prefix(DECA);
333    }
334
335    /**
336     * Returns the specified unit multiplied by the factor <code>10<sup>-1</sup></code>
337     *
338     * @param <Q>
339     *          type of the quantity measured by the unit.
340     * @param unit
341     *          any unit.
342     * @return <code>unit.prefix(1e-1)</code>.
343     * @see #DECI 
344     */
345    public static <Q extends Quantity<Q>> Unit<Q> DECI(Unit<Q> unit) {
346        return unit.prefix(DECI);
347    }
348
349    /**
350     * Returns the specified unit multiplied by the factor <code>10<sup>-2</sup></code>
351     *
352     * @param <Q>
353     *          type of the quantity measured by the unit.
354     * @param unit
355     *          any unit.
356     * @return <code>unit.prefix(1e-2)</code>.
357     * @see #CENTI
358     */
359    public static <Q extends Quantity<Q>> Unit<Q> CENTI(Unit<Q> unit) {
360        return unit.prefix(CENTI);
361    }
362
363    /**
364     * Returns the specified unit multiplied by the factor <code>10<sup>-3</sup></code>
365     *
366     * @param <Q>
367     *          type of the quantity measured by the unit.
368     * @param unit
369     *          any unit.
370     * @return <code>unit.prefix(1e-3)</code>.
371     * @see #MILLI
372     */
373    public static <Q extends Quantity<Q>> Unit<Q> MILLI(Unit<Q> unit) {
374        return unit.prefix(MILLI);
375    }
376
377    /**
378     * Returns the specified unit multiplied by the factor <code>10<sup>-6</sup></code>
379     *
380     * @param <Q>
381     *          type of the quantity measured by the unit.
382     * @param unit
383     *          any unit.
384     * @return <code>unit.prefix(1e-6)</code>.
385     * @see #MICRO 
386     */
387    public static <Q extends Quantity<Q>> Unit<Q> MICRO(Unit<Q> unit) {
388        return unit.prefix(MICRO);
389    }
390
391    /**
392     * Returns the specified unit multiplied by the factor <code>10<sup>-9</sup></code>
393     *
394     * @param <Q>
395     *          type of the quantity measured by the unit.
396     * @param unit
397     *          any unit.
398     * @return <code>unit.prefix(1e-9)</code>.
399     * @see #NANO 
400     */
401    public static <Q extends Quantity<Q>> Unit<Q> NANO(Unit<Q> unit) {
402        return unit.prefix(NANO);
403    }
404
405    /**
406     * Returns the specified unit multiplied by the factor <code>10<sup>-12</sup></code>
407     *
408     * @param <Q>
409     *          type of the quantity measured by the unit.
410     * @param unit
411     *          any unit.
412     * @return <code>unit.prefix(1e-12)</code>.
413     * @see #PICO
414     */
415    public static <Q extends Quantity<Q>> Unit<Q> PICO(Unit<Q> unit) {
416        return unit.prefix(PICO);
417    }
418
419    /**
420     * Returns the specified unit multiplied by the factor <code>10<sup>-15</sup></code>
421     *
422     * @param <Q>
423     *          type of the quantity measured by the unit.
424     * @param unit
425     *          any unit.
426     * @return <code>unit.prefix(1e-15)</code>.
427     * @see #FEMTO
428     */
429    public static <Q extends Quantity<Q>> Unit<Q> FEMTO(Unit<Q> unit) {
430        return unit.prefix(FEMTO);
431    }
432
433    /**
434     * Returns the specified unit multiplied by the factor <code>10<sup>-18</sup></code>
435     *
436     * @param <Q>
437     *          type of the quantity measured by the unit.
438     * @param unit
439     *          any unit.
440     * @return <code>unit.prefix(1e-18)</code>.
441     * @see #ATTO 
442     */
443    public static <Q extends Quantity<Q>> Unit<Q> ATTO(Unit<Q> unit) {
444        return unit.prefix(ATTO);
445    }
446
447    /**
448     * Returns the specified unit multiplied by the factor <code>10<sup>-21</sup></code>
449     *
450     * @param <Q>
451     *          type of the quantity measured by the unit.
452     * @param unit
453     *          any unit.
454     * @return <code>unit.prefix(1e-21)</code>.
455     * #see ZEPTO
456     */
457    public static <Q extends Quantity<Q>> Unit<Q> ZEPTO(Unit<Q> unit) {
458        return unit.prefix(ZEPTO);
459    }
460
461    /**
462     * Returns the specified unit multiplied by the factor <code>10<sup>-24</sup></code>
463     *
464     * @param <Q>
465     *          type of the quantity measured by the unit.
466     * @param unit
467     *          any unit.
468     * @return <code>unit.prefix(1e-24)</code>.
469     * @see #YOCTO
470     */
471    public static <Q extends Quantity<Q>> Unit<Q> YOCTO(Unit<Q> unit) {
472        return unit.prefix(YOCTO);
473    }
474    
475    /**
476     * Returns the specified unit multiplied by the factor <code>10<sup>-27</sup></code>
477     *
478     * @param <Q>
479     *          type of the quantity measured by the unit.
480     * @param unit
481     *          any unit.
482     * @return <code>unit.prefix(1e-27)</code>.
483     * @see #RONTO
484     */
485    public static <Q extends Quantity<Q>> Unit<Q> RONTO(Unit<Q> unit) {
486        return unit.prefix(RONTO);
487    }
488
489    /**
490     * Returns the specified unit multiplied by the factor <code>10<sup>-30</sup></code>
491     *
492     * @param <Q>
493     *          type of the quantity measured by the unit.
494     * @param unit
495     *          any unit.
496     * @return <code>unit.prefix(1e-30)</code>.
497     * @see #QUECTO
498     */
499    public static <Q extends Quantity<Q>> Unit<Q> QUECTO(Unit<Q> unit) {
500        return unit.prefix(QUECTO);
501    }
502    
503    /**
504     * Returns the symbol of this prefix.
505     *
506     * @return this prefix symbol, not {@code null}.
507     */
508    @Override
509    public String getSymbol() {
510        return symbol;
511    }
512
513    /**
514     * Base part of the associated factor in {@code base^exponent} representation. For metric prefix, this is always 10.
515     */
516    @Override
517    public Integer getValue() {
518        return 10;
519    }
520
521    /**
522     * Exponent part of the associated factor in {@code base^exponent} representation.
523     */
524    @Override
525    public int getExponent() {
526        return exponent;
527    }
528
529    /**
530     * Returns the name of this prefix.
531     *
532     * @return this prefix name, not {@code null}.
533     */
534    @Override
535    public String getName() {
536        return name();
537    }
538}