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.spi; 031 032import java.util.Map; 033 034import javax.measure.Dimension; 035import javax.measure.UnitConverter; 036 037import tec.units.ri.AbstractConverter; 038import tec.units.ri.quantity.QuantityDimension; 039 040/** 041 * <p> 042 * This class represents the physical model used for dimensional analysis. 043 * </p> 044 * 045 * <p> 046 * In principle, dimensions of physical quantities could be defined as "fundamental" (such as momentum or energy or electric current) making such 047 * quantities uncommensurate (not comparable). Modern physics has cast doubt on the very existence of incompatible fundamental dimensions of physical 048 * quantities. For example, most physicists do not recognize temperature, {@link QuantityDimension#TEMPERATURE ?}, as a fundamental dimension since it 049 * essentially expresses the energy per particle per degree of freedom, which can be expressed in terms of energy (or mass, length, and time). To 050 * support, such model the method {@link #getConverter} may returns a non-null value for distinct dimensions. 051 * </p> 052 * 053 * <p> 054 * The default model is {@link StandardModel Standard}. Applications may use one of the predefined model or create their own. <code> 055 * DimensionalModel relativistic = new DimensionalModel() { 056 * public Dimension getFundamentalDimension(Dimension dimension) { 057 * if (dimension.equals(QuantityDimension.LENGTH)) return QuantityDimension.TIME; // Consider length derived from time. 058 * return super.getDimension(dimension); // Returns product of fundamental dimension. 059 * } 060 * public UnitConverter getDimensionalTransform(Dimension dimension) { 061 * if (dimension.equals(QuantityDimension.LENGTH)) return new RationalConverter(1, 299792458); // Converter (1/C) from LENGTH SI unit (m) to TIME SI unit (s). 062 * return super.getDimensionalTransform(dimension); 063 * } 064 * }; 065 * try { 066 * DimensionalModel.setCurrent(relativistic); // Current thread use the relativistic model. 067 * Units.KILOGRAM.getConverterToAny(Units.JOULE); // Allowed. 068 * ... 069 * } finally { 070 * cleanup(); 071 * } 072 * </code> 073 * </p> 074 * 075 * @see <a href="http://en.wikipedia.org/wiki/Dimensional_analysis">Wikipedia: Dimensional Analysis</a> 076 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 077 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 078 * @version 0.5.7, $Date: 2015-12-28 $ 079 */ 080public abstract class DimensionalModel { 081 082 /** 083 * Holds the current model. 084 */ 085 private static DimensionalModel currentModel = new StandardModel(); 086 087 /** 088 * Returns the current dimensional model (by default an instance of {@link StandardModel}). 089 * 090 * @return the current physical model. 091 */ 092 public static DimensionalModel current() { 093 return currentModel; 094 } 095 096 /** 097 * Sets the current dimensional model 098 * 099 * @param model 100 * the new current model. 101 * @see #current 102 */ 103 // TODO should we rename to init() like in API Bootstrap? 104 protected static void setCurrent(DimensionalModel model) { 105 currentModel = model; 106 } 107 108 /** 109 * Default constructor (allows for derivation). 110 */ 111 protected DimensionalModel() { 112 } 113 114 /** 115 * Returns the fundamental dimension for the one specified. If the specified dimension is a dimensional product, the dimensional product of its 116 * fundamental dimensions is returned. Physical quantities are considered commensurate only if their fundamental dimensions are equals using the 117 * current physics model. 118 * 119 * @param dimension 120 * the dimension for which the fundamental dimension is returned. 121 * @return <code>this</code> or a rational product of fundamental dimension. 122 */ 123 public Dimension getFundamentalDimension(Dimension dimension) { 124 Map<? extends Dimension, Integer> dimensions = dimension.getBaseDimensions(); 125 if (dimensions == null) 126 return dimension; // Fundamental dimension. 127 // Dimensional Product. 128 Dimension fundamentalProduct = QuantityDimension.NONE; 129 for (Map.Entry<? extends Dimension, Integer> e : dimensions.entrySet()) { 130 fundamentalProduct = fundamentalProduct.multiply(this.getFundamentalDimension(e.getKey())).pow(e.getValue()); 131 } 132 return fundamentalProduct; 133 } 134 135 /** 136 * Returns the dimensional transform of the specified dimension. If the specified dimension is a fundamental dimension or a product of fundamental 137 * dimensions the identity converter is returned; otherwise the converter from the system unit (SI) of the specified dimension to the system unit 138 * (SI) of its fundamental dimension is returned. 139 * 140 * @param dimension 141 * the dimension for which the dimensional transform is returned. 142 * @return the dimensional transform (identity for fundamental dimensions). 143 * @throws UnsupportedOperationException 144 * if an unsupported transform is attemted. 145 */ 146 public UnitConverter getDimensionalTransform(Dimension dimension) { 147 Map<? extends Dimension, Integer> dimensions = dimension.getBaseDimensions(); 148 if (dimensions == null) 149 return AbstractConverter.IDENTITY; // Fundamental dimension. 150 // Dimensional Product. 151 UnitConverter toFundamental = AbstractConverter.IDENTITY; 152 for (Map.Entry<? extends Dimension, Integer> e : dimensions.entrySet()) { 153 UnitConverter cvtr = this.getDimensionalTransform(e.getKey()); 154 if (!(cvtr.isLinear())) 155 throw new UnsupportedOperationException("Non-linear dimensional transform"); 156 int pow = e.getValue(); 157 if (pow < 0) { // Negative power. 158 pow = -pow; 159 cvtr = cvtr.inverse(); 160 } 161 for (int j = 0; j < pow; j++) { 162 toFundamental = toFundamental.concatenate(cvtr); 163 } 164 } 165 return toFundamental; 166 } 167}