Class Quantity<U extends Unit<U>>

java.lang.Object
org.djunits.quantity.Quantity<U>
Type Parameters:
U - the unit to reference the actual unit in return values
All Implemented Interfaces:
Serializable

public class Quantity<U extends Unit<U>> extends Object implements Serializable
Quantity contains a map of all registered units belonging to this base. It also contains the SI 'fingerprint' of the unit. The fingerprint is registered in the UnitTypes singleton where are unit types are registered.

Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUNITS License

Author:
Alexander Verbraeck
See Also:
  • Constructor Details

    • Quantity

      public Quantity(String name, SIDimensions siDimensions)
      Create a unit base with the SI dimensions.
      Parameters:
      name - String; the quantity name (CamelCase)
      siDimensions - SIDimensions; the 9 dimensions of the unit, wrapped in an SIDimensions object
      Throws:
      NullPointerException - when one of the arguments is null
    • Quantity

      public Quantity(String name, String siString) throws UnitRuntimeException
      Create a unit base with the SI dimensions as a String.
      Parameters:
      name - String; the quantity name (CamelCase)
      siString - String; the 9 dimensions of the unit, represented as an SI string
      Throws:
      UnitRuntimeException - when the String cannot be translated into an SIDimensions object
      NullPointerException - when one of the arguments is null
    • Quantity

      public Quantity(String name, byte[] siSignature)
      Create a unit base with the SI dimensions, provided as a byte array.
      Parameters:
      name - String; the quantity name (CamelCase)
      siSignature - byte[]; the 9 dimensions of the unit
      Throws:
      NullPointerException - when one of the arguments is null
  • Method Details

    • registerUnit

      public void registerUnit(U unit, SIPrefixes siPrefixes, double siPrefixPower)
      Register the unit in the map. If the unit supports SI prefixes from yocto to yotta, 20 additional abbreviations are registered. When there is both a unit with an "SI prefix" and a separately registered unit, the most specific specification will be registered in the map. As an example, when the LengthUnit "METER" is registered, all 20 units such as the millimeter and the kilometer are registered as well. When earlier or later the "KILOMETER" is created as a separate unit, the "km" lookup will result in the "KILOMETER" registration rather than in the "METER" registration with a factor of 1000.
      Parameters:
      unit - U; the unit to register in the map.
      siPrefixes - SIPrefixes; indicates whether and which SI prefixes should be generated.
      siPrefixPower - double; the power factor of the SI prefixes, e.g. 2.0 for square meters and 3.0 for cubic meters.
    • unregister

      public void unregister(U unit)
      Unregister a unit from the registry, e.g. after a Unit test, or to insert a replacement for an already existing unit.
      Parameters:
      unit - U; the unit to unregister.
    • getName

      public final String getName()
      Retrieve the name of the quantity.
      Returns:
      String; the name of the quantity
    • getSiDimensions

      public final SIDimensions getSiDimensions()
      Returns:
      the siDimensions
    • getUnitById

      public U getUnitById(String id)
      Retrieve a unit by Id.
      Parameters:
      id - String; the id to look up
      Returns:
      the corresponding unit or null when it was not found
    • checkLocale

      protected void checkLocale()
      Check whether the locale for which abbreviation maps have been loaded is still current. If not, (re)load.
    • getUnitByAbbreviation

      public U getUnitByAbbreviation(String abbreviation)
      Retrieve a unit by one of its abbreviations. First try whether the abbreviation itself is available. If not, look up the unit without spaces, "." and "^" to map e.g., "kg.m/s^2" to "kgm/s2". If that fails, see if the unit is an SIDimensions string. If not, return null.
      Parameters:
      abbreviation - String; the abbreviation to look up
      Returns:
      the corresponding unit or null when it was not found
    • of

      public U of(String abbreviation)
      Retrieve a unit by one of its abbreviations. First try whether the abbreviation itself is available. If not, try without "." that might separate the units (e.g., "N.m"). If that fails, look up the unit without "." and "^" to map e.g., "kg.m/s^2" to "kgm/s2". If that fails, see if the unit is an SIDimensions string. If not, return null.
      Parameters:
      abbreviation - String; the abbreviation to look up
      Returns:
      the corresponding unit or null when it was not found
    • getUnitsById

      public Map<String,U> getUnitsById()
      Retrieve a safe copy of the unitsById.
      Returns:
      Map<String, U>; a safe copy of the unitsById
    • getUnitsByAbbreviation

      public Map<String,U> getUnitsByAbbreviation()
      Return a safe copy of the unitsByAbbreviation.
      Returns:
      Map<String, U>; a safe copy of the unitsByAbbreviation
    • getUnitsByLocalizedAbbreviation

      public Map<String,U> getUnitsByLocalizedAbbreviation()
      Return a safe copy of the unitsByLocalizedAbbreviation.
      Returns:
      Map<String, U>; a safe copy of the unitsByLocalizedAbbreviation
    • getLocalizedAbbreviations

      public Set<String> getLocalizedAbbreviations(U unit)
      Retrieve a safe copy of the localized unit abbreviations.
      Parameters:
      unit - U; the unit for which to retrieve the abbreviations
      Returns:
      Set<String>; the localized unit abbreviations
    • getLocalizedDisplayAbbreviation

      public String getLocalizedDisplayAbbreviation(U unit)
      Retrieve the localized display abbreviation.
      Parameters:
      unit - U; the unit for which to retrieve the display abbreviation
      Returns:
      String; the localized display abbreviation
    • getLocalizedTextualAbbreviation

      public String getLocalizedTextualAbbreviation(U unit)
      Retrieve the localized textual abbreviation.
      Parameters:
      unit - U; the unit for which to retrieve the textual abbreviation
      Returns:
      String; the localized textual abbreviation
    • getLocalizedName

      public String getLocalizedName()
      Retrieve the localized name of this unit.
      Returns:
      String; the localized name of this unit
    • getStandardUnit

      public U getStandardUnit()
      Retrieve the standard unit for this unit base (usually the first registered unit).
      Returns:
      U; the standardUnit for this unit base (usually the first registered unit)
    • 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