Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-1616

Inconsistent subList for ranges

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-JSR-6, 1.0-RC-1
    • 1.1-beta-2
    • groovy-jdk
    • None

    Description

      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

      Attachments

        Activity

          People

            paulk Paul King
            barspi Barzilai Spinak
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: