Skip to content

Commit

Permalink
refactor(ES.FX): move ValueRange to Primitives and simplify structure
Browse files Browse the repository at this point in the history
- Moved `ValueRange` class from `ValueRanges` folder to `Primitives`.
- Changed `ValueRange` from an abstract record to a readonly record struct.
- Removed the need for derived classes by eliminating the abstract method `CreateFor`.
- Simplified comparison logic in the `CompareTo` method.
- Deleted specific range classes (`DateTimeOffsetRange`, `DateTimeRange`, `DoubleRange`, `IntRange`, and `TimeSpanRange`) as they are no longer needed due to refactoring of the base class.
  • Loading branch information
winromulus committed Sep 23, 2024
1 parent 7459b3e commit 02dda61
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 60 deletions.
4 changes: 4 additions & 0 deletions src/ES.FX/ES.FX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<PackageReference Include="JetBrains.Annotations" />
</ItemGroup>

<ItemGroup>
<Folder Include="ValueRanges\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace ES.FX.ValueRanges;
namespace ES.FX.Primitives;

public abstract record ValueRange<T> where T : IComparable<T>
public readonly record struct ValueRange<T> where T : IComparable<T>
{
protected ValueRange(T min, T max)
public ValueRange(T min, T max)
{
if (min.CompareTo(max) > 0)
throw new ArgumentException("Min cannot be greater than Max.");
Expand All @@ -21,18 +21,16 @@ protected ValueRange(T min, T max)
ArgumentNullException.ThrowIfNull(other);
var newMin = Min.CompareTo(other.Min) > 0 ? Min : other.Min;
var newMax = Max.CompareTo(other.Max) < 0 ? Max : other.Max;
return newMin.CompareTo(newMax) <= 0 ? CreateFor(newMin, newMax) : null;
return newMin.CompareTo(newMax) <= 0 ? new ValueRange<T>(newMin, newMax) : null;
}

protected abstract ValueRange<T> CreateFor(T min, T max);

public override string ToString() => $"[{Min}, {Max}]";

public int CompareTo(ValueRange<T>? other)
{
if (other is null) return 1;

var minComparison = Min.CompareTo(other.Min);
return minComparison != 0 ? minComparison : Max.CompareTo(other.Max);
var minComparison = Min.CompareTo(other.Value.Min);
return minComparison != 0 ? minComparison : Max.CompareTo(other.Value.Max);
}
}
11 changes: 0 additions & 11 deletions src/ES.FX/ValueRanges/DateTimeOffsetRange.cs

This file was deleted.

10 changes: 0 additions & 10 deletions src/ES.FX/ValueRanges/DateTimeRange.cs

This file was deleted.

10 changes: 0 additions & 10 deletions src/ES.FX/ValueRanges/DoubleRange.cs

This file was deleted.

10 changes: 0 additions & 10 deletions src/ES.FX/ValueRanges/IntRange.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/ES.FX/ValueRanges/TimeSpanRange.cs

This file was deleted.

0 comments on commit 02dda61

Please sign in to comment.