Package org.djunits.unit
Class Units
java.lang.Object
org.djunits.unit.Units
Units is a static class that can register and resolve string representations of units, possibly using a locale. The Units
class is responsible for maintaining a registry of all units based on their textual abbreviations. It allows for a unit to
register itself, and for code to retrieve a unit based on a textual abbreviation. The Units class also takes care of
localization of the unit representations. If the Locale is not US, it will look for a resource bundle of the active Locale to
see if localized textual abbreviations are registered, and it will use these when resolving the unit. When no localized
matches can be found, it will test the (default) US Locale abbreviations as well.
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classUTF-8 loader for .properties ResourceBundles. -
Method Summary
Modifier and TypeMethodDescriptionstatic ResourceBundleReturn a resource bundle for the Locale.static StringlocalizedQuantityName(Class<? extends UnitInterface<?, ?>> unitClass) Get the localized unit name for a unit class.static StringlocalizedQuantityName(Locale locale, String quantityName) Lookup a quantity name for a given locale.static StringlocalizedUnitDisplayAbbr(Class<?> unitClass, String unitKey) Lookup a display abbreviation for a given unit key.static StringlocalizedUnitDisplayAbbr(Locale locale, String quantityName, String unitKey) Lookup a display abbreviation for a given unit key.static StringlocalizedUnitName(Class<?> unitClass, String unitKey) Lookup a display name for a given unit key.static StringlocalizedUnitName(Locale locale, String quantityName, String unitKey) Lookup a display Name for a given unit key.static StringlocalizedUnitTextualAbbr(Class<?> unitClass, String unitKey) Lookup a textual abbreviation (key) for a given unit key.static StringlocalizedUnitTextualAbbr(Locale locale, String quantityName, String unitKey) Lookup a textual abbreviation for a given unit key.static voidregister(UnitInterface<?, ?> unit) Register a unit so it can be found based on its textual abbreviations.static Map<String,Map<String, UnitInterface<?, ?>>> Return a safe copy of the registered units, e.g. to build pick lists in a user interface.static <U extends UnitInterface<U,?>>
ULook up a unit in the registry, based on its textual abbreviation.static StringunitClassName(Class<?> cls) Return the proper class name for a unit class, also when it is a nested class.static voidunregister(UnitInterface<?, ?> unit) Unregister a unit, e.g. in a unit test.
-
Method Details
-
register
Register a unit so it can be found based on its textual abbreviations.- Parameters:
unit- the unit to register- Throws:
NullPointerException- when unit is null
-
unregister
Unregister a unit, e.g. in a unit test. No exception will be thrown when the unit was not in the registration map.- Parameters:
unit- the unit to unregister- Throws:
NullPointerException- when unit is null
-
resolve
public static <U extends UnitInterface<U,?>> U resolve(Class<U> unitClass, String abbreviation) throws UnitRuntimeException Look up a unit in the registry, based on its textual abbreviation.- Type Parameters:
U- the unit type- Parameters:
unitClass- the unit class for which the abbreviation has to be looked upabbreviation- the abbreviation to look up in the unit registry- Returns:
- the unit belonging to the abbreviation (if it exists)
- Throws:
NullPointerException- when unitClass or abbreviation is nullUnitRuntimeException- when the unit did not exist, or the abbreviation was not registered
-
registeredUnits
Return a safe copy of the registered units, e.g. to build pick lists in a user interface.- Returns:
- a safe copy of the registered units
-
unitClassName
Return the proper class name for a unit class, also when it is a nested class. This method returns 'Length.Unit' rather than 'Unit' for the inner 'Unit' class of the 'Length' class.- Parameters:
cls- the unit class to return the name for, including the outer class name(s)- Returns:
- the class name, including the outer class name(s)
-
bundle
Return a resource bundle for the Locale.- Parameters:
locale- the locale to search for- Returns:
- The resource budle belonging to the given locale
-
localizedQuantityName
Lookup a quantity name for a given locale.- Parameters:
locale- the localequantityName- the simple class name of the quantity- Returns:
- the localized name of the quantity
-
localizedQuantityName
Get the localized unit name for a unit class.- Parameters:
unitClass- the class of the unit to lookup- Returns:
- the localized name of the quantity belonging to that unit
-
localizedUnitDisplayAbbr
Lookup a display abbreviation for a given unit key. If it cannot be found, use the stored US unit as a fallback. If the US-based unit cannot be found on the basis of the unit key, return the unit key itself as the display abbreviation.- Parameters:
locale- the localequantityName- the simple class name of the quantityunitKey- the key of the unit- Returns:
- the localized display abbreviation of the unit
-
localizedUnitDisplayAbbr
Lookup a display abbreviation for a given unit key. If it cannot be found, use the stored US unit as a fallback. If the US-based unit cannot be found on the basis of the unit key, return the unit key itself as the display abbreviation.- Parameters:
unitClass- the class of the unit to lookupunitKey- the key of the unit- Returns:
- the localized display abbreviation of the unit
-
localizedUnitName
Lookup a display Name for a given unit key. If it cannot be found, use the stored US unit as a fallback. If the US-based unit cannot be found on the basis of the unit key, return the unit key itself as the display name.- Parameters:
locale- the localequantityName- the simple class name of the quantityunitKey- the key of the unit- Returns:
- the localized display name of the unit
-
localizedUnitName
Lookup a display name for a given unit key. If it cannot be found, use the stored US unit as a fallback. If the US-based unit cannot be found on the basis of the unit key, return the unit key itself as the display name.- Parameters:
unitClass- the class of the unit to lookupunitKey- the key of the unit- Returns:
- the localized display name of the unit
-
localizedUnitTextualAbbr
Lookup a textual abbreviation for a given unit key. If it cannot be found, use the stored US unit as a fallback. If the US-based unit cannot be found on the basis of the unit key, return the unit key itself as the textual abbreviation.- Parameters:
locale- the localequantityName- the simple class name of the quantityunitKey- the key of the unit- Returns:
- the localized textual abbreviation of the unit
-
localizedUnitTextualAbbr
Lookup a textual abbreviation (key) for a given unit key. If it cannot be found, use the stored US unit as a fallback. If the US-based unit cannot be found on the basis of the unit key, return the unit key itself as the textual abbreviation.- Parameters:
unitClass- the class of the unit to lookupunitKey- the key of the unit- Returns:
- the localized textual abbreviation of the unit
-