Package org.djunits.unit.quantity
Class Quantity<U extends Unit<U>>
- java.lang.Object
-
- org.djunits.unit.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-2020 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:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Quantity(String name, byte[] siSignature)
Create a unit base with the SI dimensions, provided as a byte array.Quantity(String name, String siString)
Create a unit base with the SI dimensions as a String.Quantity(String name, SIDimensions siDimensions)
Create a unit base with the SI dimensions.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object obj)
String
getName()
Retrieve the name of the quantity.SIDimensions
getSiDimensions()
U
getStandardUnit()
Retrieve the standard unit for this unit base (usually the first registered unit).U
getUnitByAbbreviation(String abbreviation)
Retrieve a unit by one of its abbreviations.U
getUnitById(String id)
Retrieve a unit by Id.Map<String,U>
getUnitsByAbbreviation()
Return a safe copy of the registryByAbbreviation.Map<String,U>
getUnitsById()
Retrieve a safe copy of the registryById.int
hashCode()
U
of(String abbreviation)
Retrieve a unit by one of its abbreviations.void
registerUnit(U unit, SIPrefixes siPrefixes, double siPrefixPower)
Register the unit in the map.String
toString()
void
unregister(U unit)
Unregister a unit from the registry, e.g.
-
-
-
Constructor Detail
-
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 objectNullPointerException
- 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 Detail
-
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
-
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 registryById.- Returns:
- Map<String, U>; a safe copy of the registryById
-
getUnitsByAbbreviation
public Map<String,U> getUnitsByAbbreviation()
Return a safe copy of the registryByAbbreviation.- Returns:
- Map<String, U>; a safe copy of the registryByAbbreviation
-
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)
-
-