Skip to content

Refactor: The domain extension methods have been refactored for improving overall codebase#4

Merged
blaze6950 merged 1 commit intomainfrom
refactor/refactor-slightly-in-the-scope-of-real-usage
Feb 9, 2026
Merged

Refactor: The domain extension methods have been refactored for improving overall codebase#4
blaze6950 merged 1 commit intomainfrom
refactor/refactor-slightly-in-the-scope-of-real-usage

Conversation

@blaze6950
Copy link
Owner

This pull request refactors and centralizes range domain operations for the Intervals.NET library, improving code maintainability and ensuring consistent performance semantics. The main change is the introduction of a new internal helper class, RangeDomainOperations, which consolidates shared logic for span calculation, shifting, and expansion of ranges. Public extension methods in both common and fixed-step domain extension classes are updated to delegate their implementation to this internal class, reducing code duplication and clarifying performance guarantees.

Centralization of range domain operations:

  • Introduced the internal static class RangeDomainOperations in src/Domain/Intervals.NET.Domain.Extensions/Internal/RangeDomainOperations.cs, which implements shared logic for CalculateSpan, Shift, Expand, and ExpandByRatio methods for both fixed and variable-step domains. This ensures consistent behavior and performance semantics across domain types.

Refactoring of public extension methods:

  • Updated Span, ExpandByRatio, Shift, and Expand methods in src/Domain/Intervals.NET.Domain.Extensions/Fixed/RangeDomainExtensions.cs to delegate their implementation to the corresponding methods in RangeDomainOperations, simplifying the code and reducing duplication. [1] [2]
  • Removed the now-redundant CommonRangeDomainExtensions class from src/Domain/Intervals.NET.Domain.Extensions/CommonRangeDomainExtensions.cs, as its logic is fully covered by the new internal helper class.

Documentation improvements:

  • Enhanced XML documentation for ExpandByRatio in RangeDomainExtensions, clarifying the meaning and usage of ratio coefficients and providing examples for negative ratios. [1] [2]

These changes improve maintainability, clarify performance characteristics, and make the codebase easier to extend and reason about.

…ormance, introducing internal operations for span, shift, and expand functionalities.
@blaze6950 blaze6950 self-assigned this Feb 9, 2026
Copilot AI review requested due to automatic review settings February 9, 2026 21:23
@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 76.74419% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...omain.Extensions/Internal/RangeDomainOperations.cs 71.42% 0 Missing and 10 partials ⚠️

📢 Thoughts on this report? Let us know!

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors Intervals.NET domain extension methods by centralizing shared range-domain logic into a new internal helper (RangeDomainOperations), then updating fixed/variable extension APIs and tests to use the centralized implementation.

Changes:

  • Added Internal.RangeDomainOperations to consolidate Span/Shift/Expand/ExpandByRatio logic.
  • Updated Fixed/Variable RangeDomainExtensions to delegate implementations to the internal helper (and added Shift/Expand APIs).
  • Removed CommonRangeDomainExtensions (and its tests), expanded test coverage for Shift/Expand, and bumped Domain.Extensions package version.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/Intervals.NET.Domain.Extensions.Tests/Variable/RangeDomainExtensionsTests.cs Adds Shift/Expand tests for variable-step business-day domains.
tests/Intervals.NET.Domain.Extensions.Tests/Fixed/RangeDomainExtensionsTests.cs Adds Shift/Expand tests for fixed-step integer domain.
tests/Intervals.NET.Domain.Extensions.Tests/CommonRangeDomainExtensionsTests.cs Removes tests for deleted CommonRangeDomainExtensions API.
src/Intervals.NET.Data/RangeData.cs Removes an Intervals.NET using (currently breaks compilation due to Range<>/RangeValue<> usage).
src/Intervals.NET.Data/Extensions/RangeDataExtensions.cs Removes an Intervals.NET using (currently breaks compilation due to Range<> usage).
src/Intervals.NET.Data/Extensions/EnumerableExtensions.cs Removes an Intervals.NET using (currently breaks compilation due to Range<> usage).
src/Domain/Intervals.NET.Domain.Extensions/Variable/RangeDomainExtensions.cs Refactors Span/ExpandByRatio and adds Shift/Expand, delegating to internal operations; Span return type changes.
src/Domain/Intervals.NET.Domain.Extensions/Intervals.NET.Domain.Extensions.csproj Bumps package version from 0.0.2 to 0.0.3.
src/Domain/Intervals.NET.Domain.Extensions/Internal/RangeDomainOperations.cs New internal centralized implementation for Span/Shift/Expand/ExpandByRatio.
src/Domain/Intervals.NET.Domain.Extensions/Fixed/RangeDomainExtensions.cs Refactors Span/ExpandByRatio and adds Shift/Expand, delegating to internal operations.
src/Domain/Intervals.NET.Domain.Extensions/CommonRangeDomainExtensions.cs Removes the previously public performance-agnostic extension API.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -1,5 +1,4 @@
using Intervals.NET;
using Intervals.NET.Domain.Abstractions;
using Intervals.NET.Domain.Abstractions;
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing using Intervals.NET; breaks compilation because this file references Range<...> and RangeValue<...> types from the Intervals.NET namespace (e.g., RangeData(Range<TRangeType> range, ...)). Add the using back (or fully-qualify those types).

