001/* 002 * Units of Measurement Reference Implementation 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, 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 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 tec.units.ri.unit; 031 032import javax.measure.Quantity; 033import javax.measure.Unit; 034import javax.measure.quantity.Acceleration; 035import javax.measure.quantity.AmountOfSubstance; 036import javax.measure.quantity.Angle; 037import javax.measure.quantity.Area; 038import javax.measure.quantity.CatalyticActivity; 039import javax.measure.quantity.Dimensionless; 040import javax.measure.quantity.ElectricCapacitance; 041import javax.measure.quantity.ElectricCharge; 042import javax.measure.quantity.ElectricConductance; 043import javax.measure.quantity.ElectricCurrent; 044import javax.measure.quantity.ElectricInductance; 045import javax.measure.quantity.ElectricPotential; 046import javax.measure.quantity.ElectricResistance; 047import javax.measure.quantity.Energy; 048import javax.measure.quantity.Force; 049import javax.measure.quantity.Frequency; 050import javax.measure.quantity.Illuminance; 051import javax.measure.quantity.Length; 052import javax.measure.quantity.LuminousFlux; 053import javax.measure.quantity.LuminousIntensity; 054import javax.measure.quantity.MagneticFlux; 055import javax.measure.quantity.MagneticFluxDensity; 056import javax.measure.quantity.Mass; 057import javax.measure.quantity.Power; 058import javax.measure.quantity.Pressure; 059import javax.measure.quantity.RadiationDoseAbsorbed; 060import javax.measure.quantity.RadiationDoseEffective; 061import javax.measure.quantity.Radioactivity; 062import javax.measure.quantity.SolidAngle; 063import javax.measure.quantity.Speed; 064import javax.measure.quantity.Temperature; 065import javax.measure.quantity.Time; 066import javax.measure.quantity.Volume; 067import javax.measure.spi.SystemOfUnits; 068 069import tec.units.ri.AbstractSystemOfUnits; 070import tec.units.ri.AbstractUnit; 071import tec.units.ri.function.AddConverter; 072import tec.units.ri.function.RationalConverter; 073import tec.units.ri.quantity.QuantityDimension; 074import tec.uom.lib.common.function.Nameable; 075 076/** 077 * <p> 078 * This class defines commonly used units. 079 * 080 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 081 * @version 0.8.4, March 16, 2016 082 */ 083public class Units extends AbstractSystemOfUnits implements Nameable { 084 085 public static final String SYSTEM_NAME = "Units"; // This is for ME 086 087 // compatibility, since 088 // Class.getSimpleName() 089 // isn't available. 090 091 protected Units() { 092 } 093 094 private static final Units INSTANCE = new Units(); 095 096 /** 097 * Holds the dimensionless unit <code>ONE</code>. 098 * 099 * @deprecated consider moving it to {@link AbstractUnit} again for static constant usage reasons. 100 */ 101 static final Unit<Dimensionless> ONE = addUnit(new ProductUnit<Dimensionless>(), Dimensionless.class); 102 103 // ////////////// 104 // BASE UNITS // 105 // ////////////// 106 107 /** 108 * The SI base unit for electric current quantities (standard name <code>A</code>). The Ampere is that constant current which, if maintained in two 109 * straight parallel conductors of infinite length, of negligible circular cross-section, and placed 1 meter apart in vacuum, would produce between 110 * these conductors a force equal to 2 * 10-7 newton per meter of length. It is named after the French physicist Andre Ampere (1775-1836). 111 */ 112 public static final Unit<ElectricCurrent> AMPERE = addUnit(new BaseUnit<ElectricCurrent>("A", QuantityDimension.ELECTRIC_CURRENT), 113 ElectricCurrent.class); 114 115 /** 116 * The SI base unit for luminous intensity quantities (standard name <code>cd</code>). The candela is the luminous intensity, in a given direction, 117 * of a source that emits monochromatic radiation of frequency 540 * 1012 hertz and that has a radiant intensity in that direction of 1/683 watt per 118 * steradian 119 * 120 * @see <a href="http://en.wikipedia.org/wiki/Candela"> Wikipedia: Candela</a> 121 */ 122 public static final Unit<LuminousIntensity> CANDELA = addUnit(new BaseUnit<LuminousIntensity>("cd", QuantityDimension.LUMINOUS_INTENSITY), 123 LuminousIntensity.class); 124 125 /** 126 * The SI base unit for thermodynamic temperature quantities (standard name <code>K</code>). The kelvin is the 1/273.16th of the thermodynamic 127 * temperature of the triple point of water. It is named after the Scottish mathematician and physicist William Thomson 1st Lord Kelvin (1824-1907) 128 */ 129 public static final Unit<Temperature> KELVIN = addUnit(new BaseUnit<Temperature>("K", QuantityDimension.TEMPERATURE), Temperature.class); 130 131 /** 132 * The SI base unit for mass quantities (standard name <code>kg</code>). It is the only SI unit with a prefix as part of its name and symbol. The 133 * kilogram is equal to the mass of an international prototype in the form of a platinum-iridium cylinder kept at Sevres in France. 134 * 135 * @see #GRAM 136 */ 137 public static final Unit<Mass> KILOGRAM = addUnit(new BaseUnit<Mass>("kg", QuantityDimension.MASS), Mass.class); 138 139 /** 140 * The SI base unit for length quantities (standard name <code>m</code>). One metre was redefined in 1983 as the distance traveled by light in a 141 * vacuum in 1/299,792,458 of a second. 142 */ 143 public static final Unit<Length> METRE = addUnit(new BaseUnit<Length>("m", QuantityDimension.LENGTH), Length.class); 144 145 /** 146 * The SI base unit for amount of substance quantities (standard name <code>mol</code>). The mole is the amount of substance of a system which 147 * contains as many elementary entities as there are atoms in 0.012 kilogram of carbon 12. 148 */ 149 public static final Unit<AmountOfSubstance> MOLE = addUnit(new BaseUnit<AmountOfSubstance>("mol", QuantityDimension.AMOUNT_OF_SUBSTANCE), 150 AmountOfSubstance.class); 151 152 /** 153 * The SI base unit for duration quantities (standard name <code>s</code>). It is defined as the duration of 9,192,631,770 cycles of radiation 154 * corresponding to the transition between two hyperfine levels of the ground state of cesium (1967 Standard). 155 */ 156 public static final Unit<Time> SECOND = addUnit(new BaseUnit<Time>("s", QuantityDimension.TIME), Time.class); 157 158 // ////////////////////////////// 159 // SI DERIVED ALTERNATE UNITS // 160 // ////////////////////////////// 161 162 /** 163 * The SI derived unit for mass quantities (standard name <code>g</code>). The base unit for mass quantity is {@link #KILOGRAM}. 164 */ 165 public static final Unit<Mass> GRAM = addUnit(KILOGRAM.divide(1000)); 166 // addUnit(new AlternateUnit<Mass>( 167 // KILOGRAM.divide(1000), "g"), Mass.class); 168 169 /** 170 * The SI unit for plane angle quantities (standard name <code>rad</code>). One radian is the angle between two radii of a circle such that the 171 * length of the arc between them is equal to the radius. 172 */ 173 public static final Unit<Angle> RADIAN = addUnit(new AlternateUnit<Angle>(AbstractUnit.ONE, "rad"), Angle.class); 174 175 /** 176 * The SI unit for solid angle quantities (standard name <code>sr</code>). One steradian is the solid angle subtended at the center of a sphere by 177 * an area on the surface of the sphere that is equal to the radius squared. The total solid angle of a sphere is 4*Pi steradians. 178 */ 179 public static final Unit<SolidAngle> STERADIAN = addUnit(new AlternateUnit<SolidAngle>(AbstractUnit.ONE, "sr"), SolidAngle.class); 180 181 /** 182 * The SI unit for frequency (standard name <code>Hz</code>). A unit of frequency equal to one cycle per second. After Heinrich Rudolf Hertz 183 * (1857-1894), German physicist who was the first to produce radio waves artificially. 184 */ 185 public static final Unit<Frequency> HERTZ = addUnit(new AlternateUnit<Frequency>(AbstractUnit.ONE.divide(SECOND), "Hz"), Frequency.class); 186 187 /** 188 * The SI unit for force (standard name <code>N</code>). One newton is the force required to give a mass of 1 kilogram an Force of 1 metre per 189 * second per second. It is named after the English mathematician and physicist Sir Isaac Newton (1642-1727). 190 */ 191 public static final Unit<Force> NEWTON = addUnit(new AlternateUnit<Force>(METRE.multiply(KILOGRAM).divide(SECOND.pow(2)), "N"), Force.class); 192 193 /** 194 * The SI unit for pressure, stress (standard name <code>Pa</code>). One pascal is equal to one newton per square meter. It is named after the 195 * French philosopher and mathematician Blaise Pascal (1623-1662). 196 */ 197 public static final Unit<Pressure> PASCAL = addUnit(new AlternateUnit<Pressure>(NEWTON.divide(METRE.pow(2)), "Pa"), Pressure.class); 198 199 /** 200 * The SI unit for energy, work, quantity of heat (<code>J</code>). One joule is the amount of work done when an applied force of 1 newton moves 201 * through a distance of 1 metre in the direction of the force. It is named after the English physicist James Prescott Joule (1818-1889). 202 */ 203 public static final Unit<Energy> JOULE = addUnit(new AlternateUnit<Energy>(NEWTON.multiply(METRE), "J"), Energy.class); 204 205 /** 206 * The SI unit for power, radiant, flux (standard name <code>W</code>). One watt is equal to one joule per second. It is named after the British 207 * scientist James Watt (1736-1819). 208 */ 209 public static final Unit<Power> WATT = addUnit(new AlternateUnit<Power>(JOULE.divide(SECOND), "W"), Power.class); 210 211 /** 212 * The SI unit for electric charge, quantity of electricity (standard name <code>C</code>). One Coulomb is equal to the quantity of charge 213 * transferred in one second by a steady current of one ampere. It is named after the French physicist Charles Augustin de Coulomb (1736-1806). 214 */ 215 public static final Unit<ElectricCharge> COULOMB = addUnit(new AlternateUnit<ElectricCharge>(SECOND.multiply(AMPERE), "C"), ElectricCharge.class); 216 217 /** 218 * The SI unit for electric potential difference, electromotive force (standard name <code>V</code>). One Volt is equal to the difference of 219 * electric potential between two points on a conducting wire carrying a constant current of one ampere when the power dissipated between the points 220 * is one watt. It is named after the Italian physicist Count Alessandro Volta (1745-1827). 221 */ 222 public static final Unit<ElectricPotential> VOLT = addUnit(new AlternateUnit<ElectricPotential>(WATT.divide(AMPERE), "V"), ElectricPotential.class); 223 224 /** 225 * The SI unit for capacitance (standard name <code>F</code>). One Farad is equal to the capacitance of a capacitor having an equal and opposite 226 * charge of 1 coulomb on each plate and a potential difference of 1 volt between the plates. It is named after the British physicist and chemist 227 * Michael Faraday (1791-1867). 228 */ 229 public static final Unit<ElectricCapacitance> FARAD = addUnit(new AlternateUnit<ElectricCapacitance>(COULOMB.divide(VOLT), "F"), 230 ElectricCapacitance.class); 231 232 /** 233 * The SI unit for electric resistance (standard name <code>Ohm</code>). One Ohm is equal to the resistance of a conductor in which a current of one 234 * ampere is produced by a potential of one volt across its terminals. It is named after the German physicist Georg Simon Ohm (1789-1854). 235 */ 236 public static final Unit<ElectricResistance> OHM = addUnit(new AlternateUnit<ElectricResistance>(VOLT.divide(AMPERE), "?"), 237 ElectricResistance.class); 238 239 /** 240 * The SI unit for electric conductance (standard name <code>S</code>). One Siemens is equal to one ampere per volt. It is named after the German 241 * engineer Ernst Werner von Siemens (1816-1892). 242 */ 243 public static final Unit<ElectricConductance> SIEMENS = addUnit(new AlternateUnit<ElectricConductance>(AMPERE.divide(VOLT), "S"), 244 ElectricConductance.class); 245 246 /** 247 * The SI unit for magnetic flux (standard name <code>Wb</code>). One Weber is equal to the magnetic flux that in linking a circuit of one turn 248 * produces in it an electromotive force of one volt as it is uniformly reduced to zero within one second. It is named after the German physicist 249 * Wilhelm Eduard Weber (1804-1891). 250 */ 251 public static final Unit<MagneticFlux> WEBER = addUnit(new AlternateUnit<MagneticFlux>(VOLT.multiply(SECOND), "Wb"), MagneticFlux.class); 252 253 /** 254 * The alternate unit for magnetic flux density (standard name <code>T</code>). One Tesla is equal equal to one weber per square metre. It is named 255 * after the Serbian-born American electrical engineer and physicist Nikola Tesla (1856-1943). 256 */ 257 public static final Unit<MagneticFluxDensity> TESLA = addUnit(new AlternateUnit<MagneticFluxDensity>(WEBER.divide(METRE.pow(2)), "T"), 258 MagneticFluxDensity.class); 259 260 /** 261 * The alternate unit for inductance (standard name <code>H</code>). One Henry is equal to the inductance for which an induced electromotive force 262 * of one volt is produced when the current is varied at the rate of one ampere per second. It is named after the American physicist Joseph Henry 263 * (1791-1878). 264 */ 265 public static final Unit<ElectricInductance> HENRY = addUnit(new AlternateUnit<ElectricInductance>(WEBER.divide(AMPERE), "H"), 266 ElectricInductance.class); 267 268 /** 269 * The SI unit for Celsius temperature (standard name <code>Cel</code>). This is a unit of temperature such as the freezing point of water (at one 270 * atmosphere of pressure) is 0 Cel, while the boiling point is 100 Cel. 271 */ 272 public static final Unit<Temperature> CELSIUS = addUnit(new TransformedUnit<Temperature>(KELVIN, new AddConverter(273.15))); 273 // Not mapping to Temperature since temperature is mapped to Kelvin. 274 275 /** 276 * The SI unit for activity of a radionuclide (standard name <code>Bq</code> ). One becquerel is the radiation caused by one disintegration per 277 * second. It is named after the French physicist, Antoine-Henri Becquerel (1852-1908). 278 */ 279 public static final Unit<Radioactivity> BECQUEREL = addUnit(new AlternateUnit<Radioactivity>(AbstractUnit.ONE.divide(SECOND), "Bq"), 280 Radioactivity.class); 281 282 /** 283 * The SI unit for absorbed dose, specific energy (imparted), kerma (standard name <code>Gy</code>). One gray is equal to the dose of one joule of 284 * energy absorbed per one kilogram of matter. It is named after the British physician L. H. Gray (1905-1965). 285 */ 286 public static final Unit<RadiationDoseAbsorbed> GRAY = addUnit(new AlternateUnit<RadiationDoseAbsorbed>(JOULE.divide(KILOGRAM), "Gy"), 287 RadiationDoseAbsorbed.class); 288 289 /** 290 * The SI unit for dose equivalent (standard name <code>Sv</code>). One Sievert is equal is equal to the actual dose, in grays, multiplied by a 291 * "quality factor" which is larger for more dangerous forms of radiation. It is named after the Swedish physicist Rolf Sievert (1898-1966). 292 */ 293 public static final Unit<RadiationDoseEffective> SIEVERT = addUnit(new AlternateUnit<RadiationDoseEffective>(JOULE.divide(KILOGRAM), "Sv"), 294 RadiationDoseEffective.class); 295 296 /** 297 * The SI unit for catalytic activity (standard name <code>kat</code>). 298 */ 299 public static final Unit<CatalyticActivity> KATAL = addUnit(new AlternateUnit<CatalyticActivity>(MOLE.divide(SECOND), "kat"), 300 CatalyticActivity.class); 301 302 // //////////////////////////// 303 // SI DERIVED PRODUCT UNITS // 304 // //////////////////////////// 305 306 /** 307 * The SI unit for speed quantities (standard name <code>m/s</code>). 308 */ 309 public static final Unit<Speed> METRES_PER_SECOND = addUnit(new ProductUnit<Speed>(METRE.divide(SECOND)), Speed.class); 310 311 /** 312 * The SI unit for acceleration quantities (standard name <code>m/s2</code> ). 313 */ 314 public static final Unit<Acceleration> METRES_PER_SQUARE_SECOND = addUnit(new ProductUnit<Acceleration>(METRES_PER_SECOND.divide(SECOND)), 315 Acceleration.class); 316 317 /** 318 * The SI unit for area quantities (standard name <code>m2</code>). 319 */ 320 public static final Unit<Area> SQUARE_METRE = addUnit(new ProductUnit<Area>(METRE.multiply(METRE)), Area.class); 321 322 /** 323 * The SI unit for volume quantities (standard name <code>m3</code>). 324 */ 325 public static final Unit<Volume> CUBIC_METRE = addUnit(new ProductUnit<Volume>(SQUARE_METRE.multiply(METRE)), Volume.class); 326 327 /** 328 * A unit of velocity expressing the number of international {@link #KILOMETRE kilometres} per {@link #HOUR hour} (abbreviation <code>kph</code>). 329 */ 330 public static final Unit<Speed> KILOMETRES_PER_HOUR = addUnit(METRES_PER_SECOND.multiply(0.277778d)).asType(Speed.class); 331 332 /** 333 * The SI unit for luminous flux (standard name <code>lm</code>). One Lumen is equal to the amount of light given out through a solid angle by a 334 * source of one candela intensity radiating equally in all directions. 335 */ 336 public static final Unit<LuminousFlux> LUMEN = addUnit(new AlternateUnit<LuminousFlux>(CANDELA.multiply(STERADIAN), "lm"), LuminousFlux.class); 337 338 /** 339 * The SI unit for illuminance (standard name <code>lx</code>). One Lux is equal to one lumen per square metre. 340 */ 341 public static final Unit<Illuminance> LUX = addUnit(new AlternateUnit<Illuminance>(LUMEN.divide(METRE.pow(2)), "lx"), Illuminance.class); 342 343 // /////////////////////////////////////////////////////////////// 344 // Units outside the SI that are accepted for use with the SI. // 345 // /////////////////////////////////////////////////////////////// 346 347 /** 348 * A dimensionless unit accepted for use with SI units (standard name <code>%</code>). 349 */ 350 public static final Unit<Dimensionless> PERCENT = new TransformedUnit<Dimensionless>(AbstractUnit.ONE, new RationalConverter(1, 100)); 351 352 /** 353 * A volume unit accepted for use with SI units (standard name <code>l</code>). 354 * 355 * @see <a href="https://en.wikipedia.org/wiki/Litre"> Wikipedia: Litre</a> 356 */ 357 public static final Unit<Volume> LITRE = AbstractSystemOfUnits.Helper.addUnit(INSTANCE.units, new TransformedUnit<Volume>(CUBIC_METRE, 358 new RationalConverter(1, 1000)), "Litre", "l"); 359 360 // //////// 361 // Time // 362 // //////// 363 364 /** 365 * A time unit accepted for use with SI units (standard name <code>min</code>). 366 */ 367 public static final Unit<Time> MINUTE = new TransformedUnit<Time>("min", SECOND, RationalConverter.of(60, 1)); 368 369 /** 370 * A time unit accepted for use with SI units (standard name <code>h</code> ). 371 */ 372 public static final Unit<Time> HOUR = new TransformedUnit<Time>("h", SECOND, RationalConverter.of(60 * 60, 1)); 373 374 /** 375 * A time unit accepted for use with SI units (standard name <code>d</code> ). 376 */ 377 public static final Unit<Time> DAY = new TransformedUnit<Time>("d", SECOND, RationalConverter.of(24 * 60 * 60, 1)); 378 379 /** 380 * A unit of duration equal to 7 {@link #DAY} (standard name <code>week</code>). 381 */ 382 public static final Unit<Time> WEEK = addUnit(DAY.multiply(7)); 383 384 /** 385 * A time unit accepted for use with SI units (standard name <code>y</code> ). 386 */ 387 public static final Unit<Time> YEAR = addUnit(Units.DAY.multiply(365.2525)); 388 389 // using Gregorian year instead of Julian (365.25) 390 391 // /////////////////// 392 // Collection View // 393 // /////////////////// 394 395 @Override 396 public String getName() { 397 return SYSTEM_NAME; 398 } 399 400 /** 401 * Returns the unique instance of this class. 402 * 403 * @return the Imperial instance. 404 */ 405 public static SystemOfUnits getInstance() { 406 return INSTANCE; 407 } 408 409 /** 410 * Adds a new unit not mapped to any specified quantity type. 411 * 412 * @param unit 413 * the unit being added. 414 * @return <code>unit</code>. 415 */ 416 private static <U extends Unit<?>> U addUnit(U unit) { 417 INSTANCE.units.add(unit); 418 return unit; 419 } 420 421 /** 422 * Adds a new unit and maps it to the specified quantity type. 423 * 424 * @param unit 425 * the unit being added. 426 * @param type 427 * the quantity type. 428 * @return <code>unit</code>. 429 */ 430 private static <U extends AbstractUnit<?>> U addUnit(U unit, Class<? extends Quantity<?>> type) { 431 INSTANCE.units.add(unit); 432 INSTANCE.quantityToUnit.put(type, unit); 433 return unit; 434 } 435}