Issue Details (XML | Word | Printable)

Key: GROOVY-1616
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Paul King
Reporter: BarzilaiSpinak
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
groovy

Inconsistent subList for ranges

Created: 21/Dec/06 02:43 PM   Updated: 26/Sep/07 08:28 PM
Component/s: groovy-jdk
Affects Version/s: 1.0-JSR-6, 1.0-RC-1
Fix Version/s: 1.1-beta-2

Time Tracking:
Not Specified


 Description  « Hide
From the reply by blackdrag:
================
I think that maybe thi one:
groovy> (4..8).subList(0, 0) ===> 4..3

should be considered as bug..

bye blackdrag
================

For more details, here's my original message to the mailing list with the same Subject as this issue's Summary:

========= (on Dec 19 2006)

I've been playing with ranges and looking at the source code. I found the behaviour of "groovy.lang.IntRange.subList(int, int)" to be inconsistent with the corresponding behaviour in java.util.AbstractList.

From AbstractList:

public List<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.)

I'm concentrating on the last part, between parentheses.

In Java, this is also consistent with String.substring:

"01234".substring(2, 2); // returns "" (empty string)
"01234".substring(2, 4); // returns "23"
"01234".substring(2, 6); // StringIndexOutOfBoundsException

Now, in Groovy:
groovy> (4..8).subList(0, 2) ===> 4..5
groovy> (4..8).subList(0, 1) ===> 4..4
groovy> (4..8).subList(0, 0) ===> 4..3

Notice the last example! When fromIndex and toIndex are the same, it returns a reversed list including the "previous" item. In this case, this previous item (number 3) wasn't even part of the original list!

Now, this may have been intended behaviour in Groovy and it may have been discussed to death in the list.. I don't know

There are other combinations of ranges, lists, strings, ranges of lists, list of ranges, etc.. that do not all give the same results.

BarZ



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Paul King added a comment - 22/Dec/06 05:14 PM
This might not be as bad as it first appears, perhaps not a bug, just behaviour that is a little puzzling:
IntRange r = (4..8).subList(0,0)
println "$r.from, $r.to, ${r.size()}, ${r.isReverse()}, $r"
// --> 4, 3, 0, false, []
r = (4..3)
println "$r.from, $r.to, ${r.size()}, ${r.isReverse()}, $r"
// --> 3, 4, 2, true, [4, 3]

Paul King added a comment - 11/Jun/07 04:05 AM
After recent changes we now have the following behaviour:

def x = (4..8).subList(0, 0)
println x
// => []
println x.isEmpty()
// => true
println x.class
// => class groovy.lang.EmptyRange


Paul King added a comment - 26/Sep/07 08:28 PM
No further feedback, assuming fixed