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 common binary prefixes to be used by units.
040 * </p>
041 *
042 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
043 * @version 1.3, April 20, 2018
044 * @see <a href="https://en.wikipedia.org/wiki/Binary_prefix">Wikipedia: Binary Prefix</a>
045 * @since 2.0
046 */
047public enum BinaryPrefix implements Prefix {
048  KIBI("Ki", 1024, 1), //
049  MEBI("Mi", 1024, 2), //
050  GIBI("Gi", 1024, 3), //
051  TEBI("Ti", 1024, 4), //
052  PEBI("Pi", 1024, 5), //
053  EXBI("Ei", 1024, 6), //
054  ZEBI("Zi", 1024, 7), //
055  YOBI("Yi", 1024, 8);
056
057  /**
058   * The symbol of this prefix, as returned by {@link #getSymbol}.
059   *
060   * @serial
061   * @see #getSymbol()
062   */
063  private final String symbol;
064
065  /**
066   * Base part of the associated factor in base^exponent representation.
067   */
068  private int base;
069
070  /**
071   * Exponent part of the associated factor in base^exponent representation.
072   */
073  private int exponent;
074
075  /**
076   * Creates a new prefix.
077   *
078   * @param symbol
079   *          the symbol of this prefix.
080   * @param base
081   *          part of the associated factor in base^exponent representation.
082   * @param exponent
083   *          part of the associated factor in base^exponent representation.
084   */
085  private BinaryPrefix(String symbol, int base, int exponent) {
086    this.symbol = symbol;
087    this.base = base;
088    this.exponent = exponent;
089  }
090
091  /**
092   * Returns the specified unit multiplied by the factor <code>1024</code> (binary prefix).
093   * 
094   * @param unit
095   *          any unit.
096   * @return <code>unit.multiply(1024)</code>.
097   */
098  public static <Q extends Quantity<Q>> Unit<Q> KIBI(Unit<Q> unit) {
099    return ((AbstractUnit)unit).prefix(KIBI);
100  }
101
102  /**
103   * Returns the specified unit multiplied by the factor <code>1024<sup>2</sup></code> (binary prefix).
104   * 
105   * @param unit
106   *          any unit.
107   * @return <code>unit.multiply(1048576)</code>.
108   */
109  public static <Q extends Quantity<Q>> Unit<Q> MEBI(Unit<Q> unit) {
110    return ((AbstractUnit)unit).prefix(MEBI);
111  }
112
113  /**
114   * Returns the specified unit multiplied by the factor <code>1024<sup>3</sup></code> (binary prefix).
115   * 
116   * @param unit
117   *          any unit.
118   * @return <code>unit.multiply(1073741824)</code>.
119   */
120  public static <Q extends Quantity<Q>> Unit<Q> GIBI(Unit<Q> unit) {
121    return ((AbstractUnit)unit).prefix(GIBI);
122  }
123
124  /**
125   * Returns the specified unit multiplied by the factor <code>1024<sup>4</sup></code> (binary prefix).
126   * 
127   * @param unit
128   *          any unit.
129   * @return <code>unit.multiply(1099511627776L)</code>.
130   */
131  public static <Q extends Quantity<Q>> Unit<Q> TEBI(Unit<Q> unit) {
132    return ((AbstractUnit)unit).prefix(TEBI);
133  }
134
135  /**
136   * Returns the specified unit multiplied by the factor <code>1024<sup>5</sup></code> (binary prefix).
137   * 
138   * @param unit
139   *          any unit.
140   * @return <code>unit.multiply(1125899906842624L)</code>.
141   */
142  public static <Q extends Quantity<Q>> Unit<Q> PEBI(Unit<Q> unit) {
143    return ((AbstractUnit)unit).prefix(PEBI);
144  }
145
146  /**
147   * Returns the specified unit multiplied by the factor <code>1024<sup>6</sup></code> (binary prefix).
148   * 
149   * @param unit
150   *          any unit.
151   * @return <code>unit.multiply(1152921504606846976L)</code>.
152   */
153  public static <Q extends Quantity<Q>> Unit<Q> EXBI(Unit<Q> unit) {
154    return ((AbstractUnit)unit).prefix(EXBI);
155  }
156
157  /**
158   * Returns the specified unit multiplied by the factor <code>1024<sup>7</sup></code> (binary prefix).
159   * 
160   * @param unit
161   *          any unit.
162   * @return <code>unit.multiply(1152921504606846976d)</code>.
163   */
164  public static <Q extends Quantity<Q>> Unit<Q> ZEBI(Unit<Q> unit) {
165    return ((AbstractUnit)unit).prefix(ZEBI);
166  }
167
168  /**
169   * Returns the specified unit multiplied by the factor <code>1024<sup>8</sup></code> (binary prefix).
170   * 
171   * @param unit
172   *          any unit.
173   * @return <code>unit.multiply(1208925819614629174706176d)</code>.
174   */
175  public static <Q extends Quantity<Q>> Unit<Q> YOBI(Unit<Q> unit) {
176    return ((AbstractUnit)unit).prefix(YOBI);
177  }
178
179  /**
180   * Returns the symbol of this prefix.
181   *
182   * @return this prefix symbol, not {@code null}.
183   */
184  public String getSymbol() {
185    return symbol;
186  }
187
188  /**
189   * Base part of the associated factor in base^exponent representation.
190   */
191  public int getBase() {
192    return base;
193  }
194
195  /**
196   * Exponent part of the associated factor in base^exponent representation.
197   */
198  public int getExponent() {
199    return exponent;
200  }
201}