Package org.djunits.unit
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 typeQ- 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 Summary
ConstructorsConstructorDescriptionAbstractUnit(String textualAbbreviation, String displayAbbreviation, String name, Scale scale, UnitSystem unitSystem) Create a new unit, with textual abbreviation(s) and a display abbreviation.AbstractUnit(String textualAbbreviation, String name, Scale scale, UnitSystem unitSystem) Create a new unit, where the textual abbreviation is the same as the display abbreviation. -
Method Summary
Modifier and TypeMethodDescriptionderiveUnit(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.abstract UderiveUnit(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.booleangenerateSiPrefixes(boolean kilo, boolean perUnit) Generate and register the units with all SI-prefixes.Retrieve the display abbreviation, and apply localization when possible.getId()Return the id, which is the main abbreviation, of the unit.getName()Return the name, which is the main written explanation, of the unit.getScale()Retrieve the scale of this unit.Return the SI-prefix so it can be localized if necessary.Retrieve the stored display abbreviation, without localization.Return the name, which is the main written explanation, of the unit.Retrieve the stored textual abbreviation, which doubles as the id of the unit.Retrieve the textual abbreviation, which doubles as the id of the unit.Retrieve the unit system of this unit.inthashCode()setSiPrefix(String prefix) Set the SI-prefix so it can be localized if necessary.setSiPrefix(SIPrefix siPrefix) Set the SI-prefix so it can be localized if necessary.setSiPrefixKilo(String prefix) Set the SI-prefix so it can be localized if necessary.setSiPrefixPer(String prefix) Set the SI-prefix so it can be localized if necessary.toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.djunits.unit.UnitInterface
fromBaseValue, getBaseUnit, ofSi, quantityInUnit, siUnit, toBaseValue
-
Constructor Details
-
AbstractUnit
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 idname- the full name of the unitscale- the scale to use to convert between this unit and the standard (e.g., SI, BASE) unitunitSystem- 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 iddisplayAbbreviation- the display abbreviation of the unitname- the full name of the unitscale- the scale to use to convert between this unit and the standard (e.g., SI, BASE) unitunitSystem- unit system, e.g. SI or Imperial
-
-
Method Details
-
generateSiPrefixes
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 kilogramperUnit- 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 idname- the full name of the unitscaleFactor- the (linear) scale factor to multiply with the current (linear) scale factorunitSystem- 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 iddisplayAbbreviation- the display abbreviation of the unitname- the full name of the unitscaleFactor- the (linear) scale factor to multiply with the current (linear) scale factorunitSystem- unit system, e.g. SI or Imperial- Returns:
- a derived unit for this unit
-
getId
Description copied from interface:UnitInterfaceReturn the id, which is the main abbreviation, of the unit.- Specified by:
getIdin interfaceUnitInterface<U extends UnitInterface<U,Q>, Q extends Quantity<Q, U>> - Returns:
- the id (main abbreviation) of the unit
-
getStoredTextualAbbreviation
Description copied from interface:UnitInterfaceRetrieve the stored textual abbreviation, which doubles as the id of the unit. Do not apply localization.- Specified by:
getStoredTextualAbbreviationin interfaceUnitInterface<U extends UnitInterface<U,Q>, Q extends Quantity<Q, U>> - Returns:
- the stored (non-localized) textual abbreviation
-
getTextualAbbreviation
Description copied from interface:UnitInterfaceRetrieve the textual abbreviation, which doubles as the id of the unit. Apply localization when possible.- Specified by:
getTextualAbbreviationin interfaceUnitInterface<U extends UnitInterface<U,Q>, Q extends Quantity<Q, U>> - Returns:
- the (localized) textual abbreviation
-
getStoredDisplayAbbreviation
Description copied from interface:UnitInterfaceRetrieve the stored display abbreviation, without localization.- Specified by:
getStoredDisplayAbbreviationin interfaceUnitInterface<U extends UnitInterface<U,Q>, Q extends Quantity<Q, U>> - Returns:
- the stored (non-localized) display abbreviation
-
getDisplayAbbreviation
Description copied from interface:UnitInterfaceRetrieve the display abbreviation, and apply localization when possible.- Specified by:
getDisplayAbbreviationin interfaceUnitInterface<U extends UnitInterface<U,Q>, Q extends Quantity<Q, U>> - Returns:
- the (localized) display abbreviation
-
getStoredName
Description copied from interface:UnitInterfaceReturn the name, which is the main written explanation, of the unit. Do not apply localization.- Specified by:
getStoredNamein interfaceUnitInterface<U extends UnitInterface<U,Q>, Q extends Quantity<Q, U>> - Returns:
- the stored (non-localized) name of the unit
-
getName
Description copied from interface:UnitInterfaceReturn the name, which is the main written explanation, of the unit. Apply localization when possible.- Specified by:
getNamein interfaceUnitInterface<U extends UnitInterface<U,Q>, Q extends Quantity<Q, U>> - Returns:
- the (localized) name of the unit
-
getScale
Description copied from interface:UnitInterfaceRetrieve the scale of this unit.- Specified by:
getScalein interfaceUnitInterface<U extends UnitInterface<U,Q>, Q extends Quantity<Q, U>> - Returns:
- the scale of this unit
-
getUnitSystem
Description copied from interface:UnitInterfaceRetrieve the unit system of this unit.- Specified by:
getUnitSystemin interfaceUnitInterface<U extends UnitInterface<U,Q>, Q extends Quantity<Q, U>> - Returns:
- unitSystem the unit system of this unit
-
setSiPrefix
Description copied from interface:UnitInterfaceSet the SI-prefix so it can be localized if necessary.- Specified by:
setSiPrefixin interfaceUnitInterface<U extends UnitInterface<U,Q>, Q extends Quantity<Q, U>> - Parameters:
siPrefix- the SI-prefix to set- Returns:
- the unit for method chaining
-
setSiPrefix
Description copied from interface:UnitInterfaceSet the SI-prefix so it can be localized if necessary. This method does NOT handle kilo-prefixes.- Specified by:
setSiPrefixin interfaceUnitInterface<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
Description copied from interface:UnitInterfaceSet the SI-prefix so it can be localized if necessary. This method handles kilo-prefixes.- Specified by:
setSiPrefixKiloin interfaceUnitInterface<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
Description copied from interface:UnitInterfaceSet the SI-prefix so it can be localized if necessary. This method handles per-unit prefixes.- Specified by:
setSiPrefixPerin interfaceUnitInterface<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
Description copied from interface:UnitInterfaceReturn the SI-prefix so it can be localized if necessary.- Specified by:
getSiPrefixin interfaceUnitInterface<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() -
equals
-
toString
-