From 95c1c73611c177cd0a061784194fb379f868ea45 Mon Sep 17 00:00:00 2001 From: "David G. Moore, Jr." Date: Tue, 27 Feb 2024 05:55:12 -0500 Subject: [PATCH] Create ConstrainedFloat & remove redundant JConverter from PhoneNumber --- src/ConstrainedFloat.cs | 28 +++++++++++++++------------- src/Dgmjr.Primitives.props | 2 +- src/PhoneNumber.cs | 2 +- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/ConstrainedFloat.cs b/src/ConstrainedFloat.cs index 08f52cb..30d18cf 100644 --- a/src/ConstrainedFloat.cs +++ b/src/ConstrainedFloat.cs @@ -1,15 +1,16 @@ namespace System; using System.Numerics; -public readonly struct ConstrainedFloat +public abstract class ConstrainedFloat #if NET7_0_OR_GREATER - : IParsable + : IParsable #endif + where TSelf : ConstrainedFloat, 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) { @@ -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; @@ -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 cfloat) => cfloat.Value; - public static implicit operator cfloat(float value) => new(value, float.MinValue, float.MaxValue); + public static implicit operator ConstrainedFloat(float value) => (TSelf)Activator.CreateInstance(typeof(TSelf), value); + public static implicit operator TSelf(ConstrainedFloat value) => (TSelf)value; - public static bool operator ==(cfloat left, cfloat right) => left.Equals(right); + public static bool operator ==(ConstrainedFloat left, ConstrainedFloat right) => left.Equals(right); - public static bool operator !=(cfloat left, cfloat right) => !left.Equals(right); + public static bool operator !=(ConstrainedFloat left, ConstrainedFloat 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 cfloat && Equals(cfloat); - public bool Equals(cfloat other) => Value == other.Value; + public bool Equals(ConstrainedFloat 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)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; } } diff --git a/src/Dgmjr.Primitives.props b/src/Dgmjr.Primitives.props index ac66fb3..6a731ce 100644 --- a/src/Dgmjr.Primitives.props +++ b/src/Dgmjr.Primitives.props @@ -1,6 +1,6 @@ - + diff --git a/src/PhoneNumber.cs b/src/PhoneNumber.cs index 79d27a1..f3a32a4 100644 --- a/src/PhoneNumber.cs +++ b/src/PhoneNumber.cs @@ -33,7 +33,7 @@ namespace System.Domain; /// [ValueObject(typeof(string), conversions: Conversions.SystemTextJson | Conversions.TypeConverter)] [StructLayout(LayoutKind.Auto)] -[PhoneNumber.JConverter] +// [PhoneNumber.JConverter] public partial record struct PhoneNumber : IRegexValueObject { ///