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