Table of Contents

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

name string

The unit's name, such as "Barrels".

code string

The unit's code, such as "BBL".

unitType UnitOfMeasureType

What the unit measures.

Exceptions

ArgumentNullException

A string is null.

ArgumentOutOfRangeException

unitType isn't a member of its enum.

Properties

Code

Gets the unit's code, such as "BBL" or "MT".

public string Code { get; }

Property Value

string

Name

Gets the unit's name, such as "Barrels".

public string Name { get; }

Property Value

string

Rounding

Gets the rounding of amounts in the unit: QuantLib's default Rounding(), none.

public Rounding Rounding { get; }

Property Value

Rounding

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

UnitOfMeasure

UnitType

Gets what the unit measures (mass, volume, …).

public UnitOfMeasureType UnitType { get; }

Property Value

UnitOfMeasureType

Methods

Equals(UnitOfMeasure?)

public bool Equals(UnitOfMeasure? other)

Parameters

other UnitOfMeasure

Returns

bool

Equals(object?)

public override sealed bool Equals(object? obj)

Parameters

obj object

Returns

bool

GetHashCode()

public override sealed int GetHashCode()

Returns

int

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

left UnitOfMeasure

A unit, or null.

right UnitOfMeasure

Another unit, or null.

Returns

bool

true when both are null or their codes are equal.

operator !=(UnitOfMeasure?, UnitOfMeasure?)

Whether two units differ.

public static bool operator !=(UnitOfMeasure? left, UnitOfMeasure? right)

Parameters

left UnitOfMeasure

A unit, or null.

right UnitOfMeasure

Another unit, or null.

Returns

bool

The negation of ==.