changeset 6364:0f187cdcacb3

Garbage-collect unneeded stream building abstractions; generics and duplication cleanup in ArrayList
author briangoetz
date Thu, 01 Nov 2012 15:37:59 -0400
parents 0afeaddee9cf
children 0fe3dad5e617
files src/share/classes/java/util/ArrayList.java src/share/classes/java/util/streams/Streams.java
diffstat 2 files changed, 10 insertions(+), 202 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/ArrayList.java	Thu Nov 01 12:19:07 2012 -0400
+++ b/src/share/classes/java/util/ArrayList.java	Thu Nov 01 15:37:59 2012 -0400
@@ -1137,23 +1137,20 @@
     }
 
 
+    private class ArrayProxyImpl<E> implements Streams.ArrayProxy<E> {
+        public E[] getArray() { return (E[]) ArrayList.this.elementData; }
+        public int getOffset() { return 0; }
+        public int getLen() { return ArrayList.this.size; }
+        public int getModCount() { return ArrayList.this.modCount;  }
+    }
+
     @Override
     public Stream<E> stream() {
-        return Streams.stream(new Streams.ArrayProxy() {
-                public E[] getArray() { return (E[]) ArrayList.this.elementData; }
-                public int getOffset() { return 0; }
-                public int getLen() { return ArrayList.this.size; }
-                public int getModCount() { return ArrayList.this.modCount;  }
-                });
+        return Streams.stream(new ArrayProxyImpl<E>());
     }
 
     @Override
     public Stream<E> parallel() {
-        return Streams.parallel(new Streams.ArrayProxy() {
-                public E[] getArray() { return (E[]) ArrayList.this.elementData; }
-                public int getOffset() { return 0; }
-                public int getLen() { return ArrayList.this.size; }
-                public int getModCount() { return ArrayList.this.modCount;  }
-                });
+        return Streams.parallel(new ArrayProxyImpl<E>());
     }
 }
--- a/src/share/classes/java/util/streams/Streams.java	Thu Nov 01 12:19:07 2012 -0400
+++ b/src/share/classes/java/util/streams/Streams.java	Thu Nov 01 15:37:59 2012 -0400
@@ -61,46 +61,10 @@
         return new ValuePipeline<>(new IterableStreamAccessor<>(source, sizeOrUnknown, flags));
     }
 
-    public static<T> Stream<T> stream(Iterator<T> source) {
-        return new ValuePipeline<>(new IteratorStreamAccessor<>(source));
-    }
-
-    public static<T> Stream<T> stream(Iterator<T> source, int sizeOrUnknown) {
-        return new ValuePipeline<>(new IteratorStreamAccessor<>(source, sizeOrUnknown));
-    }
-
-    public static<T> Stream<T> stream(Iterator<T> source, int sizeOrUnknown, int flags) {
-        return new ValuePipeline<>(new IteratorStreamAccessor<>(source, sizeOrUnknown, flags));
-    }
-
-    public static <T, L extends RandomAccess & List<T>> Spliterator<T> spliterator(L source) {
-        return new RandomAccessListSpliterator<>(source);
-    }
-
-    public static <T, L extends RandomAccess & List<T>> Spliterator<T> spliterator(L source, int offset, int length) {
-        return new RandomAccessListSpliterator<>(source, offset, length);
-    }
-
-    public static <T, L extends RandomAccess & List<T>> Stream<T> stream(L source) {
-        return new ValuePipeline<>(new RandomAccessListStreamAccessor<T,L>(source));
-    }
-
-    public static <T, L extends RandomAccess & List<T>> Stream<T> stream(L source, int offset, int length) {
-        return new ValuePipeline<>(new RandomAccessListStreamAccessor<>(source, offset, length));
-    }
-
     public static <T> Stream<T> stream(ArrayProxy<T> proxy) {
         return new ValuePipeline<>(new ArrayProxyStreamAccessor<>(proxy));
     }
 
-    public static <T, L extends RandomAccess & List<T>> Stream<T> parallel(L source) {
-        return new ValuePipeline<>(new RandomAccessListParallelStreamAccessor<>(source));
-    }
-
-    public static <T, L extends RandomAccess & List<T>> Stream<T> parallel(L source, int offset, int length) {
-        return new ValuePipeline<>(new RandomAccessListParallelStreamAccessor<>(source, offset, length));
-    }
-
     public static <T> Spliterator<T> spliterator(T[] source) {
         return new ArraySpliterator<>(source);
     }
@@ -110,7 +74,7 @@
     }
 
     public static <T> Stream<T> stream(T[] source) {
-        return new ValuePipeline<>(new ArrayStreamAccessor<T>(source));
+        return new ValuePipeline<>(new ArrayStreamAccessor<>(source));
     }
 
     public static <T> Stream<T> stream(T[] source, int offset, int length) {
@@ -767,28 +731,6 @@
         }
     }
 
