Class AbstractUnit<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>

java.lang.Object
org.djunits.unit.AbstractUnit<U,Q>
Type Parameters:
U - the unit type
Q - the quantity type belonging to this unit
All Implemented Interfaces:
UnitInterface<U,Q>
Direct Known Subclasses:
AbsorbedDose.Unit, Acceleration.Unit, AmountOfSubstance.Unit, Angle.Unit, AngularAcceleration.Unit, AngularVelocity.Unit, Area.Unit, ArealObjectDensity.Unit, CatalyticActivity.Unit, Density.Unit, Duration.Unit, ElectricalCapacitance.Unit, ElectricalConductance.Unit, ElectricalInductance.Unit, ElectricalResistance.Unit, ElectricCharge.Unit, ElectricCurrent.Unit, ElectricPotential.Unit, Energy.Unit, EquivalentDose.Unit, FlowMass.Unit, FlowVolume.Unit, Force.Unit, Frequency.Unit, Illuminance.Unit, Length.Unit, LinearDensity.Unit, LinearObjectDensity.Unit, LuminousFlux.Unit, LuminousIntensity.Unit, MagneticFlux.Unit, MagneticFluxDensity.Unit, Mass.Unit, Momentum.Unit, Power.Unit, Pressure.Unit, RadioActivity.Unit, SolidAngle.Unit, Speed.Unit, Temperature.Unit, Torque.Unit, Unitless, Volume.Unit, VolumetricObjectDensity.Unit

public abstract class AbstractUnit<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>> extends Object implements UnitInterface<U,Q>
The AbstractUnit is the parent class of all units, and encodes the common behavior of the units. All units are internally stored relative to a standard unit with conversion factor. This means that e.g., a meter is stored with conversion factor 1.0, and kilometer is stored with a conversion factor 1000.0. This means that if we want to express a length meter in kilometers, we have to divide by the conversion factor.

The conversion to and from the base unit is left to a Scale. Many scales are linear (e.g., converting dm, cm, and mm to meters), but there are also non-linear scales such as the percentage for an angle, where 90 degrees equals an infinite percentage. Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djunits.org. The DJUNITS project is distributed under a three-clause BSD-style license.

