All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tec.units.ri.quantity.QuantityDimension Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
/*
 * Units of Measurement Reference Implementation
 * Copyright (c) 2005-2016, Jean-Marie Dautelle, Werner Keil, V2COM.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package tec.units.ri.quantity;

import tec.units.ri.AbstractUnit;
import tec.units.ri.unit.BaseUnit;
import tec.units.ri.unit.Units;

import javax.measure.Dimension;
import javax.measure.Quantity;
import javax.measure.Unit;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 

* This class represents a quantity dimension (dimension of a physical * quantity). *

* *

* The dimension associated to any given quantity are given by the published * {@link DimensionService} instances. For convenience, a static method * {@link QuantityDimension#getInstance(Class) aggregating the results of all * @link DimensionService} instances is provided.
*
* * QuantityDimension speedDimension = QuantityDimension.of(Speed.class); * *

* * @author Jean-Marie Dautelle * @author Werner Keil * @version 0.7, $Date: 2016-06-21 $ */ public final class QuantityDimension implements Dimension { private static final Logger logger = Logger.getLogger(QuantityDimension.class.getName()); /** * */ // private static final long serialVersionUID = 123289037718650030L; /** * Holds dimensionless. */ public static final Dimension NONE = new QuantityDimension(AbstractUnit.ONE); /** * Holds length dimension (L). */ public static final Dimension LENGTH = new QuantityDimension('L'); /** * Holds mass dimension (M). */ public static final Dimension MASS = new QuantityDimension('M'); /** * Holds time dimension (T). */ public static final Dimension TIME = new QuantityDimension('T'); /** * Holds electric current dimension (I). */ public static final Dimension ELECTRIC_CURRENT = new QuantityDimension('I'); /** * Holds temperature dimension (Θ). */ public static final Dimension TEMPERATURE = new QuantityDimension('\u0398'); /** * Holds amount of substance dimension (N). */ public static final Dimension AMOUNT_OF_SUBSTANCE = new QuantityDimension('N'); /** * Holds luminous intensity dimension (J). */ public static final Dimension LUMINOUS_INTENSITY = new QuantityDimension('J'); /** * Holds the pseudo unit associated to this dimension. */ private final Unit pseudoUnit; /** * Returns the dimension for the specified quantity type by aggregating the results of {@link DimensionService} or null if the * specified quantity is unknown. * * @param quantityType * the quantity type. * @return the dimension for the quantity type or null. */ public static > Dimension getInstance(Class quantityType) { // TODO: Track OSGi services and aggregate results (register custom // types) Unit siUnit = Units.getInstance().getUnit(quantityType); if (siUnit == null) logger.log(Level.FINER, "Quantity type: " + quantityType + " unknown"); // we're logging but probably FINER is // enough? return (siUnit != null) ? siUnit.getDimension() : null; } /** * Returns the physical dimension having the specified symbol. * * @param symbol * the associated symbol. */ @SuppressWarnings("rawtypes") QuantityDimension(char symbol) { pseudoUnit = new BaseUnit("[" + symbol + ']', NONE); } /** * Returns the dimension for the specified symbol. * * @param sambol * the quantity symbol. * @return the dimension for the given symbol. */ public static QuantityDimension getInstance(char symbol) { return new QuantityDimension(symbol); } /** * Constructor from pseudo-unit (not visible). * * @param pseudoUnit * the pseudo-unit. */ private QuantityDimension(Unit pseudoUnit) { this.pseudoUnit = pseudoUnit; } /** * Returns the product of this dimension with the one specified. If the specified dimension is not a physics dimension, then * that.multiply(this) is returned. * * @param that * the dimension multiplicand. * @return this * that */ public Dimension multiply(Dimension that) { return (that instanceof QuantityDimension) ? this.multiply((QuantityDimension) that) : that.multiply(this); } /** * Returns the product of this dimension with the one specified. * * @param that * the dimension multiplicand. * @return this * that */ public QuantityDimension multiply(QuantityDimension that) { return new QuantityDimension(this.pseudoUnit.multiply(that.pseudoUnit)); } /** * Returns the quotient of this dimension with the one specified. * * @param that * the dimension divisor. * @return this.multiply(that.pow(-1)) */ public Dimension divide(Dimension that) { return this.multiply(that.pow(-1)); } /** * Returns the quotient of this dimension with the one specified. * * @param that * the dimension divisor. * @return this.multiply(that.pow(-1)) */ public QuantityDimension divide(QuantityDimension that) { return this.multiply(that.pow(-1)); } /** * Returns this dimension raised to an exponent. * * @param n * the exponent. * @return the result of raising this dimension to the exponent. */ public final QuantityDimension pow(int n) { return new QuantityDimension(this.pseudoUnit.pow(n)); } /** * Returns the given root of this dimension. * * @param n * the root's order. * @return the result of taking the given root of this dimension. * @throws ArithmeticException * if n == 0. */ public final QuantityDimension root(int n) { return new QuantityDimension(this.pseudoUnit.root(n)); } /** * Returns the fundamental dimensions and their exponent whose product is this dimension or null if this dimension is a fundamental * dimension. * * @return the mapping between the fundamental dimensions and their exponent. */ @SuppressWarnings("rawtypes") public Map getBaseDimensions() { Map pseudoUnits = pseudoUnit.getBaseUnits(); if (pseudoUnits == null) return null; Map fundamentalDimensions = new HashMap(); for (Map.Entry entry : pseudoUnits.entrySet()) { fundamentalDimensions.put(new QuantityDimension(entry.getKey()), entry.getValue()); } return fundamentalDimensions; } @Override public String toString() { return pseudoUnit.toString(); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof QuantityDimension) { QuantityDimension other = (QuantityDimension) obj; return (pseudoUnit == other.pseudoUnit) || (pseudoUnit != null && pseudoUnit.equals(other.pseudoUnit)); } return false; } @Override public int hashCode() { return pseudoUnit == null ? 0 : pseudoUnit.hashCode(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy