001/*
002 * Units of Measurement Systems
003 * Copyright (c) 2005-2017, 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.uom.se.unit.MetricPrefix.MICRO;
033import static si.uom.SI.*;
034
035import tec.uom.se.AbstractSystemOfUnits;
036import tec.uom.se.AbstractUnit;
037import tec.uom.se.format.SimpleUnitFormat;
038import tec.uom.se.function.RationalConverter;
039import tec.uom.se.unit.ProductUnit;
040import tec.uom.se.unit.TransformedUnit;
041
042import javax.measure.Unit;
043import javax.measure.quantity.Angle;
044import javax.measure.quantity.Area;
045
046import javax.measure.quantity.Energy;
047import javax.measure.quantity.Length;
048import javax.measure.quantity.Mass;
049import javax.measure.quantity.Power;
050import javax.measure.quantity.Temperature;
051import javax.measure.quantity.Time;
052import javax.measure.quantity.Speed;
053import javax.measure.quantity.Volume;
054import javax.measure.spi.SystemOfUnits;
055
056/**
057 * <p>
058 * This class contains units from the United States customary system.
059 * </p>
060 * <p>
061 * 
062 * @noextend This class is not intended to be extended by clients.
063 * 
064 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
065 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
066 * @version 1.26, $Date: 2017-03-04 $
067 * @see <a href="http://en.wikipedia.org/wiki/United_States_customary_units">
068 *      Wikipedia: United States Customary Units</a>
069 * @see <a href=
070 *      "https://en.wikipedia.org/wiki/Imperial_and_US_customary_measurement_systems">
071 * @since 0.3
072 */
073public final class USCustomary extends AbstractSystemOfUnits {
074        private static final String SYSTEM_NAME = "United States Customary Units";
075
076        /**
077         * Default constructor (prevents this class from being instantiated).
078         */
079        private USCustomary() {
080        }
081
082        /**
083         * Returns the unique instance of this class.
084         * 
085         * @return the USCustomary instance.
086         */
087        public static SystemOfUnits getInstance() {
088                return INSTANCE;
089        }
090
091        private static final USCustomary INSTANCE = new USCustomary();
092
093        ////////////
094        // Length //
095        ////////////
096        /**
097         * US name for {@link SI#METRE}.
098         */
099        public static final Unit<Length> METER = addUnit(METRE);
100
101        /**
102         * A unit of length equal to <code>0.3048 m</code> (standard name
103         * <code>ft</code>).
104         */
105        public static final Unit<Length> FOOT = addUnit(METER.multiply(3048).divide(10000), "Foot", "ft");
106
107        /**
108         * A unit of length equal to <code>1200/3937 m</code> (standard name
109         * <code>foot_survey_us</code>). See also:
110         * <a href="http://www.sizes.com/units/foot.htm">foot</a>
111         */
112        public static final Unit<Length> FOOT_SURVEY = addUnit(METER.multiply(1200).divide(3937), "US Survey foot",
113                        "ft_survey_us");
114
115        /**
116         * A unit of length equal to <code>0.9144 m</code> (standard name
117         * <code>yd</code>).
118         */
119        public static final Unit<Length> YARD = addUnit(FOOT.multiply(3), "Yard", "yd");
120
121        /**
122         * A unit of length equal to <code>0.0254 m</code> (standard name
123         * <code>in</code>).
124         */
125        public static final Unit<Length> INCH = addUnit(FOOT.divide(12), "Inch", "in", true);
126
127        /**
128         * A unit of length equal to <code>1609.344 m</code> (standard name
129         * <code>mi</code>).
130         */
131        public static final Unit<Length> MILE = addUnit(METER.multiply(1609344).divide(1000), "Mile", "mi");
132
133        /**
134         * A unit of length equal to the distance that light travels in one year
135         * through a vacuum (standard name <code>ly</code>).
136         */
137        public static final Unit<Length> LIGHT_YEAR = addUnit(METRE.multiply(9.460528405e15), "Light year", "ly");
138
139        /**
140         * A unit of length equal to <code>1852.0 m</code> (standard name
141         * <code>nmi</code>).
142         */
143        public static final Unit<Length> NAUTICAL_MILE = addUnit(METER.multiply(1852), "Nautical mile", "nmi");
144
145        //////////
146        // Mass //
147        //////////
148
149        /**
150         * A unit of mass equal to <code>453.59237 grams</code> (avoirdupois pound,
151         * standard name <code>lb</code>).
152         */
153        public static final Unit<Mass> POUND = addUnit(KILOGRAM.multiply(45359237).divide(100000000), "Pound", "lb");
154        // Messages.US_lb_name);
155
156        /**
157         * A unit of mass equal to <code>1 / 16 {@link #POUND}</code> (standard name
158         * <code>oz</code>).
159         */
160        public static final Unit<Mass> OUNCE = addUnit(POUND.divide(16), "oz", true);
161
162        /**
163         * A unit of mass equal to <code>2000 {@link #POUND}</code> (short ton,
164         * standard name <code>ton</code>).
165         */
166        public static final Unit<Mass> TON = addUnit(POUND.multiply(2000), "ton_us");
167
168        /////////////////
169        // Temperature //
170        /////////////////
171
172        /**
173         * A unit of temperature equal to <code>5/9 °K</code> (standard name
174         * <code>°R</code>).
175         */
176        public static final Unit<Temperature> RANKINE = addUnit(KELVIN.multiply(5).divide(9));
177
178        /**
179         * A unit of temperature equal to degree Rankine minus
180         * <code>459.67 °R</code> (standard name <code>°F</code>).
181         * 
182         * @see #RANKINE
183         */
184        public static final Unit<Temperature> FAHRENHEIT = addUnit(RANKINE.shift(459.67), "°F");
185
186        ///////////
187        // Angle //
188        ///////////
189
190        /**
191         * A unit of angle equal to a full circle or <code>2<i>&pi;</i>
192         * {@link SI#RADIAN}</code> (standard name <code>rev</code>).
193         */
194        public static final Unit<Angle> REVOLUTION = addUnit(RADIAN.multiply(2).multiply(Math.PI).asType(Angle.class),
195                        "rev", true);
196
197        /**
198         * A unit of angle equal to <code>1/360 {@link #REVOLUTION}</code> (standard
199         * name <code>deg</code>).
200         */
201        public static final Unit<Angle> DEGREE_ANGLE = addUnit(REVOLUTION.divide(360));
202
203        /**
204         * A unit of angle equal to <code>1/60 {@link #DEGREE_ANGLE}</code>
205         * (standard name <code>'</code>).
206         */
207        public static final Unit<Angle> MINUTE_ANGLE = addUnit(DEGREE_ANGLE.divide(60));
208
209        /**
210         * A unit of angle equal to <code>1/60 {@link #MINUTE_ANGLE}</code>
211         * (standard name <code>"</code>).
212         */
213        public static final Unit<Angle> SECOND_ANGLE = addUnit(MINUTE_ANGLE.divide(60));
214
215        /**
216         * A unit of angle equal to <code>0.01 {@link SI#RADIAN}</code> (standard
217         * name <code>centiradian</code>).
218         */
219        public static final Unit<Angle> CENTIRADIAN = addUnit(RADIAN.divide(100));
220
221        /**
222         * A unit of angle measure equal to <code>1/400 {@link #REVOLUTION}</code>
223         * (standard name <code>grade</code> ).
224         */
225        public static final Unit<Angle> GRADE = addUnit(REVOLUTION.divide(400));
226
227        //////////////
228        // Time //
229        //////////////
230        /**
231         * A unit of time equal to <code>60 s</code> (standard name <code>min</code>
232         * ).
233         */
234        public static final Unit<Time> MINUTE = addUnit(SECOND.multiply(60));
235
236        /**
237         * A unit of duration equal to <code>60 {@link #MINUTE}</code> (standard
238         * name <code>h</code>).
239         */
240        public static final Unit<Time> HOUR = addUnit(MINUTE.multiply(60));
241
242        //////////////
243        // Speed //
244        //////////////
245        /**
246         * A unit of velocity expressing the number of {@link #FOOT feet} per
247         * {@link Units#SECOND second}.
248         */
249        public static final Unit<Speed> FOOT_PER_SECOND = addUnit(FOOT.divide(SECOND)).asType(Speed.class);
250
251        /**
252         * A unit of velocity expressing the number of international {@link #MILE
253         * miles} per {@link #HOUR hour} (abbreviation <code>mph</code>).
254         */
255        public static final Unit<Speed> MILE_PER_HOUR = addUnit(MILE.divide(HOUR).asType(Speed.class), "Mile per hour",
256                        "mph");
257
258        /**
259         * A unit of velocity expressing the number of {@link #NAUTICAL_MILE
260         * nautical miles} per {@link #HOUR hour} (abbreviation <code>kn</code>).
261         */
262        public static final Unit<Speed> KNOT = addUnit(NAUTICAL_MILE.divide(HOUR).asType(Speed.class), "Knot", "kn");
263
264        //////////
265        // Area //
266        //////////
267
268        /**
269         * A unit of area (standard name <code>sft</code> ).
270         */
271        public static final Unit<Area> SQUARE_FOOT = addUnit(new ProductUnit<Area>((AbstractUnit<?>) FOOT.multiply(FOOT)));
272
273        /**
274         * A unit of area equal to <code>100 m²</code> (standard name <code>a</code>
275         * ).
276         */
277        public static final Unit<Area> ARE = addUnit(SQUARE_METRE.multiply(100));
278
279        /**
280         * A unit of area equal to <code>100 {@link #ARE}</code> (standard name
281         * <code>ha</code>).
282         */
283        public static final Unit<Area> HECTARE = addUnit(ARE.multiply(100)); // Exact.
284
285        /**
286         * The acre is a unit of area used in the imperial and U.S. customary
287         * systems. It is equivalent to <code>43,560 square feet</code>. An acre is
288         * about 40% of a <code>HECTARE</code> – slightly smaller than an American
289         * football field. (standard name <code>ac</code> ).
290         * 
291         * @see <a href="http://en.wikipedia.org/wiki/Acre">Wikipedia: Acre</a>
292         */
293        public static final Unit<Area> ACRE = addUnit(SQUARE_FOOT.multiply(43560));
294
295        ////////////
296        // Energy //
297        ////////////
298
299        /**
300         * A unit of energy equal to one electron-volt (standard name
301         * <code>eV</code>, also recognized <code>keV, MeV, GeV</code>).
302         */
303        public static final Unit<Energy> ELECTRON_VOLT = addUnit(JOULE.multiply(1.602176462e-19), "Electron Volt", "eV");
304
305        ////////////
306        // Power //
307        ////////////
308
309        /**
310         * Horsepower (HP) is the name of several units of measurement of power. The
311         * most common definitions equal between 735.5 and 750 watts. Horsepower was
312         * originally defined to compare the output of steam engines with the power
313         * of draft horses. The unit was widely adopted to measure the output of
314         * piston engines, turbines, electric motors, and other machinery. The
315         * definition of the unit varied between geographical regions. Most
316         * countries now use the SI unit watt for measurement of power. With the
317         * implementation of the EU Directive 80/181/EEC on January 1, 2010, the use
318         * of horsepower in the EU is only permitted as supplementary unit.
319         */
320        public static final Unit<Power> HORSEPOWER = addUnit(WATT.multiply(735.499), "Horsepower", "HP");
321
322        ////////////
323        // Volume //
324        ////////////
325        /**
326         * A unit of volume equal to one cubic decimeter (default label
327         * <code>L</code>, also recognized <code>µL, mL, cL, dL</code>).
328         */
329        public static final TransformedUnit<Volume> LITER = addUnit(
330                        new TransformedUnit<Volume>(CUBIC_METRE, new RationalConverter(1, 1000)));
331
332        /**
333         * A unit of volume equal to one cubic inch (<code>in³</code>).
334         */
335        public static final Unit<Volume> CUBIC_INCH = addUnit(INCH.pow(3).asType(Volume.class));
336
337        /**
338         * A unit of volume equal to one US dry gallon. (standard name
339         * <code>gallon_dry_us</code>).
340         */
341        public static final Unit<Volume> GALLON_DRY = addUnit(CUBIC_INCH.multiply(2688025).divide(10000));
342
343        /**
344         * The cubic foot is an imperial and US customary (non-metric) unit of
345         * volume, used in the United States, Canada, and the United Kingdom. It is
346         * defined as the volume of a cube with sides of one foot (0.3048 m) in
347         * length. Its volume is 28.3168 liters or about 1⁄35 of a cubic meter. (
348         * <code>ft³</code>).
349         */
350        public static final Unit<Volume> CUBIC_FOOT = addUnit(CUBIC_INCH.multiply(1728), "ft³", true);
351
352        /**
353         * An acre-foot is a unit of volume commonly used in the United States in
354         * reference to large-scale water resources, such as reservoirs, aqueducts,
355         * canals, sewer flow capacity, irrigation water, and river flows.
356         */
357        public static final Unit<Volume> ACRE_FOOT = addUnit(CUBIC_FOOT.multiply(43560), "Acre-foot", "ac ft");
358
359        /**
360         * A unit of volume equal to one US gallon, Liquid Unit. The U.S. liquid
361         * gallon is based on the Queen Anne or Wine gallon occupying 231 cubic
362         * inches (standard name <code>gal</code>).
363         */
364        public static final Unit<Volume> GALLON_LIQUID = addUnit(CUBIC_INCH.multiply(231));
365
366        /**
367         * A unit of volume equal to <code>1 / 128 {@link #GALLON_LIQUID}</code>
368         * (standard name <code>oz_fl</code>).
369         */
370        public static final Unit<Volume> FLUID_OUNCE = addUnit(GALLON_LIQUID.divide(128));
371
372        /**
373         * A unit of volume equal to 4 US oz_fl (standard name <code>liq.gi</code>).
374         */
375        public static final Unit<Volume> GILL_LIQUID = addUnit(FLUID_OUNCE.multiply(4), "Liquid Gill", "liq.gi");
376
377        /**
378         * A unit of volume <code>~ 1 drop or 0.95 grain of water </code> (standard
379         * name <code>min</code>).
380         */
381        public static final Unit<Volume> MINIM = addUnit(MICRO(LITER).multiply(61.61152d));
382
383        /**
384         * A unit of volume equal to <code>60 {@link #MINIM}</code> (standard name
385         * <code>fl dr</code>).
386         */
387        public static final Unit<Volume> FLUID_DRAM = addUnit(MINIM.multiply(60));
388
389        /**
390         * The cup is a unit of measurement for volume, used in cooking to measure
391         * liquids (fluid measurement) and bulk foods such as granulated sugar (dry
392         * measurement). A cup is equal to <code>8 {@link #FLUID_OUNCE}</code>
393         * (standard name <code>cup</code>).
394         */
395        public static final Unit<Volume> CUP = addUnit(FLUID_OUNCE.multiply(8));
396
397        /**
398         * A unit of volume equal to <code>80 {@link #MINIM}</code> (standard name
399         * <code>tsp</code>).
400         */
401        public static final Unit<Volume> TEASPOON = addUnit(MINIM.multiply(80));
402
403        /**
404         * A unit of volume equal to <code>3 {@link #TEASPOON}</code> (standard name
405         * <code>Tbsp</code>).
406         */
407        public static final Unit<Volume> TABLESPOON = addUnit(TEASPOON.multiply(3));
408
409        /**
410         * A unit of volume equal to <code>238.4810 {@link #LITER}</code> (standard
411         * name <code>bbl</code>).
412         */
413        public static final Unit<Volume> BARREL = addUnit(LITER.multiply(238.4810d), "Barrel", "bbl");
414
415        /**
416         * A unit of volume equal to <code>4 {@link #GILL_LIQUID}</code> (standard
417         * name <code>pt</code>).
418         */
419        public static final Unit<Volume> PINT = addUnit(GILL_LIQUID.multiply(4), "Pint", "pt");
420
421        /**
422         * Holds the international foot: 0.3048 m exact.
423         */
424        // private static final int INTERNATIONAL_FOOT_DIVIDEND = 3048;
425
426        // private static final int INTERNATIONAL_FOOT_DIViSOR = 10000;
427
428        /**
429         * Adds a new unit to the collection.
430         * 
431         * @param unit
432         *            the unit being added.
433         * @return <code>unit</code>.
434         */
435        protected static <U extends Unit<?>> U addUnit(U unit) {
436                INSTANCE.units.add(unit);
437                return unit;
438        }
439
440        @Override
441        public String getName() {
442                return SYSTEM_NAME;
443        }
444
445        /**
446         * Adds a new unit not mapped to any specified quantity type and puts a text
447         * as symbol or label.
448         *
449         * @param unit
450         *            the unit being added.
451         * @param name
452         *            the string to use as name
453         * @param text
454         *            the string to use as label or symbol
455         * @param isLabel
456         *            if the string should be used as a label or not
457         * @return <code>unit</code>.
458         */
459        private static <U extends Unit<?>> U addUnit(U unit, String name, String text, boolean isLabel) {
460                if (isLabel) {
461                        SimpleUnitFormat.getInstance().label(unit, text);
462                }
463                if (name != null && unit instanceof AbstractUnit) {
464                        return Helper.addUnit(INSTANCE.units, unit, name);
465                } else {
466                        INSTANCE.units.add(unit);
467                }
468                return unit;
469        }
470
471        /**
472         * Adds a new unit not mapped to any specified quantity type and puts a text
473         * as symbol or label.
474         *
475         * @param unit
476         *            the unit being added.
477         * @param name
478         *            the string to use as name
479         * @param label
480         *            the string to use as label
481         * @return <code>unit</code>.
482         */
483        private static <U extends Unit<?>> U addUnit(U unit, String name, String label) {
484                return addUnit(unit, name, label, true);
485        }
486
487        /**
488         * Adds a new unit not mapped to any specified quantity type and puts a text
489         * as symbol or label.
490         *
491         * @param unit
492         *            the unit being added.
493         * @param text
494         *            the string to use as label or symbol
495         * @param isLabel
496         *            if the string should be used as a label or not
497         * @return <code>unit</code>.
498         */
499        private static <U extends Unit<?>> U addUnit(U unit, String text, boolean isLabel) {
500                return addUnit(unit, null, text, isLabel);
501        }
502        
503        /**
504         * Adds a new unit not mapped to any specified quantity type and puts a text
505         * as label.
506         *
507         * @param unit
508         *            the unit being added.
509         * @param text
510         *            the string to use as label or symbol
511         * @return <code>unit</code>.
512         */
513        private static <U extends Unit<?>> U addUnit(U unit, String text) {
514                return addUnit(unit, null, text, true);
515        }
516}