Class SystemUnit<Q extends javax.measure.Quantity<Q>>

java.lang.Object
org.apache.sis.measure.AbstractUnit<Q>
org.apache.sis.measure.SystemUnit<Q>
Type Parameters:
Q - the kind of quantity to be measured using this units.
All Implemented Interfaces:
Serializable, javax.measure.spi.QuantityFactory<Q>, javax.measure.Unit<Q>, LenientComparable

final class SystemUnit<Q extends javax.measure.Quantity<Q>> extends AbstractUnit<Q> implements javax.measure.spi.QuantityFactory<Q>
Implementation of base, alternate and derived units (see AbstractUnit for a description of unit kinds). A SystemUnit is a base or alternate unit if associated to a base UnitDimension, or is a derived units otherwise. No other type is allowed since SystemUnit is always a combination of fundamental units without scale factor or offset.
Since:
0.8
Version:
1.1
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final class 
    The converter for replacing the keys in the getBaseUnits() map from UnitDimension instances to SystemUnit instances.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) final UnitDimension
    The dimension of this unit of measurement.
    (package private) final ScalarFactory<Q>
    The factory to use for creating quantities, or null if none.
    (package private) static final String
    The non-empty symbol for Units.UNITY.
    (package private) final Class<Q>
    The type of quantity that uses this unit, or null if unknown.
    private ConventionalUnit<Q>[]
    Units for the same quantity but with scale factors that are not the SI one.
    private static final long
    For cross-version compatibility.

    Fields inherited from class org.apache.sis.measure.AbstractUnit

    DIVIDE, epsg, MULTIPLY, scope
  • Constructor Summary

    Constructors
    Constructor
    Description
    SystemUnit(Class<Q> quantity, UnitDimension dimension, String symbol, byte scope, short epsg, ScalarFactory<Q> factory)
    Creates a new unit having the given symbol and EPSG code.
  • Method Summary

    Modifier and Type
    Method
    Description
    javax.measure.Unit<Q>
    alternate(String symbol)
    Returns a system unit equivalent to this unscaled standard unit but used in expressions to distinguish between quantities of a different nature but of the same dimensions.
    <T extends javax.measure.Quantity<T>>
    javax.measure.Unit<T>
    asType(Class<T> type)
    Casts this unit to a parameterized unit of specified nature or throw a ClassCastException if the dimension of the specified quantity and this unit's dimension do not match.
    javax.measure.Quantity<Q>
    create(Number value, javax.measure.Unit<Q> unit)
    Creates a quantity for the given value and unit of measurement.
    private SystemUnit<?>
    create(UnitDimension newDimension, char operation, javax.measure.Unit<?> other)
    Returns a unit of the given dimension with default name and symbol.
    javax.measure.Unit<?>
    divide(javax.measure.Unit<?> divisor)
    Returns the quotient of this unit with the one specified.
    boolean
    Compares this unit with the given object for equality, optionally ignoring metadata and rounding errors.
    (package private) final boolean
    equalsIgnoreMetadata(javax.measure.Unit<Q> other)
    Returns true if this unit is equal to the given unit ignoring name, symbol and EPSG code.
    (package private) final Map<SystemUnit<?>,Fraction>
    Returns the base units used by Apache SIS implementations.
    Returns the base units and their exponent whose product is this unit, or null if this unit is a base unit (not a product of existing units).
    javax.measure.UnitConverter
    getConverterTo(javax.measure.Unit<Q> unit)
    Returns a converter of numeric values from this unit to another unit of same type.
    javax.measure.UnitConverter
    getConverterToAny(javax.measure.Unit<?> unit)
    Returns a converter from this unit to the specified unit of unknown type.
    javax.measure.Dimension
    Returns the dimension of this unit.
    Returns the unscaled system unit from which this unit is derived.
    int
    Returns a hash code value for this unit.
    javax.measure.Unit<?>
    multiply(javax.measure.Unit<?> multiplier)
    Returns the product of this unit with the one specified.
    javax.measure.Unit<?>
    pow(int n)
    Returns a unit equals to this unit raised to an exponent.
    private <T extends javax.measure.Quantity<T>>
    javax.measure.Unit<?>
    product(javax.measure.Unit<T> other, boolean inverse)
    Implementation of multiply(Unit) and divide(Unit) methods.
    (package private) final ConventionalUnit<Q>[]
    Returns units for the same quantity but with scale factors that are not the SI one, or null if none.
    (package private) final void
    related(int n)
    Invoked by Units initializer before to fill the related array.
    javax.measure.Unit<?>
    root(int n)
    Returns a unit equals to the given root of this unit.
    private boolean
    Returns true if the given symbol is null or equals to the symbol of this unit.
    javax.measure.Unit<Q>
    transform(javax.measure.UnitConverter operation)
    Returns the unit derived from this unit using the specified converter.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For cross-version compatibility.
      See Also:
    • ONE

      static final String ONE
      The non-empty symbol for Units.UNITY.
      See Also:
    • quantity

      final Class<Q extends javax.measure.Quantity<Q>> quantity
      The type of quantity that uses this unit, or null if unknown. This field should be null only when this unit is the result of an arithmetic operation and that result cannot be mapped to a known Quantity subtype.
    • dimension

      final UnitDimension dimension
      The dimension of this unit of measurement. Cannot be null.
    • factory

      final transient ScalarFactory<Q extends javax.measure.Quantity<Q>> factory
      The factory to use for creating quantities, or null if none. This field does not need to be serialized because AbstractUnit.readResolve() replaces deserialized instances by corresponding Units hard-coded instances.
  • Constructor Details

    • SystemUnit

      SystemUnit(Class<Q> quantity, UnitDimension dimension, String symbol, byte scope, short epsg, ScalarFactory<Q> factory)
      Creates a new unit having the given symbol and EPSG code.
      Parameters:
      quantity - the type of quantity that uses this unit, or null if unknown.
      dimension - the unit dimension.
      symbol - the unit symbol, or null if this unit has no specific symbol.
      scope - UnitRegistry.SI, UnitRegistry.ACCEPTED, other constants or 0 if unknown.
      epsg - the EPSG code, or 0 if this unit has no EPSG code.
      factory - the factory to use for creating quantities, or null if none.
  • Method Details

    • create

      private SystemUnit<?> create(UnitDimension newDimension, char operation, javax.measure.Unit<?> other)
      Returns a unit of the given dimension with default name and symbol. This method is invoked for creating the result of arithmetic operations. If there is no predefined unit for the given dimension, then the new unit may be allocated a symbol derived from this unit's symbol. A new symbol is created only if this unit symbol and the other unit symbol are simple (for example "m" but not "m²", or "N" but not "N/m").
      Parameters:
      operation - symbol to write after the symbol of this unit for generating the new unit symbol, or 0 for not inferring new symbol. Ignored if the condition documented in javadoc does not hold.
      other - other units to append after the operation symbol, or null if none or should be ignored. Ignored if the condition documented in javadoc does not hold.
    • sameSymbol

      private boolean sameSymbol(String symbol)
      Returns true if the given symbol is null or equals to the symbol of this unit.
    • getDimension

      public javax.measure.Dimension getDimension()
      Returns the dimension of this unit. Two units u1 and u2 are compatible if and only if u1.getDimension().equals(u2.getDimension()).
      Specified by:
      getDimension in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Returns:
      the dimension of this unit.
      See Also:
    • getSystemUnit

      public SystemUnit<Q> getSystemUnit()
      Returns the unscaled system unit from which this unit is derived. Since this unit is already a base, alternate or derived unit, this method returns true.
      Specified by:
      getSystemUnit in interface javax.measure.spi.QuantityFactory<Q extends javax.measure.Quantity<Q>>
      Specified by:
      getSystemUnit in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Specified by:
      getSystemUnit in class AbstractUnit<Q extends javax.measure.Quantity<Q>>
      Returns:
      this
    • getBaseUnits

      public Map<SystemUnit<?>,Integer> getBaseUnits()
      Returns the base units and their exponent whose product is this unit, or null if this unit is a base unit (not a product of existing units).
      Specified by:
      getBaseUnits in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Specified by:
      getBaseUnits in class AbstractUnit<Q extends javax.measure.Quantity<Q>>
      Returns:
      the base units and their exponent making up this unit.
    • getBaseSystemUnits

      final Map<SystemUnit<?>,Fraction> getBaseSystemUnits()
      Returns the base units used by Apache SIS implementations. Contrarily to getBaseUnits(), this method never returns null.
      Specified by:
      getBaseSystemUnits in class AbstractUnit<Q extends javax.measure.Quantity<Q>>
    • asType

      public <T extends javax.measure.Quantity<T>> javax.measure.Unit<T> asType(Class<T> type) throws ClassCastException
      Casts this unit to a parameterized unit of specified nature or throw a ClassCastException if the dimension of the specified quantity and this unit's dimension do not match.
      Specified by:
      asType in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Type Parameters:
      T - the type of the quantity measured by the unit.
      Parameters:
      type - the quantity class identifying the nature of the unit.
      Returns:
      this unit parameterized with the specified type.
      Throws:
      ClassCastException - if the dimension of this unit is different from the specified quantity dimension.
    • equalsIgnoreMetadata

      final boolean equalsIgnoreMetadata(javax.measure.Unit<Q> other)
      Returns true if this unit is equal to the given unit ignoring name, symbol and EPSG code. This method should always returns true if parameterized type has not been compromised with raw types or unchecked casts.
      Parameters:
      other - the other unit, which must be a system unit.
    • getConverterTo

      public javax.measure.UnitConverter getConverterTo(javax.measure.Unit<Q> unit) throws javax.measure.UnconvertibleException
      Returns a converter of numeric values from this unit to another unit of same type.
      Specified by:
      getConverterTo in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Parameters:
      unit - the unit of same type to which to convert the numeric values.
      Returns:
      the converter from this unit to that unit.
      Throws:
      javax.measure.UnconvertibleException - if the converter cannot be constructed.
    • getConverterToAny

      public javax.measure.UnitConverter getConverterToAny(javax.measure.Unit<?> unit) throws javax.measure.IncommensurableException
      Returns a converter from this unit to the specified unit of unknown type. This method can be used when the quantity type of the specified unit is unknown at compile-time or when dimensional analysis allows for conversion between units of different type.
      Specified by:
      getConverterToAny in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Parameters:
      unit - the unit to which to convert the numeric values.
      Returns:
      the converter from this unit to that unit.
      Throws:
      javax.measure.IncommensurableException - if this unit is not compatible with that unit.
      See Also:
    • alternate

      public javax.measure.Unit<Q> alternate(String symbol)
      Returns a system unit equivalent to this unscaled standard unit but used in expressions to distinguish between quantities of a different nature but of the same dimensions.

      The most important alternate unit in Apache SIS is Units.RADIAN, defined as below:

      Specified by:
      alternate in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Parameters:
      symbol - the new symbol for the alternate unit.
      Returns:
      the alternate unit.
      Throws:
      IllegalArgumentException - if the specified symbol is already associated to a different unit.
    • multiply

      public javax.measure.Unit<?> multiply(javax.measure.Unit<?> multiplier)
      Returns the product of this unit with the one specified.
      Specified by:
      multiply in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Parameters:
      multiplier - the unit multiplier.
      Returns:
      this × multiplier
    • divide

      public javax.measure.Unit<?> divide(javax.measure.Unit<?> divisor)
      Returns the quotient of this unit with the one specified.
      Specified by:
      divide in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Parameters:
      divisor - the unit divisor.
      Returns:
      thisdivisor
    • product

      private <T extends javax.measure.Quantity<T>> javax.measure.Unit<?> product(javax.measure.Unit<T> other, boolean inverse)
      Implementation of multiply(Unit) and divide(Unit) methods.
      Parameters:
      inverse - wether to use the inverse of other.
    • pow

      public javax.measure.Unit<?> pow(int n)
      Returns a unit equals to this unit raised to an exponent.
      Specified by:
      pow in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Parameters:
      n - the exponent.
      Returns:
      the result of raising this unit to the exponent.
    • root

      public javax.measure.Unit<?> root(int n)
      Returns a unit equals to the given root of this unit.
      Specified by:
      root in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Parameters:
      n - the root's order.
      Returns:
      the result of taking the given root of this unit.
      Throws:
      ArithmeticException - if n == 0.
    • transform

      public javax.measure.Unit<Q> transform(javax.measure.UnitConverter operation)
      Returns the unit derived from this unit using the specified converter.
      Specified by:
      transform in interface javax.measure.Unit<Q extends javax.measure.Quantity<Q>>
      Parameters:
      operation - the converter from the transformed unit to this unit.
      Returns:
      the unit after the specified transformation.
    • related

      final void related(int n)
      Invoked by Units initializer before to fill the related array. We define this method only for isolating the generic array creation.
    • related

      final ConventionalUnit<Q>[] related()
      Returns units for the same quantity but with scale factors that are not the SI one, or null if none. This method returns a direct reference to the internal field; caller shall not modify.
      Overrides:
      related in class AbstractUnit<Q extends javax.measure.Quantity<Q>>
    • equals

      public boolean equals(Object other, ComparisonMode mode)
      Compares this unit with the given object for equality, optionally ignoring metadata and rounding errors.
      Specified by:
      equals in interface LenientComparable
      Overrides:
      equals in class AbstractUnit<Q extends javax.measure.Quantity<Q>>
      Parameters:
      other - the other object to compare with this unit, or null.
      mode - the strictness level of the comparison.
      Returns:
      true if the given object is equal to this unit.
      See Also:
    • hashCode

      public int hashCode()
      Returns a hash code value for this unit.
      Overrides:
      hashCode in class AbstractUnit<Q extends javax.measure.Quantity<Q>>
    • create

      public javax.measure.Quantity<Q> create(Number value, javax.measure.Unit<Q> unit)
      Creates a quantity for the given value and unit of measurement.
      Specified by:
      create in interface javax.measure.spi.QuantityFactory<Q extends javax.measure.Quantity<Q>>