-
Notifications
You must be signed in to change notification settings - Fork 412
Expand file tree
/
Copy pathHowMuch.cs
More file actions
126 lines (99 loc) · 4.32 KB
/
Copy pathHowMuch.cs
File metadata and controls
126 lines (99 loc) · 4.32 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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<HowMuch, HowMuchUnit>
{
public HowMuch(double value, HowMuchUnit unit)
{
Unit = unit;
Value = value;
}
public static HowMuch From(double value, HowMuchUnit unit)
{
return new HowMuch(value, unit);
}
public double As(HowMuchUnit unit)
{
throw new NotImplementedException();
}
public HowMuchUnit Unit { get; }
public double Value { get; }
#region IQuantity
public static readonly QuantityInfo<HowMuch, HowMuchUnit> Info = new(
nameof(HowMuch),
HowMuchUnit.Some,
new UnitDefinition<HowMuchUnit>[]
{
new(HowMuchUnit.Some, "Some", BaseUnits.Undefined),
new(HowMuchUnit.ATon, "Tons", new BaseUnits(mass: MassUnit.Tonne)),
new(HowMuchUnit.AShitTon, "ShitTons", BaseUnits.Undefined)
},
new HowMuch(0, HowMuchUnit.Some),
new BaseDimensions(0, 1, 0, 0, 0, 0, 0),
From,
RegisterUnitConversions);
QuantityInfo<HowMuch, HowMuchUnit> IQuantity<HowMuch, HowMuchUnit>.QuantityInfo
{
get => Info;
}
QuantityInfo<HowMuchUnit> IQuantity<HowMuchUnit>.QuantityInfo
{
get => Info;
}
QuantityInfo IQuantity.QuantityInfo
{
get => Info;
}
public BaseDimensions Dimensions => Info.BaseDimensions;
UnitKey IQuantity.UnitKey
{
get => UnitKey.ForUnit(Unit);
}
public double As(Enum unit) => Convert.ToDouble(unit);
public double 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<HowMuchUnit> ToUnit(HowMuchUnit unit)
{
throw new NotImplementedException();
}
IQuantity<HowMuchUnit> IQuantity<HowMuchUnit>.ToUnit(UnitSystem unitSystem)
{
throw new NotImplementedException();
}
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})";
public bool Equals(IQuantity? other, IQuantity tolerance) => throw new NotImplementedException();
public bool Equals(HowMuch other, HowMuch tolerance)
{
throw new NotImplementedException();
}
#if !NET
QuantityInfo IQuantity.QuantityInfo
{
get { return Info; }
}
Enum IQuantity.Unit => Unit;
#endif
#endregion
internal static void RegisterUnitConversions(UnitConverter unitConverter)
{
// Register in unit converter: HowMuchUnit -> BaseUnit
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.ATon, HowMuchUnit.Some, howMuch => new HowMuch(howMuch.Value * 1e3, HowMuchUnit.Some));
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.AShitTon, HowMuchUnit.Some, howMuch => new HowMuch(howMuch.Value * 1e6, HowMuchUnit.Some));
// Register in unit converter: BaseUnit <-> BaseUnit
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.Some, HowMuchUnit.Some, howMuch => howMuch);
// Register in unit converter: BaseUnit -> HowMuchUnit
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.Some, HowMuchUnit.ATon, howMuch => new HowMuch(howMuch.Value * 1e-3, HowMuchUnit.ATon));
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.Some, HowMuchUnit.AShitTon, howMuch => new HowMuch(howMuch.Value * 1e-6, HowMuchUnit.AShitTon));
}
}
}