001/*
002 * Units of Measurement Systems
003 * Copyright (c) 2005-2018, Jean-Marie Dautelle, Werner Keil and others.
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-363, Units of Measurement nor the names of their contributors may be used to
017 *    endorse or promote products 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 systems.uom.common;
031
032import static tec.units.indriya.unit.MetricPrefix.MICRO;
033import static tec.units.indriya.unit.Units.*;
034
035import javax.measure.Unit;
036import javax.measure.quantity.Area;
037import javax.measure.quantity.Force;
038import javax.measure.quantity.Length;
039import javax.measure.quantity.Mass;
040import javax.measure.quantity.Temperature;
041import javax.measure.quantity.Time;
042import javax.measure.quantity.Volume;
043import javax.measure.spi.SystemOfUnits;
044
045import tec.units.indriya.AbstractSystemOfUnits;
046import tec.units.indriya.AbstractUnit;
047import tec.units.indriya.format.SimpleUnitFormat;
048import tec.units.indriya.unit.ProductUnit;
049
050/**
051 * <p>
052 * This class contains units from the Imperial system.
053 * </p>
054 * <p>
055 * 
056 * @noextend This class is not intended to be extended by clients.
057 * 
058 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
059 * @version 1.0.4, $Date: 2018-05-02 $
060 * @see <a href="http://en.wikipedia.org/wiki/Imperial_unit">Wikipedia: Imperial
061 *      Units</a>
062 * @see <a href=
063 *      "https://en.wikipedia.org/wiki/Imperial_and_US_customary_measurement_systems">
064 * @since 0.2
065 */
066public final class Imperial extends AbstractSystemOfUnits {
067    private static final String SYSTEM_NAME = "Imperial";
068    
069    /**
070     * Holds the avoirdupois pound: 0.45359237 kg exact
071     */
072    static final int AVOIRDUPOIS_POUND_DIVIDEND = 45359237;
073
074    static final int AVOIRDUPOIS_POUND_DIVISOR = 100000000;
075    
076    /**
077     * Holds the standard gravity constant: 9.80665 m/s² exact.
078     */
079    private static final int STANDARD_GRAVITY_DIVIDEND = 980665;
080
081    private static final int STANDARD_GRAVITY_DIVISOR = 100000;
082
083    /**
084     * Default constructor (prevents this class from being instantiated).
085     */
086    private Imperial() {
087    }
088
089    /**
090     * Returns the unique instance of this class.
091     * 
092     * @return the Imperial instance.
093     */
094    public static SystemOfUnits getInstance() {
095        return INSTANCE;
096    }
097
098    private static final Imperial INSTANCE = new Imperial();
099
100    // //////////
101    // Length //
102    // //////////
103
104    /**
105     * A unit of length equal to <code>0.0254 m</code> (standard name
106     * <code>in</code>).
107     */
108    public static final Unit<Length> INCH = addUnit(USCustomary.INCH, "Inch", "in");
109
110    // ////////
111    // Mass //
112    // ////////
113
114    /**
115     * A unit of mass equal to <code>453.59237 grams</code> (avoirdupois pound,
116     * standard name <code>lb</code>).
117     */
118    public static final Unit<Mass> POUND = addUnit(
119            KILOGRAM.multiply(AVOIRDUPOIS_POUND_DIVIDEND).divide(AVOIRDUPOIS_POUND_DIVISOR), "Pound", "lb", true);
120    // LABEL);
121    /**
122     * An English and imperial unit of weight or mass now equal to 14
123     * avoirdupois pounds or 6.35029318 kg (<code>st</code>).
124     */
125    public static final Unit<Mass> STONE = addUnit(KILOGRAM.multiply(6.35029318), "st", true);
126
127    /**
128     * A unit of mass equal to <code>1 / 16 {@link #POUND}</code> (standard name
129     * <code>oz</code>).
130     */
131    public static final Unit<Mass> OUNCE = addUnit(POUND.divide(16), "oz");
132
133    /**
134     * A unit of mass equal to <code>2240 {@link #POUND}</code> (long ton,
135     * standard name <code>ton_uk</code>).
136     */
137    public static final Unit<Mass> TON_UK = addUnit(POUND.multiply(2240), "ton_uk");
138
139    /**
140     * A unit of mass equal to <code>1000 kg</code> (metric ton, standard name
141     * <code>t</code>).
142     */
143    public static final Unit<Mass> METRIC_TON = addUnit(KILOGRAM.multiply(1000), "t");
144
145    // ///////////////
146    // Temperature //
147    // ///////////////
148
149    /**
150     * A unit of temperature equal to <code>5/9 °K</code> (standard name
151     * <code>°R</code>).
152     */
153    static final Unit<Temperature> RANKINE = addUnit(KELVIN.multiply(5).divide(9), "°R", true);
154
155    /**
156     * A unit of temperature equal to degree Rankine minus
157     * <code>459.67 °R</code> (standard name <code>°F</code>).
158     * 
159     * @see #RANKINE
160     */
161    static final Unit<Temperature> FAHRENHEIT = addUnit(RANKINE.shift(459.67), "°F", true);
162
163    //////////////
164    // Time     //
165    //////////////
166    /**
167     * A unit of time equal to <code>60 s</code> (standard name <code>min</code>
168     * ).
169     */
170    static final Unit<Time> MINUTE = addUnit(SECOND.multiply(60));
171
172    /**
173     * A unit of duration equal to <code>60 {@link #MINUTE}</code> (standard
174     * name <code>h</code>).
175     */
176    static final Unit<Time> HOUR = addUnit(MINUTE.multiply(60));
177    
178    //////////
179    // Area //
180    //////////
181
182    /**
183     * A unit of area (standard name <code>sft</code> ).
184     */
185    public static final Unit<Area> SQUARE_FOOT = addUnit(USCustomary.SQUARE_FOOT, "sft", true);
186
187    /**
188     * One acre is 43,560 <code>square feet</code> (standard name
189     * <code>ac</code> ).
190     */
191    public static final Unit<Area> ACRE = addUnit(USCustomary.SQUARE_FOOT.multiply(43560), "Acre", "ac", true);
192
193    ////////////
194    // Volume //
195    ////////////
196    /**
197     * A unit of volume equal to one cubic decimeter (default label
198     * <code>L</code>, also recognized <code>µL, mL, cL, dL</code>).
199     */
200    public static final Unit<Volume> LITRE = addUnit(CUBIC_METRE.divide(1000), "L", true);
201
202    /**
203     * A unit of volume equal to one cubic inch (<code>in³</code>).
204     */
205    public static final Unit<Volume> CUBIC_INCH = addUnit(new ProductUnit<Volume>(USCustomary.INCH.pow(3)),
206            "Cubic Inch", "in³");
207
208    /**
209     * A unit of volume equal to <code>4.546 09 {@link #LITRE}</code> (standard
210     * name <code>gal_uk</code>).
211     */
212    public static final Unit<Volume> GALLON_UK = addUnit(LITRE.multiply(454609).divide(100000), "gal_uk");
213
214    /**
215     * A unit of volume equal to one UK gallon, Liquid Unit.
216     */
217    // public static final Unit<Volume> GALLON_LIQUID =
218    // addUnit(CUBIC_INCH.multiply(277.42));
219
220    /**
221     * A unit of volume equal to <code>1 / 160 {@link #GALLON_UK}</code>
222     * (standard name <code>fl_oz_uk</code>).
223     */
224    static final Unit<Volume> FLUID_OUNCE_UK = GALLON_UK.divide(160);
225
226    /**
227     * A unit of volume equal to <code>1 / 160 {@link #GALLON_LIQUID}</code>
228     * (standard name <code>fl_oz</code>).
229     */
230    public static final Unit<Volume> FLUID_OUNCE = addUnit(FLUID_OUNCE_UK, "fl_oz", true);
231
232    /**
233     * A unit of volume equal to <code>1 / 160 {@link #GALLON_LIQUID}</code>
234     * (standard name <code>fl_oz</code>).
235     * @deprecated use FLUID_OUNCE
236     */
237    public static final Unit<Volume> OUNCE_LIQUID = FLUID_OUNCE_UK;
238
239    /**
240     * A unit of volume equal to <code>5 {@link #FLUID_OUNCE}</code> (standard
241     * name <code>gi</code>).
242     */
243    public static final Unit<Volume> GILL = addUnit(FLUID_OUNCE.multiply(5), "Gill", "gi");
244
245    /**
246     * A unit of volume equal to <code>20 {@link #FLUID_OUNCE}</code> (standard
247     * name <code>pt</code>).
248     */
249    public static final Unit<Volume> PINT = addUnit(FLUID_OUNCE.multiply(20), "Pint", "pt", true);
250
251    /**
252     * A unit of volume equal to <code>40 {@link #FLUID_OUNCE}</code> (standard
253     * name <code>qt</code>).
254     */
255    public static final Unit<Volume> QUART = addUnit(FLUID_OUNCE.multiply(40), "Quart", "qt");
256
257    /**
258     * A unit of volume <code>~ 1 drop or 0.95 grain of water </code> (standard
259     * name <code>min</code>).
260     */
261    public static final Unit<Volume> MINIM = addUnit(MICRO(LITRE).multiply(59.1938802d), "Minim", "min_br");
262
263    /**
264     * A unit of volume equal to <code>20 {@link #MINIM}</code> (standard name
265     * <code>fl scr</code>).
266     */
267    public static final Unit<Volume> FLUID_SCRUPLE = addUnit(MINIM.multiply(60), "fl scr", true);
268
269    /**
270     * A unit of volume equal to <code>3 {@link #FLUID_SCRUPLE}</code> (standard
271     * name <code>fl drc</code>).
272     */
273    public static final Unit<Volume> FLUID_DRACHM = addUnit(FLUID_SCRUPLE.multiply(3), "fl drc", true);
274    
275    /**
276     * A unit of force equal to <code>{@link #POUND}·{@link #G}</code>
277     * (standard name <code>lbf</code>).
278     */
279    public static final Unit<Force> POUND_FORCE = addUnit(
280            NEWTON.multiply(1L * AVOIRDUPOIS_POUND_DIVIDEND * STANDARD_GRAVITY_DIVIDEND)
281                    .divide(1L * AVOIRDUPOIS_POUND_DIVISOR * STANDARD_GRAVITY_DIVISOR), "lbf");
282    /**
283     * A unit of force equal to <code>9.80665 N</code> (standard name
284     * <code>kgf</code>).
285     */
286    static final Unit<Force> KILOGRAM_FORCE = addUnit(
287            NEWTON.multiply(STANDARD_GRAVITY_DIVIDEND).divide(STANDARD_GRAVITY_DIVISOR));
288
289    
290    /**
291     * Adds a new unit not mapped to any specified quantity type.
292     *
293     * @param unit
294     *            the unit being added.
295     * @return <code>unit</code>.
296     */
297    private static <U extends Unit<?>> U addUnit(U unit) {
298        INSTANCE.units.add(unit);
299        return unit;
300    }
301
302    /**
303     * Adds a new unit not mapped to any specified quantity type and puts a text
304     * as symbol or label.
305     *
306     * @param unit
307     *            the unit being added.
308     * @param name
309     *            the string to use as name
310     * @param text
311     *            the string to use as label or symbol
312     * @param isLabel
313     *            if the string should be used as a label or not
314     * @return <code>unit</code>.
315     */
316    private static <U extends Unit<?>> U addUnit(U unit, String name, String text, boolean isLabel) {
317        if (isLabel) {
318            SimpleUnitFormat.getInstance().label(unit, text);
319        }
320        if (name != null && unit instanceof AbstractUnit) {
321            return Helper.addUnit(INSTANCE.units, unit, name);
322        } else {
323            INSTANCE.units.add(unit);
324        }
325        return unit;
326    }
327
328    /**
329     * Adds a new unit not mapped to any specified quantity type and puts a text
330     * as symbol or label.
331     *
332     * @param unit
333     *            the unit being added.
334     * @param name
335     *            the string to use as name
336     * @param label
337     *            the string to use as label
338     * @return <code>unit</code>.
339     */
340    private static <U extends Unit<?>> U addUnit(U unit, String name, String label) {
341        return addUnit(unit, name, label, true);
342    }
343
344    /**
345     * Adds a new unit not mapped to any specified quantity type and puts a text
346     * as symbol or label.
347     *
348     * @param unit
349     *            the unit being added.
350     * @param text
351     *            the string to use as label or symbol
352     * @param isLabel
353     *            if the string should be used as a label or not
354     * @return <code>unit</code>.
355     */
356    private static <U extends Unit<?>> U addUnit(U unit, String text, boolean isLabel) {
357        return addUnit(unit, null, text, isLabel);
358    }
359
360    /**
361     * Adds a new unit not mapped to any specified quantity type and puts a text
362     * as label.
363     *
364     * @param unit
365     *            the unit being added.
366     * @param text
367     *            the string to use as label or symbol
368     * @return <code>unit</code>.
369     */
370    private static <U extends Unit<?>> U addUnit(U unit, String text) {
371        return addUnit(unit, null, text, true);
372    }
373
374    // ///////////////////
375    // Collection View //
376    // ///////////////////
377
378    @Override
379    public String getName() {
380        return SYSTEM_NAME;
381    }
382}