| 1 | /* |
| 2 | * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences. |
| 3 | * Copyright (C) 2006 - JScience (http://jscience.org/) |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software is |
| 7 | * freely granted, provided that this notice is preserved. |
| 8 | */ |
| 9 | package javax.measure.unit; |
| 10 | |
| 11 | import java.io.Serializable; |
| 12 | |
| 13 | import javax.measure.converter.RationalConverter; |
| 14 | import javax.measure.converter.UnitConverter; |
| 15 | import javax.measure.quantity.Dimensionless; |
| 16 | |
| 17 | /** |
| 18 | * <p> This class represents the dimension of an unit. Two units <code>u1</code> |
| 19 | * and <code>u2</code> are {@link Unit#isCompatible compatible} if and |
| 20 | * only if <code>(u1.getDimension().equals(u2.getDimension())))</code> |
| 21 | * </p> |
| 22 | * |
| 23 | * <p> Instances of this class are immutable.</p> |
| 24 | * |
| 25 | * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> |
| 26 | * @version 3.1, April 22, 2006 |
| 27 | * @see <a href="http://en.wikipedia.org/wiki/Dimensional_analysis"> |
| 28 | * Wikipedia: Dimensional Analysis</a> |
| 29 | */ |
| 30 | public final class Dimension implements Serializable { |
| 31 | |
| 32 | /** |
| 33 | * Holds the current physical model. |
| 34 | */ |
| 35 | private static Model CurrentModel = Model.STANDARD; |
| 36 | |
| 37 | /** |
| 38 | * Holds dimensionless. |
| 39 | */ |
| 40 | public static final Dimension NONE = new Dimension(Unit.ONE); |
| 41 | |
| 42 | /** |
| 43 | * Holds length dimension (L). |
| 44 | */ |
| 45 | public static final Dimension LENGTH = new Dimension('L'); |
| 46 | |
| 47 | /** |
| 48 | * Holds mass dimension (M). |
| 49 | */ |
| 50 | public static final Dimension MASS = new Dimension('M'); |
| 51 | |
| 52 | /** |
| 53 | * Holds time dimension (T). |
| 54 | */ |
| 55 | public static final Dimension TIME = new Dimension('T'); |
| 56 | |
| 57 | /** |
| 58 | * Holds electric current dimension (I). |
| 59 | */ |
| 60 | public static final Dimension ELECTRIC_CURRENT = new Dimension('I'); |
| 61 | |
| 62 | /** |
| 63 | * Holds temperature dimension (θ). |
| 64 | */ |
| 65 | public static final Dimension TEMPERATURE = new Dimension('θ'); |
| 66 | |
| 67 | /** |
| 68 | * Holds amount of substance dimension (N). |
| 69 | */ |
| 70 | public static final Dimension AMOUNT_OF_SUBSTANCE = new Dimension('N'); |
| 71 | |
| 72 | /** |
| 73 | * Holds the pseudo unit associated to this dimension. |
| 74 | */ |
| 75 | private final Unit<?> _pseudoUnit; |
| 76 | |
| 77 | /** |
| 78 | * Creates a new dimension associated to the specified symbol. |
| 79 | * |
| 80 | * @param symbol the associated symbol. |
| 81 | */ |
| 82 | public Dimension(char symbol) { |
| 83 | _pseudoUnit = new BaseUnit<Dimensionless>("[" + symbol + "]"); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Creates a dimension having the specified pseudo-unit |
| 88 | * (base unit or product of base unit). |
| 89 | * |
| 90 | * @param pseudoUnit the pseudo-unit identifying this dimension. |
| 91 | */ |
| 92 | private Dimension(Unit<?> pseudoUnit) { |
| 93 | _pseudoUnit = pseudoUnit; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns the product of this dimension with the one specified. |
| 98 | * |
| 99 | * @param that the dimension multiplicand. |
| 100 | * @return <code>this * that</code> |
| 101 | */ |
| 102 | public final Dimension times(Dimension that) { |
| 103 | return new Dimension(this._pseudoUnit.times(that._pseudoUnit)); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns the quotient of this dimension with the one specified. |
| 108 | * |
| 109 | * @param that the dimension divisor. |
| 110 | * @return <code>this / that</code> |
| 111 | */ |
| 112 | public final Dimension divide(Dimension that) { |
| 113 | return new Dimension(this._pseudoUnit.divide(that._pseudoUnit)); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns this dimension raised to an exponent. |
| 118 | * |
| 119 | * @param n the exponent. |
| 120 | * @return the result of raising this dimension to the exponent. |
| 121 | */ |
| 122 | public final Dimension pow(int n) { |
| 123 | return new Dimension(this._pseudoUnit.pow(n)); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns the given root of this dimension. |
| 128 | * |
| 129 | * @param n the root's order. |
| 130 | * @return the result of taking the given root of this dimension. |
| 131 | * @throws ArithmeticException if <code>n == 0</code>. |
| 132 | */ |
| 133 | public final Dimension root(int n) { |
| 134 | return new Dimension(this._pseudoUnit.root(n)); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns the representation of this dimension. |
| 139 | * |
| 140 | * @return the representation of this dimension. |
| 141 | */ |
| 142 | public String toString() { |
| 143 | return _pseudoUnit.toString(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Indicates if the specified dimension is equals to the one specified. |
| 148 | * |
| 149 | * @param that the object to compare to. |
| 150 | * @return <code>true</code> if this dimension is equals to that dimension; |
| 151 | * <code>false</code> otherwise. |
| 152 | */ |
| 153 | public boolean equals(Object that) { |
| 154 | if (this == that) |
| 155 | return true; |
| 156 | return (that instanceof Dimension) |
| 157 | && _pseudoUnit.equals(((Dimension) that)._pseudoUnit); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the hash code for this dimension. |
| 162 | * |
| 163 | * @return this dimension hashcode value. |
| 164 | */ |
| 165 | public int hashCode() { |
| 166 | return _pseudoUnit.hashCode(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets the model used to determinate the units dimensions. |
| 171 | * |
| 172 | * @param model the new model to be used when calculating unit dimensions. |
| 173 | */ |
| 174 | public static void setModel(Model model) { |
| 175 | Dimension.CurrentModel = model; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Returns the model used to determinate the units dimensions |
| 180 | * (default {@link Model#STANDARD STANDARD}). |
| 181 | * |
| 182 | * @return the model used when calculating unit dimensions. |
| 183 | */ |
| 184 | public static Model getModel() { |
| 185 | return Dimension.CurrentModel; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * This interface represents the mapping between {@link BaseUnit base units} |
| 190 | * and {@link Dimension dimensions}. Custom models may allow |
| 191 | * conversions not possible using the {@link #STANDARD standard} model. |
| 192 | * For example:[code] |
| 193 | * public static void main(String[] args) { |
| 194 | * Dimension.Model relativistic = new Dimension.Model() { |
| 195 | * RationalConverter meterToSecond = new RationalConverter(1, 299792458); // 1/c |
| 196 | * |
| 197 | * public Dimension getDimension(BaseUnit unit) { |
| 198 | * if (unit.equals(SI.METER)) return Dimension.TIME; |
| 199 | * return Dimension.Model.STANDARD.getDimension(unit); |
| 200 | * } |
| 201 | * |
| 202 | * public UnitConverter getTransform(BaseUnit unit) { |
| 203 | * if (unit.equals(SI.METER)) return meterToSecond; |
| 204 | * return Dimension.Model.STANDARD.getTransform(unit); |
| 205 | * }}; |
| 206 | * Dimension.setModel(relativistic); |
| 207 | * |
| 208 | * // Converts 1.0 GeV (energy) to kg (mass). |
| 209 | * System.out.println(Unit.valueOf("GeV").getConverterTo(KILOGRAM).convert(1.0)); |
| 210 | * } |
| 211 | * |
| 212 | * > 1.7826617302520883E-27[/code] |
| 213 | */ |
| 214 | public interface Model { |
| 215 | |
| 216 | /** |
| 217 | * Holds the standard model (default). |
| 218 | */ |
| 219 | public Model STANDARD = new Model() { |
| 220 | |
| 221 | public Dimension getDimension(BaseUnit<?> unit) { |
| 222 | if (unit.equals(SI.METRE)) return Dimension.LENGTH; |
| 223 | if (unit.equals(SI.KILOGRAM)) return Dimension.MASS; |
| 224 | if (unit.equals(SI.KELVIN)) return Dimension.TEMPERATURE; |
| 225 | if (unit.equals(SI.SECOND)) return Dimension.TIME; |
| 226 | if (unit.equals(SI.AMPERE)) return Dimension.ELECTRIC_CURRENT; |
| 227 | if (unit.equals(SI.MOLE)) return Dimension.AMOUNT_OF_SUBSTANCE; |
| 228 | if (unit.equals(SI.CANDELA)) return SI.WATT.getDimension(); |
| 229 | return new Dimension(new BaseUnit<Dimensionless>("[" + unit.getSymbol() + "]")); |
| 230 | } |
| 231 | |
| 232 | public UnitConverter getTransform(BaseUnit<?> unit) { |
| 233 | if (unit.equals(SI.CANDELA)) return new RationalConverter(1, 683); |
| 234 | return UnitConverter.IDENTITY; |
| 235 | } |
| 236 | }; |
| 237 | |
| 238 | /** |
| 239 | * Returns the dimension of the specified base unit (a dimension |
| 240 | * particular to the base unit if the base unit is not recognized). |
| 241 | * |
| 242 | * @param unit the base unit for which the dimension is returned. |
| 243 | * @return the dimension of the specified unit. |
| 244 | */ |
| 245 | Dimension getDimension(BaseUnit<?> unit); |
| 246 | |
| 247 | /** |
| 248 | * Returns the normalization transform of the specified base unit |
| 249 | * ({@link UnitConverter#IDENTITY IDENTITY} if the base unit is |
| 250 | * not recognized). |
| 251 | * |
| 252 | * @param unit the base unit for which the transform is returned. |
| 253 | * @return the normalization transform. |
| 254 | */ |
| 255 | UnitConverter getTransform(BaseUnit<?> unit); |
| 256 | } |
| 257 | |
| 258 | private static final long serialVersionUID = 1L; |
| 259 | } |