Author:
Alexander Verbraeck
  • Constructor Details

    • AbstractUnit

      public AbstractUnit(String textualAbbreviation, String name, Scale scale, UnitSystem unitSystem)
      Create a new unit, where the textual abbreviation is the same as the display abbreviation.
      Parameters:
      textualAbbreviation - the textual abbreviation of the unit, which also serves as the id
      name - the full name of the unit
      scale - the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
      unitSystem - unit system, e.g. SI or Imperial
    • AbstractUnit

      public AbstractUnit(String textualAbbreviation, String displayAbbreviation, String name, Scale scale, UnitSystem unitSystem)
      Create a new unit, with textual abbreviation(s) and a display abbreviation.
      Parameters:
      textualAbbreviation - the textual abbreviation of the unit, which also serves as the id
      displayAbbreviation - the display abbreviation of the unit
      name - the full name of the unit
      scale - the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
      unitSystem - unit system, e.g. SI or Imperial
  • Method Details

    • generateSiPrefixes

      public U generateSiPrefixes(boolean kilo, boolean perUnit)
      Generate and register the units with all SI-prefixes.
      Parameters:
      kilo - whether the base unit already has a "kilo" in its abbreviation/name, such as the kilogram
      perUnit - whether it is a "per unit" such as "per meter"
      Returns:
      the unit for method chaining
    • deriveUnit

      public U deriveUnit(String textualAbbreviation, String name, double scaleFactor, UnitSystem unitSystem)
      Return a linearly scaled derived unit for this unit, where the textual abbreviation is the same as the display abbreviation.
      Parameters:
      textualAbbreviation - the textual abbreviation of the unit, which doubles as the id
      name - the full name of the unit
      scaleFactor - the (linear) scale factor to multiply with the current (linear) scale factor
      unitSystem - unit system, e.g. SI or Imperial
      Returns:
      a derived unit for this unit
    • deriveUnit

      public abstract U deriveUnit(String textualAbbreviation, String displayAbbreviation, String name, double scaleFactor, UnitSystem unitSystem)
      Return a linearly scaled derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
      Parameters:
      textualAbbreviation - the textual abbreviation of the unit, which doubles as the id
      displayAbbreviation - the display abbreviation of the unit
      name - the full name of the unit
      scaleFactor - the (linear) scale factor to multiply with the current (linear) scale factor
      unitSystem - unit system, e.g. SI or Imperial
      Returns:
      a derived unit for this unit
    • getId

      public String getId()
      Description copied from interface: UnitInterface
      Return the id, which is the main abbreviation, of the unit.
      Specified by:
      getId in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Returns:
      the id (main abbreviation) of the unit
    • getStoredTextualAbbreviation

      public String getStoredTextualAbbreviation()
      Description copied from interface: UnitInterface
      Retrieve the stored textual abbreviation, which doubles as the id of the unit. Do not apply localization.
      Specified by:
      getStoredTextualAbbreviation in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Returns:
      the stored (non-localized) textual abbreviation
    • getTextualAbbreviation

      public String getTextualAbbreviation()
      Description copied from interface: UnitInterface
      Retrieve the textual abbreviation, which doubles as the id of the unit. Apply localization when possible.
      Specified by:
      getTextualAbbreviation in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Returns:
      the (localized) textual abbreviation
    • getStoredDisplayAbbreviation

      public String getStoredDisplayAbbreviation()
      Description copied from interface: UnitInterface
      Retrieve the stored display abbreviation, without localization.
      Specified by:
      getStoredDisplayAbbreviation in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Returns:
      the stored (non-localized) display abbreviation
    • getDisplayAbbreviation

      public String getDisplayAbbreviation()
      Description copied from interface: UnitInterface
      Retrieve the display abbreviation, and apply localization when possible.
      Specified by:
      getDisplayAbbreviation in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Returns:
      the (localized) display abbreviation
    • getStoredName

      public String getStoredName()
      Description copied from interface: UnitInterface
      Return the name, which is the main written explanation, of the unit. Do not apply localization.
      Specified by:
      getStoredName in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Returns:
      the stored (non-localized) name of the unit
    • getName

      public String getName()
      Description copied from interface: UnitInterface
      Return the name, which is the main written explanation, of the unit. Apply localization when possible.
      Specified by:
      getName in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Returns:
      the (localized) name of the unit
    • getScale

      public Scale getScale()
      Description copied from interface: UnitInterface
      Retrieve the scale of this unit.
      Specified by:
      getScale in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Returns:
      the scale of this unit
    • getUnitSystem

      public UnitSystem getUnitSystem()
      Description copied from interface: UnitInterface
      Retrieve the unit system of this unit.
      Specified by:
      getUnitSystem in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Returns:
      unitSystem the unit system of this unit
    • setSiPrefix

      public U setSiPrefix(SIPrefix siPrefix)
      Description copied from interface: UnitInterface
      Set the SI-prefix so it can be localized if necessary.
      Specified by:
      setSiPrefix in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Parameters:
      siPrefix - the SI-prefix to set
      Returns:
      the unit for method chaining
    • setSiPrefix

      public U setSiPrefix(String prefix)
      Description copied from interface: UnitInterface
      Set the SI-prefix so it can be localized if necessary. This method does NOT handle kilo-prefixes.
      Specified by:
      setSiPrefix in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Parameters:
      prefix - the string-representation of the SI-prefix to set
      Returns:
      the unit for method chaining
    • setSiPrefixKilo

      public U setSiPrefixKilo(String prefix)
      Description copied from interface: UnitInterface
      Set the SI-prefix so it can be localized if necessary. This method handles kilo-prefixes.
      Specified by:
      setSiPrefixKilo in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Parameters:
      prefix - the string-representation of the SI-prefix to set
      Returns:
      the unit for method chaining
    • setSiPrefixPer

      public U setSiPrefixPer(String prefix)
      Description copied from interface: UnitInterface
      Set the SI-prefix so it can be localized if necessary. This method handles per-unit prefixes.
      Specified by:
      setSiPrefixPer in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Parameters:
      prefix - the string-representation of the SI-prefix to set
      Returns:
      the unit for method chaining
    • getSiPrefix

      public SIPrefix getSiPrefix()
      Description copied from interface: UnitInterface
      Return the SI-prefix so it can be localized if necessary.
      Specified by:
      getSiPrefix in interface UnitInterface<U extends UnitInterface<U,Q>,Q extends Quantity<Q,U>>
      Returns:
      the SI-prefix of this unit, or null when the unit has no SI-prefix
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object