changeset 257:83b0ae82038b

RT-18904: Performance optimisation to ReadOnlyUnbackedList.subList
author jgiles
date Fri, 13 Jan 2012 10:19:07 +1300
parents 056d61a3b3b4
children e4d7464ce84a
files javafx-ui-controls/src/com/sun/javafx/scene/control/ReadOnlyUnbackedObservableList.java
diffstat 1 files changed, 11 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/com/sun/javafx/scene/control/ReadOnlyUnbackedObservableList.java	Fri Jan 13 08:10:21 2012 +1300
+++ b/javafx-ui-controls/src/com/sun/javafx/scene/control/ReadOnlyUnbackedObservableList.java	Fri Jan 13 10:19:07 2012 +1300
@@ -37,7 +37,6 @@
 
 import com.sun.javafx.collections.ListInvalidationListenerWrapper;
 import com.sun.javafx.collections.ListenerList;
-import java.util.ArrayList;
 import java.util.Collections;
 
 /**
@@ -97,7 +96,6 @@
         }
     }
 
-
     @Override public int indexOf(Object o) {
         if (o == null) return -1;
 
@@ -154,13 +152,18 @@
      * NOTE: This method does not fulfill the subList contract from Collections,
      * it simply returns a list containing the values in the given range.
      */
-    @Override public List<T> subList(int fromIndex, int toIndex) {
+    @Override public List<T> subList(final int fromIndex, final int toIndex) {
         if (fromIndex >= toIndex) return Collections.emptyList();
-        List<T> sublist = new ArrayList<T>();
-        for (int i = fromIndex; i < toIndex; i++) {
-            sublist.add(get(i));
-        }
-        return sublist;
+        final List<T> outer = this;
+        return new ReadOnlyUnbackedObservableList<T>() {
+            @Override public T get(int i) {
+                return outer.get(i + fromIndex);
+            }
+
+            @Override public int size() {
+                return toIndex - fromIndex;
+            }
+        };
     }
 
     @Override