Suggested change
using Intervals.NET.Domain.Abstractions;
using Intervals.NET.Domain.Abstractions;
using Intervals.NET;

Copilot uses AI. Check for mistakes.
@@ -1,5 +1,4 @@
using System.Runtime.CompilerServices;
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing using Intervals.NET; breaks compilation because this file uses Range<...> (e.g., in method signatures and examples) which lives in the Intervals.NET namespace. Add the using back (or fully-qualify Intervals.NET.Range<T>).

Suggested change
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using Intervals.NET;

Copilot uses AI. Check for mistakes.
@@ -1,4 +1,3 @@
using Intervals.NET;
using Intervals.NET.Domain.Abstractions;
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing using Intervals.NET; breaks compilation because this file references Range<TRangeType> in ToRangeData(...), which is in the Intervals.NET namespace. Add the using back (or fully-qualify the type).

Suggested change
using Intervals.NET.Domain.Abstractions;
using Intervals.NET.Domain.Abstractions;
using Intervals.NET;

Copilot uses AI. Check for mistakes.
Comment on lines +102 to 105
public static RangeValue<long> Span<TRangeValue, TDomain>(this Range<TRangeValue> range, TDomain domain)
where TRangeValue : IComparable<TRangeValue>
where TDomain : IVariableStepDomain<TRangeValue>
{
// If either boundary is unbounded in the direction that expands the range, span is infinite
if (range.Start.IsNegativeInfinity || range.End.IsPositiveInfinity)
{
return RangeValue<double>.PositiveInfinity;
}

var firstStep = CalculateFirstStep(range, domain);
var lastStep = CalculateLastStep(range, domain);

switch (firstStep.CompareTo(lastStep))
{
// After domain alignment, boundaries can cross (e.g., open range smaller than one step)
// Example: (Jan 1 00:00, Jan 1 00:01) with day domain -> firstStep=Jan 2, lastStep=Dec 31
case > 0:
return 0.0;
case 0:
return HandleSingleStepCase(range, domain);
}

// Note: IRangeDomain.Distance returns long, but for variable-step domains we interpret
// this as the number of complete steps and add 1.0 to get the span including boundaries
var distance = (double)domain.Distance(firstStep, lastStep);
return distance + 1.0;

// Local functions
static TRangeValue CalculateFirstStep(Range<TRangeValue> r, TDomain d)
{
if (r.IsStartInclusive)
{
// Include boundary: use floor to include the step we're on/in
return d.Floor(r.Start.Value);
}

// Exclude boundary: floor to get the boundary, then add 1 to skip it
var flooredStart = d.Floor(r.Start.Value);
return d.Add(flooredStart, 1);
}

static TRangeValue CalculateLastStep(Range<TRangeValue> r, TDomain d)
{
if (r.IsEndInclusive)
{
// Include boundary: use floor to include the step we're on/in
return d.Floor(r.End.Value);
}

// Exclude boundary: floor to get the boundary, then subtract 1 to exclude it
var flooredEnd = d.Floor(r.End.Value);
return d.Add(flooredEnd, -1);
}

static double HandleSingleStepCase(Range<TRangeValue> r, TDomain d)
{
// If both floor to the same step, check if either bound is actually ON that step
var startIsOnBoundary = d.Floor(r.Start.Value).CompareTo(r.Start.Value) == 0;
var endIsOnBoundary = d.Floor(r.End.Value).CompareTo(r.End.Value) == 0;

if (r is { IsStartInclusive: true, IsEndInclusive: true } && (startIsOnBoundary || endIsOnBoundary))
{
return 1.0;
}

// Otherwise, they're in between domain steps, return 0
return 0.0;
}
}
where TDomain : IVariableStepDomain<TRangeValue> => Internal.RangeDomainOperations.CalculateSpan(range, domain);

Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Span changed from RangeValue<double> to RangeValue<long> for variable-step domains. This is a breaking public API change and currently makes existing call sites/tests that compare against doubles (e.g., Assert.Equal(5.0, span.Value)) fail to compile. If the change is intentional, update downstream usages/tests and consider calling it out prominently in release notes; otherwise, keep the return type consistent with prior versions.

Copilot uses AI. Check for mistakes.
@blaze6950 blaze6950 merged commit 8bedbb3 into main Feb 9, 2026
10 checks passed
@blaze6950 blaze6950 deleted the refactor/refactor-slightly-in-the-scope-of-real-usage branch February 9, 2026 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants