Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8271865

SortedList::getViewIndex behaves not correctly for some index values

XMLWordPrintable

    • b17
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Method
      public int getViewIndex(int index)
      of class javafx.collections.transformation.SortedList<E>
      behaves erroneously under certain conditions

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See provided JUnit test method

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      SortedList.getViewIndex(int index) returns a negative value if index is outside of the list
      ACTUAL -
      SortedList.getViewIndex(int index) returns zero or throws AIOOBE for some index values outside of the list

      --- Output of Test Case ---
      Source list : [4, 1, 3, 2]
      Sorted list : [1, 2, 3, 4]
      sortedList.getViewIndex(0) = 3
      sortedList.getViewIndex(1) = 0
      sortedList.getViewIndex(2) = 2
      sortedList.getViewIndex(3) = 1
      sortedList.getViewIndex(4) = 0
      sortedList.getViewIndex(5) = 0
      sortedList.getViewIndex(6) = 0
      Index 7 out of bounds for length 7
      Index 8 out of bounds for length 7
      Index 9 out of bounds for length 7


      ---------- BEGIN SOURCE ----------
      @Test
      public void test_SortedList_getViewIndex()
      {
      final var sourceList = FXCollections.observableArrayList(4, 1, 3, 2);
      final var sortedList = new SortedList<>(sourceList, Integer::compare);
      System.out.println("Source list : " + sourceList);
      System.out.println("Sorted list : " + sortedList);
      assertEquals(3, sortedList.getViewIndex(0));
      assertEquals(0, sortedList.getViewIndex(1));
      assertEquals(2, sortedList.getViewIndex(2));
      assertEquals(1, sortedList.getViewIndex(3));
      for (int i = 0; i < 10; i++)
      {
      try
      {
      System.out.println("sortedList.getViewIndex(" + i + ") = " + sortedList.getViewIndex(i));
      }
      catch (IndexOutOfBoundsException ex)
      {
      System.out.println(ex.getMessage());
      }
      }
      // the following tests fail:
      assertTrue(sortedList.getViewIndex(4) < 0);
      assertTrue(sortedList.getViewIndex(5) < 0);
      assertTrue(sortedList.getViewIndex(6) < 0);
      assertTrue(sortedList.getViewIndex(7) < 0);
      assertTrue(sortedList.getViewIndex(8) < 0);
      assertTrue(sortedList.getViewIndex(9) < 0);
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Check index before calling the getViewIndex(int index) method

      FREQUENCY : always


            kcr Kevin Rushforth
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: