001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2018, Jean-Marie Dautelle, Werner Keil, Otavio Santana. 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.AbstractConverter; 037import tech.units.indriya.quantity.QuantityDimension; 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 QuantityDimension#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:units@catmedia.us">Werner Keil</a> 077 * @version 0.5.5, $Date: 2015-07-25 $ 078 */ 079public abstract class DimensionalModel { 080 081 /** 082 * Holds the current model. 083 */ 084 private static DimensionalModel currentModel = new StandardModel(); 085 086 /** 087 * Returns the current model (by default an instance of {@link StandardModel}). 088 * 089 * @return the current dimensional model. 090 */ 091 public static DimensionalModel current() { 092 return currentModel; 093 } 094 095 /** 096 * Sets the current dimensional model 097 * 098 * @param model 099 * the new current model. 100 * @see #current 101 */ 102 protected static void setCurrent(DimensionalModel model) { 103 currentModel = model; 104 } 105 106 /** 107 * DefaultQuantityFactory constructor (allows for derivation). 108 */ 109 protected DimensionalModel() { 110 } 111 112 /** 113 * Returns the fundamental dimension for the one specified. If the specified dimension is a dimensional product, the dimensional product of its 114 * fundamental dimensions is returned. Physical quantities are considered commensurate only if their fundamental dimensions are equals using the 115 * current physics model. 116 * 117 * @param dimension 118 * the dimension for which the fundamental dimension is returned. 119 * @return <code>this</code> or a rational product of fundamental dimension. 120 */ 121 public Dimension getFundamentalDimension(Dimension dimension) { 122 Map<? extends Dimension, Integer> dimensions = dimension.getBaseDimensions(); 123 if (dimensions == null) 124 return dimension; // Fundamental dimension. 125 // Dimensional Product. 126 Dimension fundamentalProduct = QuantityDimension.NONE; 127 for (Map.Entry<? extends Dimension, Integer> e : dimensions.entrySet()) { 128 fundamentalProduct = fundamentalProduct.multiply(this.getFundamentalDimension(e.getKey())).pow(e.getValue()); 129 } 130 return fundamentalProduct; 131 } 132 133 /** 134 * Returns the dimensional transform of the specified dimension. If the specified dimension is a fundamental dimension or a product of fundamental 135 * dimensions the identity converter is returned; otherwise the converter from the system unit (SI) of the specified dimension to the system unit 136 * (SI) of its fundamental dimension is returned. 137 * 138 * @param dimension 139 * the dimension for which the dimensional transform is returned. 140 * @return the dimensional transform (identity for fundamental dimensions). 141 */ 142 public AbstractConverter getDimensionalTransform(Dimension dimension) { 143 Map<? extends Dimension, Integer> dimensions = dimension.getBaseDimensions(); 144 if (dimensions == null) 145 return AbstractConverter.IDENTITY; // Fundamental dimension. 146 // Dimensional Product. 147 AbstractConverter toFundamental = AbstractConverter.IDENTITY; 148 for (Map.Entry<? extends Dimension, Integer> e : dimensions.entrySet()) { 149 AbstractConverter cvtr = this.getDimensionalTransform(e.getKey()); 150 if (!(cvtr.isLinear())) 151 throw new UnsupportedOperationException("Non-linear dimensional transform"); 152 int pow = e.getValue(); 153 if (pow < 0) { // Negative power. 154 pow = -pow; 155 cvtr = cvtr.inverse(); 156 } 157 for (int j = 0; j < pow; j++) { 158 toFundamental = (AbstractConverter) toFundamental.concatenate(cvtr); 159 } 160 } 161 return toFundamental; 162 } 163}