eclipse / eclipse-collections

Eclipse Collections is a collections framework for Java with optimized data structures and a rich, functional and fluent API.
http://www.eclipse.org/collections
2.39k stars 596 forks source link

Interval types do not support reverse ranges for certain helper methods #1564

Closed donraab closed 2 months ago

donraab commented 3 months ago

A few Interval helper methods are missing support for reverse ranges, which are ranges where the from value is greater than the to value, which would require the by value to be negative.

Classes: Interval, IntInterval, LongInterval

The following examples throw exceptions today:

  1. zeroTo
Interval.zeroTo(-10);

// java.lang.IllegalArgumentException: Step by is incorrect for the range

This code should result in: Interval.fromToBy(0, -10, -1)

  1. to
Interval.from(10).to(-10);

// java.lang.IllegalArgumentException: Step by is incorrect for the range

This code should result in: Interval.fromToBy(10, -10, -1)

  1. oneTo
Interval.oneTo(-10); 

// java.lang.IllegalArgumentException: Only positive ranges allowed using oneToBy

This exception looks like it was intentional but I think if we fix the other two, this one should be changed to be consistent. This code should result in: Interval.fromToBy(1, -10, -1)

Failing tests should be written to cover all missing scenarios first. I was not exhaustive in my search, so there may be other helper methods that do not support reverse ranges.

airtyon commented 3 months ago

hi @donraab, may I give this one a try?

donraab commented 3 months ago

Hi @airtyon, yes, thank you! I will assign it to you.