Class UnitOfMeasure
- Namespace
- NanoQL.Experimental
- Assembly
- NanoQL.dll
A unit of measure specification: its name, code and type.
[Experimental("NQL0001")]
public class UnitOfMeasure : IEquatable<UnitOfMeasure>
- Inheritance
-
UnitOfMeasure
- Implements
- Derived
- Inherited Members
Remarks
Immutable, and an extension point: each of QuantLib's units derives from it (BarrelUnitOfMeasure,
…), and a user may build one. QuantLib's empty UnitOfMeasure(), a placeholder whose members throw, is a
UnitOfMeasure? null.
QuantLib's units are presets: each reads its data from Current when it is built (ResolveUnitOfMeasure(string, Func<UnitOfMeasureData>)), QuantLib's unless a provider configures them, its code QuantLib's always.
QuantLib keeps a process-wide registry keyed by name, so a second construction with a known name returns the first
construction's data whatever it was given: UnitOfMeasure("Barrels", "B2", Mass) after
("Barrels", "BBL", Volume) has code BBL and type Volume (unitofmeasure.cpp:37-49). There is no
registry here, so every unit holds the data it is given, a documented divergence
(Divergences/CommodityRegistryTests.cs).
Two units are equal when their codes are, compared ordinally, whatever their other data, as QuantLib's
operator== compares them (unitofmeasure.hpp:130-132); the hash is the code's. The equality is sealed,
so every subclass compares the same way.
Every unit's rounding is QuantLib's default Rounding(), which leaves an amount as it is, where QuantLib's is
Rounding(0), to the closest whole unit, which its public constructor can't change
(unitofmeasure.hpp:82-86), so its automated conversions round a thousand-barrel quantity to thousands of
barrels: 2 MB plus 1,500 BBL is 4 MB there, 3.5 MB here (Divergences/QuantityRoundingTests.cs).
Constructors
UnitOfMeasure(string, string, UnitOfMeasureType)
Initializes a new instance of the UnitOfMeasure class, with no triangulation unit.
public UnitOfMeasure(string name, string code, UnitOfMeasureType unitType)
Parameters
namestringThe unit's name, such as "Barrels".
codestringThe unit's code, such as "BBL".
unitTypeUnitOfMeasureTypeWhat the unit measures.
Exceptions
- ArgumentNullException
A string is null.
- ArgumentOutOfRangeException
unitTypeisn't a member of its enum.
Properties
Code
Gets the unit's code, such as "BBL" or "MT".
public string Code { get; }
Property Value
Name
Gets the unit's name, such as "Barrels".
public string Name { get; }
Property Value
Rounding
Gets the rounding of amounts in the unit: QuantLib's default Rounding(), none.
public Rounding Rounding { get; }
Property Value
TriangulationUnitOfMeasure
Gets the unit conversions go through when no direct one is known; null for none, QuantLib's empty unit.
public UnitOfMeasure? TriangulationUnitOfMeasure { get; }
Property Value
UnitType
Gets what the unit measures (mass, volume, …).
public UnitOfMeasureType UnitType { get; }
Property Value
Methods
Equals(UnitOfMeasure?)
public bool Equals(UnitOfMeasure? other)
Parameters
otherUnitOfMeasure
Returns
Equals(object?)
public override sealed bool Equals(object? obj)
Parameters
objobject
Returns
GetHashCode()
public override sealed int GetHashCode()
Returns
ToString()
The unit's code, as QuantLib's operator<< writes it.
public override string ToString()
Returns
- string
The code.
Operators
operator ==(UnitOfMeasure?, UnitOfMeasure?)
Whether two units are equal: their codes are.
public static bool operator ==(UnitOfMeasure? left, UnitOfMeasure? right)
Parameters
leftUnitOfMeasureA unit, or null.
rightUnitOfMeasureAnother unit, or null.
Returns
operator !=(UnitOfMeasure?, UnitOfMeasure?)
Whether two units differ.
public static bool operator !=(UnitOfMeasure? left, UnitOfMeasure? right)
Parameters
leftUnitOfMeasureA unit, or null.
rightUnitOfMeasureAnother unit, or null.
Returns
- bool
The negation of
==.