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