Class Units

java.lang.Object
org.djunits.unit.Units

public final class Units extends Object
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
  • Method Details

    • register

      public static void register(UnitInterface<?,?> unit)
      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

      public static void unregister(UnitInterface<?,?> unit)
      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 up
      abbreviation - 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 null
      UnitRuntimeException - when the unit did not exist, or the abbreviation was not registered
    • registeredUnits

      public static Map<String,Map<String,UnitInterface<?,?>>> 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

      public static String unitClassName(Class<?> cls)
      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

      public static ResourceBundle bundle(Locale locale)
      Return a resource bundle for the Locale.
      Parameters:
      locale - the locale to search for
      Returns:
      The resource budle belonging to the given locale
    • localizedQuantityName

      public static String localizedQuantityName(Locale locale, String quantityName)
      Lookup a quantity name for a given locale.
      Parameters:
      locale - the locale
      quantityName - the simple class name of the quantity
      Returns:
      the localized name of the quantity
    • localizedQuantityName

      public static String localizedQuantityName(Class<? extends UnitInterface<?,?>> unitClass)
      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

      public static String localizedUnitDisplayAbbr(Locale locale, String quantityName, String unitKey)
      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 locale
      quantityName - the simple class name of the quantity
      unitKey - the key of the unit
      Returns:
      the localized display abbreviation of the unit
    • localizedUnitDisplayAbbr

      public static String localizedUnitDisplayAbbr(Class<?> unitClass, String unitKey)
      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 lookup
      unitKey - the key of the unit
      Returns:
      the localized display abbreviation of the unit
    • localizedUnitName

      public static String localizedUnitName(Locale locale, String quantityName, String unitKey)
      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 locale
      quantityName - the simple class name of the quantity
      unitKey - the key of the unit
      Returns:
      the localized display name of the unit
    • localizedUnitName

      public static String localizedUnitName(Class<?> unitClass, String unitKey)
      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 lookup
      unitKey - the key of the unit
      Returns:
      the localized display name of the unit
    • localizedUnitTextualAbbr

      public static String localizedUnitTextualAbbr(Locale locale, String quantityName, String unitKey)
      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 locale
      quantityName - the simple class name of the quantity
      unitKey - the key of the unit
      Returns:
      the localized textual abbreviation of the unit
    • localizedUnitTextualAbbr

      public static String localizedUnitTextualAbbr(Class<?> unitClass, String unitKey)
      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 lookup
      unitKey - the key of the unit
      Returns:
      the localized textual abbreviation of the unit