eclipse / eclipse-collections

Eclipse Collections is a collections framework for Java with optimized data structures and a rich, functional and fluent API.
https://eclipse.dev/collections/
2.43k stars 611 forks source link

reverse interval ranges #1570

Closed airtyon closed 4 months ago

airtyon commented 6 months ago

Closes: #1564

Hi @donraab,

In this PR, I tried to keep the changes as much consistent as possible.

Upon consideration, I think there might be a different way to approach this.

In my opinion we might consider allowing the direction of the interval to be determined by difference of from - to.

  1. We allow step to have different values and sign 1.a. Pro: Not invasive 1.b. Con: The API doesn't look nice. i.e. fromToBy(0, 10, -1)

  2. We allow step to only be positive 2.a. Pro: This would reflect more a "real life" situation, where having negative steps is not allowed. i.e. a car is moving in some direction with this many km/h. if the car reverses, the direction changes but the speed is still positive. 2.b. Con: Somehow invasive.

donraab commented 4 months ago

Hi @airtyon,

I like your suggestions (especially number one), but I would be hesitant to make any changes to Interval because it is very old and has a lot of usages. I will think a bit on your first suggestion. I can't think of any reason not to do it off hand, but need to stare at the code a bit more. I'd also like to add some more tests for the three Interval classes first. For small Intervals I think we should be testing more than size alone.

Thanks!

In my opinion we might consider allowing the direction of the interval to be determined by difference of from - to.

  1. We allow step to have different values and sign 1.a. Pro: Not invasive 1.b. Con: The API doesn't look nice. i.e. fromToBy(0, 10, -1)
  2. We allow step to only be positive 2.a. Pro: This would reflect more a "real life" situation, where having negative steps is not allowed. i.e. a car is moving in some direction with this many km/h. if the car reverses, the direction changes but the speed is still positive. 2.b. Con: Somehow invasive.