Skip to content

Commit ea45f7a

Browse files
committed
Implement interfaces for HowMuch
1 parent f5682d8 commit ea45f7a

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

UnitsNet.Tests/CustomQuantities/HowMuch.cs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using UnitsNet.Units;
34

45
namespace UnitsNet.Tests.CustomQuantities
@@ -100,6 +101,118 @@ QuantityInfo IQuantity.QuantityInfo
100101

101102
Enum IQuantity.Unit => Unit;
102103
#endif
104+
105+
#endregion
106+
107+
108+
#region Equality / IComparable
109+
110+
/// <summary>Returns true if less or equal to.</summary>
111+
public static bool operator <=(HowMuch left, HowMuch right)
112+
{
113+
return left.Value <= right.ToUnit(left.Unit).Value;
114+
}
115+
116+
/// <summary>Returns true if greater than or equal to.</summary>
117+
public static bool operator >=(HowMuch left, HowMuch right)
118+
{
119+
return left.Value >= right.ToUnit(left.Unit).Value;
120+
}
121+
122+
/// <summary>Returns true if less than.</summary>
123+
public static bool operator <(HowMuch left, HowMuch right)
124+
{
125+
return left.Value < right.ToUnit(left.Unit).Value;
126+
}
127+
128+
/// <summary>Returns true if greater than.</summary>
129+
public static bool operator >(HowMuch left, HowMuch right)
130+
{
131+
return left.Value > right.ToUnit(left.Unit).Value;
132+
}
133+
134+
// We use obsolete attribute to communicate the preferred equality members to use.
135+
// CS0809: Obsolete member 'memberA' overrides non-obsolete member 'memberB'.
136+
#pragma warning disable CS0809
137+
138+
/// <summary>Indicates strict equality of two <see cref="HowMuch"/> quantities, where both <see cref="Value" /> and <see cref="Unit" /> are exactly equal.</summary>
139+
[Obsolete(
140+
"For null checks, use `x is null` syntax to not invoke overloads. For equality checks, use Equals(HowMuch other, HowMuch tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units.")]
141+
public static bool operator ==(HowMuch left, HowMuch right)
142+
{
143+
return left.Equals(right);
144+
}
145+
146+
/// <summary>Indicates strict inequality of two <see cref="HowMuch"/> quantities, where both <see cref="Value" /> and <see cref="Unit" /> are exactly equal.</summary>
147+
[Obsolete(
148+
"For null checks, use `x is null` syntax to not invoke overloads. For equality checks, use Equals(HowMuch other, HowMuch tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units.")]
149+
public static bool operator !=(HowMuch left, HowMuch right)
150+
{
151+
return !(left == right);
152+
}
153+
154+
/// <inheritdoc />
155+
/// <summary>Indicates strict equality of two <see cref="HowMuch"/> quantities, where both <see cref="Value" /> and <see cref="Unit" /> are exactly equal.</summary>
156+
[Obsolete(
157+
"Use Equals(HowMuch other, HowMuch tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units.")]
158+
public override bool Equals(object? obj)
159+
{
160+
if (obj is null || !(obj is HowMuch otherQuantity))
161+
return false;
162+
163+
return Equals(otherQuantity);
164+
}
165+
166+
/// <inheritdoc />
167+
/// <summary>Indicates strict equality of two <see cref="HowMuch"/> quantities, where both <see cref="Value" /> and <see cref="Unit" /> are exactly equal.</summary>
168+
[Obsolete(
169+
"Use Equals(HowMuch other, HowMuch tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units.")]
170+
public bool Equals(HowMuch other)
171+
{
172+
return new { Value, Unit }.Equals(new { other.Value, other.Unit });
173+
}
174+
175+
#pragma warning restore CS0809
176+
177+
public override int GetHashCode()
178+
{
179+
return Comparison.GetHashCode(Unit, Value);
180+
}
181+
182+
public int CompareTo(object? obj)
183+
{
184+
if (obj is null) throw new ArgumentNullException(nameof(obj));
185+
if (!(obj is HowMuch otherQuantity)) throw new ArgumentException("Expected type HowMuch.", nameof(obj));
186+
187+
return CompareTo(otherQuantity);
188+
}
189+
190+
public int CompareTo(HowMuch other)
191+
{
192+
return Value.CompareTo(other.ToUnit(Unit).Value);
193+
}
194+
195+
#endregion
196+
197+
#region IParsable
198+
199+
public static HowMuch Parse(string str, IFormatProvider? provider)
200+
{
201+
return UnitsNetSetup.Default.QuantityParser.Parse<HowMuch, HowMuchUnit>(
202+
str,
203+
provider,
204+
From);
205+
}
206+
207+
public static bool TryParse([NotNullWhen(true)] string? str, IFormatProvider? provider, out HowMuch result)
208+
{
209+
return UnitsNetSetup.Default.QuantityParser.TryParse<HowMuch, HowMuchUnit>(
210+
str,
211+
provider,
212+
From,
213+
out result);
214+
}
215+
103216
#endregion
104217
}
105218
}

0 commit comments

Comments
 (0)