001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2020, Units of Measurement project. 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-385, Indriya nor the names of their 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 tech.units.indriya.spi; 031 032import java.util.Map; 033 034import javax.measure.Dimension; 035 036import tech.units.indriya.function.AbstractConverter; 037import tech.units.indriya.unit.UnitDimension; 038 039/** 040 * <p> 041 * This class represents the physical model used for dimensional analysis. 042 * </p> 043 * 044 * <p> 045 * In principle, dimensions of physical quantities could be defined as "fundamental" (such as momentum or energy or electric current) making such 046 * quantities uncommensurate (not comparable). Modern physics has cast doubt on the very existence of incompatible fundamental dimensions of physical 047 * quantities. For example, most physicists do not recognize temperature, {@link UnitDimension#TEMPERATURE Θ}, as a fundamental dimension since it 048 * essentially expresses the energy per particle per degree of freedom, which can be expressed in terms of energy (or mass, length, and time). To 049 * support, such model the method {@link #getConverter} may returns a non-null value for distinct dimensions. 050 * </p> 051 * 052 * <p> 053 * The default model is {@link StandardModel Standard}. Applications may use one of the predefined model or create their own. <code> 054 * DimensionalModel relativistic = new DimensionalModel() { 055 * public Dimension getFundamentalDimension(Dimension dimension) { 056 * if (dimension.equals(QuantityDimension.LENGTH)) return QuantityDimension.TIME; // Consider length derived from time. 057 * return super.getDimension(dimension); // Returns product of fundamental dimension. 058 * } 059 * public UnitConverter getDimensionalTransform(Dimension dimension) { 060 * if (dimension.equals(QuantityDimension.LENGTH)) return new RationalConverter(1, 299792458); // Converter (1/C) from LENGTH SI unit (m) to TIME SI unit (s). 061 * return super.getDimensionalTransform(dimension); 062 * } 063 * }; 064 * try { 065 * DimensionalModel.setCurrent(relativistic); // Current thread use the relativistic model. 066 * Units.KILOGRAM.getConverterToAny(Units.JOULE); // Allowed. 067 * ... 068 * } finally { 069 * cleanup(); 070 * } 071 * </code> 072 * </p> 073 * 074 * @see <a href="http://en.wikipedia.org/wiki/Dimensional_analysis">Wikipedia: Dimensional Analysis</a> 075 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 076 * @author <a href="mailto:werner@units.tech">Werner Keil</a> 077 * @version 1.2, $Date: 2019-08-21 $ 078 * @since 1.0 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 model (by default an instance of {@link StandardModel}). 089 * 090 * @return the current dimensional 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 * DimensionalModel 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 = UnitDimension.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 */ 143 public AbstractConverter getDimensionalTransform(Dimension dimension) { 144 Map<? extends Dimension, Integer> dimensions = dimension.getBaseDimensions(); 145 if (dimensions == null) 146 return AbstractConverter.IDENTITY; // Fundamental dimension. 147 // Dimensional Product. 148 AbstractConverter toFundamental = AbstractConverter.IDENTITY; 149 for (Map.Entry<? extends Dimension, Integer> e : dimensions.entrySet()) { 150 AbstractConverter cvtr = this.getDimensionalTransform(e.getKey()); 151 if (!(cvtr.isLinear())) 152 throw new UnsupportedOperationException("Non-linear dimensional transform"); 153 int pow = e.getValue(); 154 if (pow < 0) { // Negative power. 155 pow = -pow; 156 cvtr = cvtr.inverse(); 157 } 158 for (int j = 0; j < pow; j++) { 159 toFundamental = (AbstractConverter) toFundamental.concatenate(cvtr); 160 } 161 } 162 return toFundamental; 163 } 164}