Skip to content

Commit

Permalink
Create ConstrainedFloat & remove redundant JConverter from PhoneNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmjr committed Feb 27, 2024
1 parent aec9932 commit 95c1c73
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
28 changes: 15 additions & 13 deletions src/ConstrainedFloat.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
namespace System;
using System.Numerics;

public readonly struct ConstrainedFloat
public abstract class ConstrainedFloat<TSelf>
#if NET7_0_OR_GREATER
: IParsable<ConstrainedFloat>
: IParsable<TSelf>
#endif
where TSelf : ConstrainedFloat<TSelf>, new()
{
private const float DefaultMin = float.MinValue;
private const float DefaultMax = float.MaxValue;

public ConstrainedFloat(float value, float min = DefaultMin, float max = DefaultMax)
protected ConstrainedFloat(float value, float min = DefaultMin, float max = DefaultMax)
{
if (min > max)
{
Expand All @@ -26,7 +27,7 @@ public ConstrainedFloat(float value, float min = DefaultMin, float max = Default
Max = max;
}

public ConstrainedFloat(float value, float[] range)
protected ConstrainedFloat(float value, float[] range)
{
Range = range;
Value = value;
Expand All @@ -41,28 +42,29 @@ public ConstrainedFloat(float value, float[] range)
public float Min { get; }
public float Max { get; }

public static implicit operator float(cfloat cfloat) => cfloat.Value;
public static implicit operator float(ConstrainedFloat<TSelf> cfloat) => cfloat.Value;

public static implicit operator cfloat(float value) => new(value, float.MinValue, float.MaxValue);
public static implicit operator ConstrainedFloat<TSelf>(float value) => (TSelf)Activator.CreateInstance(typeof(TSelf), value);
public static implicit operator TSelf(ConstrainedFloat<TSelf> value) => (TSelf)value;

public static bool operator ==(cfloat left, cfloat right) => left.Equals(right);
public static bool operator ==(ConstrainedFloat<TSelf> left, ConstrainedFloat<TSelf> right) => left.Equals(right);

public static bool operator !=(cfloat left, cfloat right) => !left.Equals(right);
public static bool operator !=(ConstrainedFloat<TSelf> left, ConstrainedFloat<TSelf> right) => !left.Equals(right);

public override bool Equals(object? obj) => obj is cfloat cfloat && Equals(cfloat);
public override bool Equals(object? obj) => obj is ConstrainedFloat<TSelf> cfloat && Equals(cfloat);

public bool Equals(cfloat other) => Value == other.Value;
public bool Equals(ConstrainedFloat<TSelf> other) => Value == other.Value;

public override int GetHashCode() => HashCode.Combine(Value, Min, Max, Range);

public override string ToString() => Value.ToString();

public static cfloat Parse (string s, IFormatProvider? provider) => float.Parse(s, provider);
public static TSelf Parse (string s, IFormatProvider? provider) => (ConstrainedFloat<TSelf>)float.Parse(s, provider);

public static bool TryParse (string? s, IFormatProvider? provider, out cfloat result)
public static bool TryParse (string? s, IFormatProvider? provider, out TSelf result)
{
var bResult = float.TryParse(s, Globalization.NumberStyles.Any, provider, out var floatResult);
result = bResult ? new (floatResult) : default;
result = bResult ? (TSelf)Activator.CreateInstance(typeof(TSelf), floatResult) : default;
return bResult;
}
}
2 changes: 1 addition & 1 deletion src/Dgmjr.Primitives.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<ItemGroup>
<Using Include="System.ConstrainedFloat" Alias="cfloat" />
<!-- <Using Include="System.ConstrainedFloat" Alias="cfloat" /> -->
<Using Include="System.Int24" Alias="i24"/>
<Using Include="System.UInt24" Alias="ui24"/>
<Using Include="System.DateOnly" Alias="date"/>
Expand Down
2 changes: 1 addition & 1 deletion src/PhoneNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace System.Domain;
/// </summary>
[ValueObject(typeof(string), conversions: Conversions.SystemTextJson | Conversions.TypeConverter)]
[StructLayout(LayoutKind.Auto)]
[PhoneNumber.JConverter]
// [PhoneNumber.JConverter]
public partial record struct PhoneNumber : IRegexValueObject<PhoneNumber>
{
/// <summary>
Expand Down

0 comments on commit 95c1c73

Please sign in to comment.