forked from angularsen/UnitsNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HowMuch.cs
61 lines (47 loc) · 2.19 KB
/
HowMuch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
namespace UnitsNet.Tests.CustomQuantities
{
/// <inheritdoc cref="IQuantity"/>
/// <summary>
/// Example of a custom/third-party quantity implementation, for plugging in quantities and units at runtime.
/// </summary>
public readonly struct HowMuch : IQuantity
{
public HowMuch(QuantityValue value, HowMuchUnit unit)
{
Unit = unit;
Value = value;
}
public bool Equals(IQuantity? other, IQuantity tolerance) => throw new NotImplementedException();
Enum IQuantity.Unit => Unit;
public HowMuchUnit Unit { get; }
public QuantityValue Value { get; }
#region IQuantity
private static readonly HowMuch Zero = new(0, HowMuchUnit.Some);
public BaseDimensions Dimensions => BaseDimensions.Dimensionless;
public QuantityInfo QuantityInfo => new(
nameof(HowMuch),
typeof(HowMuchUnit),
new UnitInfo[]
{
new UnitInfo<HowMuchUnit>(HowMuchUnit.Some, "Some", BaseUnits.Undefined, nameof(HowMuch)),
new UnitInfo<HowMuchUnit>(HowMuchUnit.ATon, "Tons", BaseUnits.Undefined, nameof(HowMuch)),
new UnitInfo<HowMuchUnit>(HowMuchUnit.AShitTon, "ShitTons", BaseUnits.Undefined, nameof(HowMuch)),
},
HowMuchUnit.Some,
Zero,
BaseDimensions.Dimensionless);
public QuantityValue As(Enum unit) => Convert.ToInt32(unit);
public QuantityValue As(UnitSystem unitSystem) => throw new NotImplementedException();
public IQuantity ToUnit(Enum unit)
{
if (unit is HowMuchUnit howMuchUnit) return new HowMuch(As(unit), howMuchUnit);
throw new ArgumentException("Must be of type HowMuchUnit.", nameof(unit));
}
public IQuantity ToUnit(UnitSystem unitSystem) => throw new NotImplementedException();
public override string ToString() => $"{Value} {Unit}";
public string ToString(string? format, IFormatProvider? formatProvider) => $"HowMuch ({format}, {formatProvider})";
public string ToString(IFormatProvider? provider) => $"HowMuch ({provider})";
#endregion
}
}