Skip to content

Add IComparableGenerator<T>.NextBetween(T low, T high, BoundaryConditions conditions) implementation #43

@carusology

Description

@carusology

Right now the IComparableGenerator<T> supports the following APIs:

NextGreaterThan(T other)
NextGreaterThanOrEqualTo(T other)
NextLessThan(T other)
NextLessThanOrEqualTo(T other)

These are great when comparing against a constraint that is a single value. However, it fails to support constraints that are ranges of values. Today, you cannot effectively get a number between two values. If you try to combine two calls of NextGreaterThanOrEqualTo() and NextLessThanOrEqualTo() (or NextGreaterThan() and NextLessThan(), or some combination thereof), you can end up with two values outside of your target range that you cannot consolidate. The two pieces of state (the upper and lower boundaries) need to be combined into a single call paired with a BoundaryConditions flags-enum value that clarifies whether the comparisons should be inclusive or exclusive with regard to the boundaries.

Here's a rough idea of something that could work:

[Flags]
public enum BoundaryConditions {

    InclusiveLower = 0,  // This means we default to 'inclusive'
    InclusiveUpper = 0,
    Inclusive = 0

    ExclusiveLower = 1
    ExclusiveUpper = 2
    Exclusive = 3

}

This would be used to create the following API that allow calls like the following:

interface IComparableGenerator<T> {
    T NextBetween(T low, T high, BoundaryConditions conditions = BoundaryConditions.Inclusive);
}

generator.NextBetween(low, high);

generator.NextBetween(
    low,
    high, 
    BoundaryConditions.InclusiveLower | BoundaryConditions.ExclusiveUpper
);

generator.NextBetween(
    low,
    high,
    BoundaryConditions.Exclusive
);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions