001/*
002 *  Unit-API - Units of Measurement API for Java
003 *  Copyright (c) 2005-2016, 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 nor the names of its 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 tec.uom.se.unit.Units.*;
030import static systems.uom.common.NonSI.AVOIRDUPOIS_POUND_DIVIDEND;
031import static systems.uom.common.NonSI.AVOIRDUPOIS_POUND_DIVISOR;
032
033import javax.measure.Unit;
034import javax.measure.quantity.Area;
035import javax.measure.quantity.Length;
036import javax.measure.quantity.Mass;
037import javax.measure.quantity.Temperature;
038import javax.measure.quantity.Time;
039import javax.measure.quantity.Volume;
040import javax.measure.spi.SystemOfUnits;
041
042import tec.uom.se.AbstractSystemOfUnits;
043import tec.uom.se.format.SimpleUnitFormat;
044import tec.uom.se.unit.ProductUnit;
045
046/**
047 * <p>
048 * This class contains units from the Imperial system.
049 * </p>
050 * <p>
051 * 
052 * @noextend This class is not intended to be extended by clients.
053 * 
054 * @author <a href="mailto:units@catmedia.us">Werner Keil</a>
055 * @version 0.5, $Date: 2016-03-20 $
056 * @see <a
057 *      href="http://en.wikipedia.org/wiki/http://en.wikipedia.org/wiki/Imperial_unit">Wikipedia:
058 *      Imperial Units</a>
059 */
060public final class Imperial extends AbstractSystemOfUnits  {
061
062        /**
063         * Default constructor (prevents this class from being instantiated).
064         */
065        private Imperial() {
066        }
067
068        /**
069         * Returns the unique instance of this class.
070         * 
071         * @return the Imperial instance.
072         */
073        public static SystemOfUnits getInstance() {
074                return INSTANCE;
075        }
076
077        private static final Imperial INSTANCE = new Imperial();
078
079        // //////////
080        // Length //
081        // //////////
082
083        /**
084         * A unit of length equal to <code>0.0254 m</code> (standard name
085         * <code>in</code>).
086         */
087        public static final Unit<Length> INCH = USCustomary.INCH;
088        
089        // ////////
090        // Mass //
091        // ////////
092
093        /**
094         * A unit of mass equal to <code>453.59237 grams</code> (avoirdupois pound,
095         * standard name <code>lb</code>).
096         */
097        static final Unit<Mass> POUND = addUnit(KILOGRAM.multiply(
098                        AVOIRDUPOIS_POUND_DIVIDEND).divide(AVOIRDUPOIS_POUND_DIVISOR));
099        /**
100         * An English and imperial unit of weight or mass now equal to 14 avoirdupois pounds or 6.35029318 kg (<code>st</code>).
101         */
102        public static final Unit<Mass> STONE = addUnit(KILOGRAM.multiply(6.35029318));
103
104        /**
105         * A unit of mass equal to <code>1 / 16 {@link #POUND}</code> (standard name
106         * <code>oz</code>).
107         */
108        public static final Unit<Mass> OUNCE = addUnit(POUND.divide(16));
109        
110        /**
111         * A unit of mass equal to <code>2240 {@link #POUND}</code> (long ton,
112         * standard name <code>ton_uk</code>).
113         */
114        public static final Unit<Mass> TON_UK = addUnit(POUND.multiply(2240));
115
116        /**
117         * A unit of mass equal to <code>1000 kg</code> (metric ton, standard name
118         * <code>t</code>).
119         */
120        public static final Unit<Mass> METRIC_TON = addUnit(KILOGRAM.multiply(1000));
121
122        // ///////////////
123        // Temperature //
124        // ///////////////
125
126        /**
127         * A unit of temperature equal to <code>5/9 °K</code> (standard name
128         * <code>°R</code>).
129         */
130        static final Unit<Temperature> RANKINE = addUnit(KELVIN.multiply(5)
131                        .divide(9));
132
133        /**
134         * A unit of temperature equal to degree Rankine minus
135         * <code>459.67 °R</code> (standard name <code>°F</code>).
136         * 
137         * @see #RANKINE
138         */
139        static final Unit<Temperature> FAHRENHEIT = addUnit(RANKINE
140                        .shift(459.67));
141
142        // /////////
143        // Angle //
144        // /////////
145
146        
147        // ////////////
148        // TimeUnit //
149        // ////////////
150        /**
151         * A unit of time equal to <code>60 s</code> (standard name <code>min</code>
152         * ).
153         */
154        static final Unit<Time> MINUTE = addUnit(SECOND.multiply(60));
155
156        /**
157         * A unit of duration equal to <code>60 {@link #MINUTE}</code> (standard
158         * name <code>h</code>).
159         */
160        static final Unit<Time> HOUR = addUnit(MINUTE.multiply(60));
161
162        // ////////////
163        // Velocity //
164        // ////////////
165
166
167        // ////////
168        // Area //
169        // ////////
170
171        /**
172         * A unit of area (standard name <code>sft</code>
173         * ).
174         */
175        public static final Unit<Area> SQUARE_FOOT = USCustomary.SQUARE_FOOT;
176        
177        /**
178         * One acre is 43,560 <code>square feet</code> (standard name <code>a</code>
179         * ).
180         */
181        public static final Unit<Area> ACRE = addUnit(USCustomary.SQUARE_FOOT.multiply(43560));
182
183        // //////////
184        // Energy //
185        // //////////
186
187
188        // //////////
189        // Volume //
190        // //////////
191        /**
192         * A unit of volume equal to one cubic decimeter (default label
193         * <code>L</code>, also recognized <code>µL, mL, cL, dL</code>).
194         */
195        static final Unit<Volume> LITRE = addUnit(CUBIC_METRE.divide(1000));
196
197        /**
198         * A unit of volume equal to one cubic inch (<code>in³</code>).
199         */
200        static final Unit<Volume> CUBIC_INCH = addUnit(new ProductUnit<Volume>(USCustomary.INCH.pow(3))); //, "in³"));
201
202        /**
203         * A unit of volume equal to <code>4.546 09 {@link #LITRE}</code> (standard
204         * name <code>gal_uk</code>).
205         */
206        public static final Unit<Volume> GALLON_UK = addUnit(LITRE.multiply(454609)
207                        .divide(100000));
208
209        /**
210         * A unit of volume equal to one UK gallon, Liquid Unit.
211         */
212        public static final Unit<Volume> GALLON_LIQUID = addUnit(CUBIC_INCH
213                        .multiply(277.42));
214        
215        /**
216         * A unit of volume equal to <code>1 / 160 {@link #GALLON_UK}</code>
217         * (standard name <code>oz_fl_uk</code>).
218         */
219        static final Unit<Volume> OUNCE_LIQUID_UK = addUnit(GALLON_UK
220                        .divide(160));
221        
222        /**
223         * A unit of volume equal to <code>1 / 160 {@link #GALLON_LIQUID}</code>
224         * (standard name <code>oz_fl</code>).
225         */
226        public static final Unit<Volume> OUNCE_LIQUID = OUNCE_LIQUID_UK;
227
228        /**
229         * A unit of volume equal to <code>5 {@link #OUNCE_LIQUID}</code> (standard name
230         * <code>gi</code>).
231         */
232        public static final Unit<Volume> GILL = addUnit(OUNCE_LIQUID.multiply(5));
233
234        /**
235         * A unit of volume equal to <code>20 {@link #OUNCE_LIQUID}</code> (standard name
236         * <code>pt</code>).
237         */
238        public static final Unit<Volume> PINT = addUnit(OUNCE_LIQUID.multiply(20));
239
240        /**
241         * A unit of volume equal to <code>40 {@link #OUNCE_LIQUID}</code> (standard name
242         * <code>qt</code>).
243         */
244        public static final Unit<Volume> QUART = addUnit(OUNCE_LIQUID.multiply(40));
245        
246        /**
247         * A unit of volume <code>~ 1 drop or 0.95 grain of water </code> (standard
248         * name <code>min</code>).
249         */
250        public static final Unit<Volume> MINIM = addUnit(MICRO(LITRE).multiply(
251                        59.1938802d));
252
253        /**
254         * A unit of volume equal to <code>20 {@link #MINIM}</code> (standard name
255         * <code>fl scr</code>).
256         */
257        public static final Unit<Volume> FLUID_SCRUPLE = addUnit(MINIM.multiply(60));
258
259        /**
260         * A unit of volume equal to <code>3 {@link #FLUID_SCRUPLE}</code> (standard name
261         * <code>fl drc</code>).
262         */
263        public static final Unit<Volume> FLUID_DRACHM = addUnit(FLUID_SCRUPLE.multiply(3));
264
265        /**
266     * Adds a new unit not mapped to any specified quantity type.
267     *
268     * @param  unit the unit being added.
269     * @return <code>unit</code>.
270     */
271    private static <U extends Unit<?>>  U addUnit(U unit) {
272        INSTANCE.units.add(unit);
273        return unit;
274    }
275    
276        // //////////////////////////////////////////////////////////////////////////
277        // Label adjustments for Imperial system
278        static {
279                SimpleUnitFormat.getInstance().label(POUND, "lb");
280                SimpleUnitFormat.getInstance().label(FLUID_DRACHM, "fl drc");
281                SimpleUnitFormat.getInstance().label(FLUID_SCRUPLE, "fl scr");
282        }
283    
284        // ///////////////////
285        // Collection View //
286        // ///////////////////
287
288        @Override
289        public String getName() {
290                return getClass().getSimpleName();
291        }
292}