Class Unit

java.lang.Object
org.osgi.util.measurement.Unit

public class Unit extends Object
A unit system for measurements. This class contains definitions of the most common SI units.

This class only support exponents for the base SI units in the range -64 to +63. Any operation which produces an exponent outside of this range will result in a Unit object with undefined exponents.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Unit
    The electric current unit ampere (A)
    private static final int
     
    private static final Unit[]
    An array containing all units defined.
    private static Map<Unit,Unit>
     
    static final Unit
    The electric charge unit coulomb (C).
    static final Unit
    The luminous intensity unit candela (cd)
    private static final int
     
    static final Unit
    The capacitance unit farad (F).
    static final Unit
    The absorbed dose unit gray (Gy).
    static final Unit
    The frequency unit hertz (Hz).
    static final Unit
    The energy unit joule (J).
    static final Unit
    The temperature unit kelvin (K)
    private static final int
     
    static final Unit
    The catalytic activity unit katal (kat).
    static final Unit
    The mass unit kilogram (kg)
    private static final int
     
    static final Unit
    The illuminance unit lux (lx).
    static final Unit
    The length unit meter (m)
    static final Unit
    The speed unit meter per second (m/s)
    static final Unit
    The acceleration unit meter per second squared (m/s2)
    private static final int
     
    static final Unit
    The area unit square meter (m2)
    static final Unit
    The volume unit cubic meter (m3)
    private static final long
     
    static final Unit
    The amount of substance unit mole (mol)
    private static final int
     
    static final Unit
    The force unit newton (N).
    private final String
     
    static final Unit
    The electric resistance unit ohm.
    static final Unit
    The pressure unit pascal (Pa).
    static final Unit
    The angle unit radians (rad)
    private static final int
     
    static final Unit
    The time unit second (s)
    static final Unit
    The electric conductance unit siemens (S).
    private static final int
     
    static final Unit
    The magnetic flux density unit tesla (T).
    private final long
     
    static final Unit
    No Unit (Unity)
    private static final long
     
    static final Unit
    The electric potential difference unit volt (V).
    static final Unit
    The power unit watt (W).
    static final Unit
    The magnetic flux unit weber (Wb).
    private static final long
     
    private static final int
     
    private static final long
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Unit(String name, long type)
    Creates a new Unit instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) Unit
    add(Unit that)
    Returns a new Unit that is the addition of this Unit and the Unit specified.
    private static void
    addSIname(int si, String name, StringBuilder numerator, StringBuilder denominator)
     
    private static String
    computeName(long type)
     
    private static long
    createType(int _x, int _rad, int _cd, int _mol, int _A, int _K, int _kg, int _s, int _m)
    Create a type field from the base SI unit exponent values.
    (package private) Unit
    div(Unit that)
    Returns a new Unit that is the division of this Unit and the Unit specified
    boolean
    Checks whether this Unit object is equal to the specified Unit object.
    (package private) static Unit
    find(long type)
    Finds a Unit based on a type.
    int
    Returns the hash code for this object.
    private boolean
    Checks whether the unit has a special type, i.e.
    (package private) Unit
    mul(Unit that)
    Returns a new Unit that is the multiplication of this Unit and the Unit specified
    (package private) Unit
    sub(Unit that)
    Returns a new Unit that is the subtraction between this Unit and the Unit specified.
    Returns a String object representing the Unit

    Methods inherited from class java.lang.Object

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

    • UNITY

      private static final long UNITY
    • ZERO

      private static final long ZERO
      See Also:
    • MASK

      private static final long MASK
      See Also:
    • m_SHIFT

      private static final int m_SHIFT
      See Also:
    • s_SHIFT

      private static final int s_SHIFT
      See Also:
    • kg_SHIFT

      private static final int kg_SHIFT
      See Also:
    • K_SHIFT

      private static final int K_SHIFT
      See Also:
    • A_SHIFT

      private static final int A_SHIFT
      See Also:
    • mol_SHIFT

      private static final int mol_SHIFT
      See Also:
    • cd_SHIFT

      private static final int cd_SHIFT
      See Also:
    • rad_SHIFT

      private static final int rad_SHIFT
      See Also:
    • x_SHIFT

      private static final int x_SHIFT
      See Also:
    • x_MASK

      private static final long x_MASK
      See Also:
    • unity

      public static final Unit unity
      No Unit (Unity)
    • m

      public static final Unit m
      The length unit meter (m)
    • s

      public static final Unit s
      The time unit second (s)
    • kg

      public static final Unit kg
      The mass unit kilogram (kg)
    • K

      public static final Unit K
      The temperature unit kelvin (K)
    • A

      public static final Unit A
      The electric current unit ampere (A)
    • mol

      public static final Unit mol
      The amount of substance unit mole (mol)
    • cd

      public static final Unit cd
      The luminous intensity unit candela (cd)
    • m_s

      public static final Unit m_s
      The speed unit meter per second (m/s)
    • m_s2

      public static final Unit m_s2
      The acceleration unit meter per second squared (m/s2)
    • m2

      public static final Unit m2
      The area unit square meter (m2)
    • m3

      public static final Unit m3
      The volume unit cubic meter (m3)
    • Hz

      public static final Unit Hz
      The frequency unit hertz (Hz).

      hertz is expressed in SI units as 1/s

    • N

      public static final Unit N
      The force unit newton (N).

      N is expressed in SI units as m·kg/s2

    • Pa

      public static final Unit Pa
      The pressure unit pascal (Pa).

      Pa is equal to N/m2 or is expressed in SI units as kg/m·s2

    • J

      public static final Unit J
      The energy unit joule (J).

      joule is equal to N·m or is expressed in SI units as m2·kg/s2

    • W

      public static final Unit W
      The power unit watt (W).

      watt is equal to J/s or is expressed in SI units as m2·kg/s3

    • C

      public static final Unit C
      The electric charge unit coulomb (C).

      coulomb is expressed in SI units as s·A

    • V

      public static final Unit V
      The electric potential difference unit volt (V).

      volt is equal to W/A or is expressed in SI units as m2·kg/s3·A

    • F

      public static final Unit F
      The capacitance unit farad (F).

      farad is equal to C/V or is expressed in SI units as s4·A2/m2·kg

    • Ohm

      public static final Unit Ohm
      The electric resistance unit ohm.

      ohm is equal to V/A or is expressed in SI units as m2·kg/s3·A2

    • S

      public static final Unit S
      The electric conductance unit siemens (S).

      siemens is equal to A/V or is expressed in SI units as s3·A2/m2·kg

    • Wb

      public static final Unit Wb
      The magnetic flux unit weber (Wb).

      weber is equal to V·s or is expressed in SI units as m2·kg/s2·A

    • T

      public static final Unit T
      The magnetic flux density unit tesla (T).

      tesla is equal to Wb/m2 or is expressed in SI units as kg/s2·A

    • lx

      public static final Unit lx
      The illuminance unit lux (lx).

      lux is expressed in SI units as cd/m2

    • Gy

      public static final Unit Gy
      The absorbed dose unit gray (Gy).

      Gy is equal to J/kg or is expressed in SI units as m2/s2

    • kat

      public static final Unit kat
      The catalytic activity unit katal (kat).

      katal is expressed in SI units as mol/s

    • rad

      public static final Unit rad
      The angle unit radians (rad)
    • allUnits

      private static final Unit[] allUnits
      An array containing all units defined. The first seven items must be m, s, kg, K, A, mol, cd, rad in this order!
    • base

      private static Map<Unit,Unit> base
    • name

      private final String name
    • type

      private final long type
  • Constructor Details

    • Unit

      private Unit(String name, long type)
      Creates a new Unit instance.
      Parameters:
      name - the name of the Unit
      type - the type of the Unit
  • Method Details

    • createType

      private static long createType(int _x, int _rad, int _cd, int _mol, int _A, int _K, int _kg, int _s, int _m)
      Create a type field from the base SI unit exponent values.
    • equals

      public boolean equals(Object obj)
      Checks whether this Unit object is equal to the specified Unit object. The Unit objects are considered equal if their exponents are equal.
      Overrides:
      equals in class Object
      Parameters:
      obj - the Unit object that should be checked for equality
      Returns:
      true if the specified Unit object is equal to this Unit object.
    • hashCode

      public int hashCode()
      Returns the hash code for this object.
      Overrides:
      hashCode in class Object
      Returns:
      This object's hash code.
    • mul

      Unit mul(Unit that)
      Returns a new Unit that is the multiplication of this Unit and the Unit specified
      Parameters:
      that - the Unit that will be multiplied with this Unit
      Returns:
      a new Unit that is the multiplication of this Unit and the Unit specified
      Throws:
      RuntimeException - if both Unit s are special
      See Also:
    • div

      Unit div(Unit that)
      Returns a new Unit that is the division of this Unit and the Unit specified
      Parameters:
      that - the Unit that this Unit will be divided with
      Returns:
      a new Unit that is the division of this Unit and the Unit specified
      Throws:
      RuntimeException - if both Unit s are special
      See Also:
    • add

      Unit add(Unit that)
      Returns a new Unit that is the addition of this Unit and the Unit specified.
      Parameters:
      that - the Unit that should be added to this Unit
      Returns:
      a new Unit that is the addition of this Unit and the Unit specified.
      Throws:
      RuntimeException - if the two Unit s are not the same
    • sub

      Unit sub(Unit that)
      Returns a new Unit that is the subtraction between this Unit and the Unit specified.
      Parameters:
      that - the Unit that will be subtracted from this Unit
      Returns:
      a new Unit that is the subtraction between this Unit and the Unit specified.
      Throws:
      RuntimeException - if the Unit specified is not the same as this Unit
    • find

      static Unit find(long type)
      Finds a Unit based on a type. If the Unit is not found, it will be created and added to the list of all units under a null name.
      Parameters:
      type - the type of the Unit to find
      Returns:
      the Unit
    • toString

      public String toString()
      Returns a String object representing the Unit
      Overrides:
      toString in class Object
      Returns:
      A String object representing the Unit
    • computeName

      private static String computeName(long type)
    • addSIname

      private static void addSIname(int si, String name, StringBuilder numerator, StringBuilder denominator)
    • isSpecial

      private boolean isSpecial()
      Checks whether the unit has a special type, i.e. not a SI unit.
      Returns:
      true if the type is special, otherwise false.