-    private static class RandomAccessListIterator<T, L extends RandomAccess & List<T>>  extends RandomAccessIterator<T> {
-        protected final L elements;
-
-        private RandomAccessListIterator(L elements, int startOffset, int len) {
-            super(startOffset, len);
-
-            assert (startOffset < elements.size()) || (0 == len && 0 == startOffset) : "offset not in list";
-            assert endOffset <= elements.size() : "end not in list";
-
-            this.elements = Objects.requireNonNull(elements);
-        }
-
-        @Override
-        public T next() {
-            if(!hasNext()) {
-                throw new NoSuchElementException();
-            }
-
-            return elements.get(offset++);
-        }
-    }
-
     private static class ArraySpliterator<T> extends ArrayIterator<T> implements Spliterator<T> {
         boolean traversing = false;
 
@@ -848,65 +790,6 @@
         }
     }
 
-    private static class RandomAccessListSpliterator<T, L extends RandomAccess & List<T>> extends RandomAccessListIterator<T,L> implements Spliterator<T> {
-        boolean traversing = false;
-
-        RandomAccessListSpliterator(L elements) {
-            this(elements, 0, elements.size());
-        }
-
-        RandomAccessListSpliterator(L elements, int offset, int length) {
-            super(elements, offset, length);
-        }
-
-        @Override
-        public int getSizeIfKnown() {
-            return endOffset - offset;
-        }
-
-        @Override
-        public int estimateSize() {
-            return getSizeIfKnown();
-        }
-
-        @Override
-        public boolean isPredictableSplits() {
-            return true;
-        }
-
-        @Override
-        public void forEach(Block<? super T> sink) {
-            traversing = true;
-            for (int i=offset; i<endOffset; i++) {
-                sink.apply(elements.get(i));
-            }
-            // update only once; reduce heap write traffic
-            offset = endOffset;
-        }
-
-        @Override
-        public int getNaturalSplits() {
-            return (endOffset - offset > 1) ? 1 : 0;
-        }
-
-        @Override
-        public Spliterator<T> split() {
-            if (traversing) {
-                throw new IllegalStateException("split after starting traversal");
-            }
-            int t = (endOffset - offset) / 2;
-            Spliterator<T> ret = new RandomAccessListSpliterator<>(elements, offset, t);
-            offset += t;
-            return ret;
-        }
-
-        @Override
-        public Iterator<T> iterator() {
-            traversing = true;
-            return this;
-        }
-    }
-
     private static class ArrayStreamAccessor<T>
             extends ArrayIterator<T> implements StreamAccessor<T> {
 
@@ -980,76 +863,4 @@
         }
     }
 
-    private static class RandomAccessListStreamAccessor<T,L extends RandomAccess & List<T>>
-            extends RandomAccessListIterator<T,L> implements StreamAccessor<T> {
-
-        RandomAccessListStreamAccessor(L elements) {
-            this(elements, 0, elements.size());
-        }
-
-        RandomAccessListStreamAccessor(L elements, int offset, int length) {
-            super(elements, offset, length);
-        }
-
-        @Override
-        public void forEach(Block<? super T> sink) {
-            for (int i=offset; i<endOffset; i++) {
-                sink.apply(elements.get(i));
-            }
-            // update only once; reduce heap write traffic
-            offset = endOffset;
-        }
-
-        @Override
-        public int getStreamFlags() {
-            return StreamOpFlags.IS_SIZED | StreamOpFlags.IS_ORDERED;
-        }
-
-        @Override
-        public boolean isParallel() {
-            return false;
-        }
-
-        @Override
-        public Iterator<T> iterator() {
-            return this;
-        }
-
-        @Override
-        public Spliterator<T> spliterator() {
-            throw new UnsupportedOperationException();
-        }
-    }
-
-    private static class RandomAccessListParallelStreamAccessor<T,L extends RandomAccess & List<T>>
-            extends RandomAccessListSpliterator<T,L> implements StreamAccessor<T> {
-
-        RandomAccessListParallelStreamAccessor(L elements) {
-            this(elements, 0, elements.size());
-        }
-
-        RandomAccessListParallelStreamAccessor(L elements, int offset, int length) {
-            super(elements, offset, length);
-        }
-
-        @Override
-        public int getStreamFlags() {
-            return StreamOpFlags.IS_SIZED | StreamOpFlags.IS_ORDERED;
-        }
-
-        @Override
-        public boolean isParallel() {
-            return true;
-        }
-
-        @Override
-        public Iterator<T> iterator() {
-            return this;
-        }
-
-        @Override
-        public Spliterator<T> spliterator() {
-            return this;
-        }
-    }
 }