001/* 002 * Units of Measurement Reference Implementation 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, 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 1.0, $Date: 2016-10-06 $ 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 protected static void setCurrent(DimensionalModel model) { 104 currentModel = model; 105 } 106 107 /** 108 * Default constructor (allows for derivation). 109 */ 110 protected DimensionalModel() { 111 } 112 113 /** 114 * Returns the fundamental dimension for the one specified. If the specified dimension is a dimensional product, the dimensional product of its 115 * fundamental dimensions is returned. Physical quantities are considered commensurate only if their fundamental dimensions are equals using the 116 * current physics model. 117 * 118 * @param dimension 119 * the dimension for which the fundamental dimension is returned. 120 * @return <code>this</code> or a rational product of fundamental dimension. 121 */ 122 public Dimension getFundamentalDimension(Dimension dimension) { 123 Map<? extends Dimension, Integer> dimensions = dimension.getBaseDimensions(); 124 if (dimensions == null) 125 return dimension; // Fundamental dimension. 126 // Dimensional Product. 127 Dimension fundamentalProduct = QuantityDimension.NONE; 128 for (Map.Entry<? extends Dimension, Integer> e : dimensions.entrySet()) { 129 fundamentalProduct = fundamentalProduct.multiply(this.getFundamentalDimension(e.getKey())).pow(e.getValue()); 130 } 131 return fundamentalProduct; 132 } 133 134 /** 135 * Returns the dimensional transform of the specified dimension. If the specified dimension is a fundamental dimension or a product of fundamental 136 * dimensions the identity converter is returned; otherwise the converter from the system unit (SI) of the specified dimension to the system unit 137 * (SI) of its fundamental dimension is returned. 138 * 139 * @param dimension 140 * the dimension for which the dimensional transform is returned. 141 * @return the dimensional transform (identity for fundamental dimensions). 142 * @throws UnsupportedOperationException 143 * if an unsupported transform is attemted. 144 */ 145 public UnitConverter getDimensionalTransform(Dimension dimension) { 146 Map<? extends Dimension, Integer> dimensions = dimension.getBaseDimensions(); 147 if (dimensions == null) 148 return AbstractConverter.IDENTITY; // Fundamental dimension. 149 // Dimensional Product. 150 UnitConverter toFundamental = AbstractConverter.IDENTITY; 151 for (Map.Entry<? extends Dimension, Integer> e : dimensions.entrySet()) { 152 UnitConverter cvtr = this.getDimensionalTransform(e.getKey()); 153 if (!(cvtr.isLinear())) 154 throw new UnsupportedOperationException("Non-linear dimensional transform"); 155 int pow = e.getValue(); 156 if (pow < 0) { // Negative power. 157 pow = -pow; 158 cvtr = cvtr.inverse(); 159 } 160 for (int j = 0; j < pow; j++) { 161 toFundamental = toFundamental.concatenate(cvtr); 162 } 163 } 164 return toFundamental; 165 } 166}