Class UnitRegistry

java.lang.Object
org.apache.sis.measure.UnitRegistry
All Implemented Interfaces:
Serializable, javax.measure.spi.SystemOfUnits

final class UnitRegistry extends Object implements javax.measure.spi.SystemOfUnits, Serializable
Lookup mechanism for finding a units from its quantity, dimension or symbol. This class opportunistically implements SystemOfUnits, but Apache SIS rather uses the static methods directly since we define all units in terms of SI.
Since:
0.8
Version:
1.2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static final byte
    Identifies units defined outside the SI system but accepted for use with SI.
    (package private) static final byte
    Identifies units defined by the centimeter–gram–second (CGS) system.
    private static final Map<Object,Object>
    All UnitDimension, SystemUnit or ConventionalUnit that are hard-coded in Apache SIS.
    (package private) static final byte
    Identifies units defined for use in British imperial system.
    private final int
    The bitmask for units to include.
    (package private) final String
    Name of this system of units.
    (package private) static final byte
    Identifies units defined in another system than the above.
    (package private) static final byte
    A bitmask specifying that the unit symbol can be combined with a SI prefix.
    private static final long
    For cross-version compatibility.
    (package private) static final byte
    Identifies units defined by the SI system.
    private Set<javax.measure.Unit<?>>
    The value returned by getUnits(), created when first needed.
    private static final WeakValueHashMap<Object,Object>
    Units defined by the user.
  • Constructor Summary

    Constructors
    Constructor
    Description
    UnitRegistry(String name, int includes)
    Creates a new unit system.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static void
    alias(javax.measure.Unit<?> unit, Comparable<?> alias)
    Adds an alias for the given unit.
    private static int
    filter(int existed, SystemUnit<?> unit, String s)
    Clears the existed bits for the cases where we allow dimension or quantity type collisions.
    (package private) static Object
    get(Object key)
    Returns the value associated to the given key, or null if none.
    Returns the name of this system of units.
    <Q extends javax.measure.Quantity<Q>>
    javax.measure.Unit<Q>
    getUnit(Class<Q> type)
    Returns the default unit for the specified quantity, or null if none.
    Set<javax.measure.Unit<?>>
    Returns a read only view over the units explicitly defined by this system.
    Set<javax.measure.Unit<?>>
    getUnits(javax.measure.Dimension dimension)
    Returns the units defined in this system having the specified dimension, or an empty set if none.
    (package private) static void
    Adds the given components, dim pair in the map of hard-coded values.
    (package private) static <Q extends javax.measure.Quantity<Q>>
    ConventionalUnit<Q>
    Invoked by Units static class initializer for registering SI conventional units.
    (package private) static <Q extends javax.measure.Quantity<Q>>
    SystemUnit<Q>
    init(SystemUnit<Q> unit)
    Invoked by Units static class initializer for registering SI base and derived units.
    (package private) static Object
    putIfAbsent(Object key, Object value)
    Adds the given key, value pair in the map of user-defined values, provided that no value is currently associated to the given key.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • serialVersionUID

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

      static final byte PREFIXABLE
      A bitmask specifying that the unit symbol can be combined with a SI prefix. This is usually combined only with SI, not ACCEPTED except the litre unit (cL, mL, etc) and bel (for creating the decibel unit). The gal unit can also be prefixed, but this unit is deprecated by ISO 80000-3:2006.
      See Also:
    • SI

      static final byte SI
      Identifies units defined by the SI system. All SystemUnit instances with this code can have a SI prefix.
      See Also:
    • ACCEPTED

      static final byte ACCEPTED
      Identifies units defined outside the SI system but accepted for use with SI.
      See Also:
    • CGS

      static final byte CGS
      Identifies units defined by the centimeter–gram–second (CGS) system.
      Since:
      1.2
      See Also:
    • IMPERIAL

      static final byte IMPERIAL
      Identifies units defined for use in British imperial system.
      See Also:
    • OTHER

      static final byte OTHER
      Identifies units defined in another system than the above.
      See Also:
    • HARD_CODED

      private static final Map<Object,Object> HARD_CODED
      All UnitDimension, SystemUnit or ConventionalUnit that are hard-coded in Apache SIS. This map is populated by Units static initializer and shall not be modified after initialization, in order to avoid the need for synchronization. Key and value types are restricted to the following pairs:
      Key and value types
      Key type Value type Description
      Map<UnitDimension,Fraction> UnitDimension Key is the base dimensions with their powers
      UnitDimension SystemUnit Key is the dimension of base or derived units.
      Class<Quantity> SystemUnit Key is the quantity type of base of derived units.
      String AbstractUnit Key is the unit symbol.
      Short AbstractUnit Key is the EPSG code.
    • USER_DEFINED

      private static final WeakValueHashMap<Object,Object> USER_DEFINED
      Units defined by the user. Accesses to this map implies synchronization. Values are stored by weak references and garbage collected when no longer used. Key and value types are the same than the one described in HARD_CODED.
      Implementation note: we separate hard-coded values from user-defined values because the amount of hard-coded values is relatively large, using weak references for them is useless, and most applications will not define any custom values. This map will typically stay empty.
    • name

      final String name
      Name of this system of units.
    • includes

      private final int includes
      The bitmask for units to include. Can be any combination of SI, ACCEPTED, IMPERIAL or OTHER bits.
    • units

      private transient Set<javax.measure.Unit<?>> units
      The value returned by getUnits(), created when first needed.
  • Constructor Details

    • UnitRegistry

      UnitRegistry(String name, int includes)
      Creates a new unit system.
  • Method Details

    • init

      static void init(Map<UnitDimension,Fraction> components, UnitDimension dim)
      Adds the given components, dim pair in the map of hard-coded values. This method shall be invoked in a single thread by the Units class initializer only (indirectly).
    • init

      static <Q extends javax.measure.Quantity<Q>> SystemUnit<Q> init(SystemUnit<Q> unit)
      Invoked by Units static class initializer for registering SI base and derived units. This method shall be invoked in a single thread by the Units class initializer only.
    • filter

      private static int filter(int existed, SystemUnit<?> unit, String s)
      Clears the existed bits for the cases where we allow dimension or quantity type collisions. This method is invoked for assertions only.
    • init

      static <Q extends javax.measure.Quantity<Q>> ConventionalUnit<Q> init(ConventionalUnit<Q> unit)
      Invoked by Units static class initializer for registering SI conventional units. This method shall be invoked in a single thread by the Units class initializer only.
    • alias

      static void alias(javax.measure.Unit<?> unit, Comparable<?> alias)
      Adds an alias for the given unit. The given alias shall be either an instance of String (for a symbol alias) or an instance of Short (for an EPSG code alias).
    • putIfAbsent

      static Object putIfAbsent(Object key, Object value)
      Adds the given key, value pair in the map of user-defined values, provided that no value is currently associated to the given key. This method shall be invoked only after the Units class has been fully initialized.
    • get

      static Object get(Object key)
      Returns the value associated to the given key, or null if none. This method can be invoked at anytime (at Units class initialization time or not).
    • getName

      public String getName()
      Returns the name of this system of units.
      Specified by:
      getName in interface javax.measure.spi.SystemOfUnits
    • getUnit

      public <Q extends javax.measure.Quantity<Q>> javax.measure.Unit<Q> getUnit(Class<Q> type)
      Returns the default unit for the specified quantity, or null if none.
      Specified by:
      getUnit in interface javax.measure.spi.SystemOfUnits
    • getUnits

      public Set<javax.measure.Unit<?>> getUnits()
      Returns a read only view over the units explicitly defined by this system. This include the base and derived units which are assigned a special name and symbol. This set does not include new units created by arithmetic or other operations.
      Specified by:
      getUnits in interface javax.measure.spi.SystemOfUnits
    • getUnits

      public Set<javax.measure.Unit<?>> getUnits(javax.measure.Dimension dimension)
      Returns the units defined in this system having the specified dimension, or an empty set if none.
      Specified by:
      getUnits in interface javax.measure.spi.SystemOfUnits