changeset 6387:969458b5ee9d

Garbage-collect dead Iterators methods; move filter/map methods from {Filter,Map}Op to Iterators; add Iterators.asEnumeration
author briangoetz
date Mon, 12 Nov 2012 12:45:50 -0500
parents 62f516761797
children e8517ea52d33
files src/share/classes/java/util/Iterators.java src/share/classes/java/util/concurrent/ForkJoinUtils.java src/share/classes/java/util/streams/ops/FilterOp.java src/share/classes/java/util/streams/ops/MapOp.java test-ng/tests/org/openjdk/tests/java/util/IteratorsNullTest.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/FilterOpTest.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/MapOpTest.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/TeeOpTest.java
diffstat 8 files changed, 79 insertions(+), 276 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Iterators.java	Mon Nov 12 10:55:20 2012 +0100
+++ b/src/share/classes/java/util/Iterators.java	Mon Nov 12 12:45:50 2012 -0500
@@ -37,91 +37,6 @@
         throw new Error("No instances for you!");
     }
 
-    public static<T> T getFirst(Iterator<? extends T> iterator) {
-        if (!iterator.hasNext())
-            return null;
-
-        T next = iterator.next();
-        if (next == null)
-            throw new NullPointerException();
-        else
-            return next;
-    }
-
-    public static<T> T getOnly(Iterator<? extends T> iterator) {
-        if (!iterator.hasNext())
-            throw new NoSuchElementException();
-
-        T next = iterator.next();
-        if (next == null)
-            throw new NullPointerException();
-
-        if (iterator.hasNext())
-            throw new IllegalStateException();
-
-        return next;
-    }
-
-    public static <T> T reduce(final Iterator<? extends T> iterator, T base, final BinaryOperator<T> operator) {
-        Objects.requireNonNull(iterator);
-        Objects.requireNonNull(operator);
-        while(iterator.hasNext()) {
-            base = operator.operate(base, iterator.next());
-        }
-
-        return base;
-    }
-
-    public static <U, T> U mapReduce(Iterator<? extends T> iterator, Mapper<? extends U, ? super T> mapper, U base, BinaryOperator<U> operator) {
-        Objects.requireNonNull(iterator);
-        Objects.requireNonNull(mapper);
-        Objects.requireNonNull(operator);
-        while(iterator.hasNext()) {
-            base = operator.operate(base, mapper.map(iterator.next()));
-        }
-
-        return base;
-    }
-
-    public static <T> int mapReduce(Iterator<? extends T> iterator, IntMapper<? super T> mapper, int base, IntBinaryOperator operator) {
-        Objects.requireNonNull(iterator);
-        Objects.requireNonNull(mapper);
-        Objects.requireNonNull(operator);
-        while(iterator.hasNext()) {
-            base = operator.eval(base, mapper.map(iterator.next()));
-        }
-
-        return base;
-    }
-
-    public static <T> long mapReduce(Iterator<? extends T> iterator, LongMapper<? super T> mapper, long base, LongBinaryOperator operator) {
-        Objects.requireNonNull(iterator);
-        Objects.requireNonNull(mapper);
-        Objects.requireNonNull(operator);
-        while(iterator.hasNext()) {
-            base = operator.eval(base, mapper.map(iterator.next()));
-        }
-
-        return base;
-     }
-
-    public static <T> double mapReduce(Iterator<? extends T> iterator, DoubleMapper<? super T> mapper, double base, DoubleBinaryOperator operator) {
-        Objects.requireNonNull(iterator);
-        Objects.requireNonNull(mapper);
-        Objects.requireNonNull(operator);
-        while(iterator.hasNext()) {
-            base = operator.eval(base, mapper.map(iterator.next()));
-        }
-
-        return base;
-    }
-
-    // @@@ toString
-    // @@@ concat
-    // @@@ zip
-
-    // @@@ Overloaded concat methods for various inputs
-
     public static <T> Iterator<T> concat(final Iterator<? extends T> i1, final Iterator<? extends T> i2) {
         Objects.requireNonNull(i1);
         Objects.requireNonNull(i2);
@@ -175,4 +90,63 @@
             }
         };
     }
+
+    public static<T> Iterator<T> filter(final Iterator<T> source, final Predicate<? super T> predicate) {
+        Objects.requireNonNull(source);
+        Objects.requireNonNull(predicate);
+        return new Iterator<T>() {
+            boolean nextReady = false;
+            T nextValue = null;
+
+            @Override
+            public boolean hasNext() {
+                while (!nextReady && source.hasNext()) {
+                    nextValue = source.next();
+                    nextReady = predicate.test(nextValue);
+                }
+
+                return nextReady;
+            }
+
+            @Override
+            public T next() {
+                if (nextReady || hasNext()) {
+                    nextReady = false;
+                    return nextValue;
+                }
+
+                throw new NoSuchElementException();
+            }
+        };
+    }
+
+    public static<T, R> Iterator<R> map(final Iterator<T> source, final Mapper<? extends R, ? super T> mapper) {
+        Objects.requireNonNull(source);
+        Objects.requireNonNull(mapper);
+        return new Iterator<R>() {
+            @Override
+            public boolean hasNext() {
+                return source.hasNext();
+            }
+
+            @Override
+            public R next() {
+                return mapper.map(source.next());
+            }
+        };
+    }
+
+    public static<T> Enumeration<T> asEnumeration(Iterator<T> iterator) {
+        return new Enumeration<T>() {
+            @Override
+            public boolean hasMoreElements() {
+                return iterator.hasNext();
+            }
+
+            @Override
+            public T nextElement() {
+                return iterator.next();
+            }
+        };
+    }
 }
--- a/src/share/classes/java/util/concurrent/ForkJoinUtils.java	Mon Nov 12 10:55:20 2012 +0100
+++ b/src/share/classes/java/util/concurrent/ForkJoinUtils.java	Mon Nov 12 12:45:50 2012 -0500
@@ -51,16 +51,6 @@
 
     private ForkJoinUtils() {} // no construction
 
-    public static int suggestDepth(long s) {
-        long leafSize = suggestTargetSize(s);
-        int d = 0;
-        while (s > leafSize) {
-            s /= 2;
-            ++d;
-        }
-        return d;
-    }
-
     public static long suggestTargetSize(long s) {
         return 1 + ((s + 7) >>> 3) / ForkJoinPool.getCommonPoolParallelism();
     }
--- a/src/share/classes/java/util/streams/ops/FilterOp.java	Mon Nov 12 10:55:20 2012 +0100
+++ b/src/share/classes/java/util/streams/ops/FilterOp.java	Mon Nov 12 12:45:50 2012 -0500
@@ -25,7 +25,7 @@
 package java.util.streams.ops;
 
 import java.util.Iterator;
-import java.util.NoSuchElementException;
+import java.util.Iterators;
 import java.util.Objects;
 import java.util.functions.Predicate;
 import java.util.streams.Sink;
@@ -50,7 +50,7 @@
 
     @Override
     public Iterator<T> wrapIterator(int flags, Iterator<T> source) {
-        return iterator(source, predicate);
+        return Iterators.filter(source, predicate);
     }
 
     @Override
@@ -64,33 +64,4 @@
             }
         };
     }
-
-    public static<T> Iterator<T> iterator(final Iterator<T> source, final Predicate<? super T> predicate) {
-        Objects.requireNonNull(source);
-        Objects.requireNonNull(predicate);
-        return new Iterator<T>() {
-            boolean nextReady = false;
-            T nextValue = null;
-
-            @Override
-            public boolean hasNext() {
-                while (!nextReady && source.hasNext()) {
-                    nextValue = source.next();
-                    nextReady = predicate.test(nextValue);
-                }
-
-                return nextReady;
-            }
-
-            @Override
-            public T next() {
-                if (nextReady || hasNext()) {
-                    nextReady = false;
-                    return nextValue;
-                }
-
-                throw new NoSuchElementException();
-            }
-        };
-    }
 }
--- a/src/share/classes/java/util/streams/ops/MapOp.java	Mon Nov 12 10:55:20 2012 +0100
+++ b/src/share/classes/java/util/streams/ops/MapOp.java	Mon Nov 12 12:45:50 2012 -0500
@@ -25,6 +25,7 @@
 package java.util.streams.ops;
 
 import java.util.Iterator;
+import java.util.Iterators;
 import java.util.Objects;
 import java.util.functions.Mapper;
 import java.util.streams.Sink;
@@ -49,7 +50,7 @@
 
     @Override
     public Iterator<R> wrapIterator(int flags, final Iterator<T> source) {
-        return iterator(source, mapper);
+        return Iterators.map(source, mapper);
     }
 
     @Override
@@ -61,20 +62,4 @@
             }
         };
     }
-
-    public static<T, R> Iterator<R> iterator(final Iterator<T> source, final Mapper<? extends R, ? super T> mapper) {
-        Objects.requireNonNull(source);
-        Objects.requireNonNull(mapper);
-        return new Iterator<R>() {
-            @Override
-            public boolean hasNext() {
-                return source.hasNext();
-            }
-
-            @Override
-            public R next() {
-                return mapper.map(source.next());
-            }
-        };
-    }
 }
--- a/test-ng/tests/org/openjdk/tests/java/util/IteratorsNullTest.java	Mon Nov 12 10:55:20 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,118 +0,0 @@
-/*
- * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package org.openjdk.tests.java.util;
-
-import org.testng.annotations.DataProvider;
-import org.testng.annotations.Factory;
-import org.testng.annotations.Test;
-
-import java.util.*;
-import java.util.functions.*;
-import java.util.streams.ops.FilterOp;
-import java.util.streams.ops.MapOp;
-import java.util.streams.ops.SortedOp;
-
-import static org.openjdk.tests.java.util.LambdaTestHelpers.*;
-
-/**
- * IteratorsNullTest -- tests of proper null handling in Iterators methods
- */
-@Test(groups = { "null", "lambda" })
-public class IteratorsNullTest extends NullArgsTestCase {
-    @Factory(dataProvider = "data")
-    public IteratorsNullTest(String name, Block<Object[]> sink, Object[] args) {
-        super(name, sink, args);
-    }
-
-    @DataProvider(name = "data")
-    @SuppressWarnings("unchecked")
-    public static Object[][] makeData() {
-        List<Integer> list = countTo(10);
-        return new Object[][] {
-                { "FilterOp.iterator", new Block<Object[]>() {
-                    public void apply(Object[] args) {
-                        FilterOp.iterator((Iterator<Integer>) args[0], (Predicate<Integer>) args[1]);
-                    }
-                }, new Object[] { list.iterator(), pTrue }
-                },
-                { "MapOp.iterator", new Block<Object[]>() {
-                    public void apply(Object[] args) {
-                        MapOp.iterator((Iterator<Integer>) args[0], (Mapper<Integer, Integer>) args[1]);
-                    }
-                }, new Object[] { list.iterator(), mDoubler }
-                },
-                {"Iterators.reduce", new Block<Object[]>() {
-                    public void apply(Object[] args) {
-                        Iterators.<Integer>reduce((Iterator<Integer>) args[0], 0, (BinaryOperator<Integer>) args[1]);
-                    }
-                }, new Object[]{list.iterator(), rPlus}
-                },
-                { "Iterators.mapReduce", new Block<Object[]>() {
-                    public void apply(Object[] args) {
-                        Iterators.<Integer, Integer>mapReduce((Iterator<Integer>) args[0],
-                                                              (Mapper<Integer, Integer>) args[1],
-                                                              0, (BinaryOperator<Integer>) args[2]);
-                    }
-                }, new Object[] { list.iterator(), mDoubler, rPlus }
-                },
-                { "Iterators.mapReduce(int)", new Block<Object[]>() {
-                    public void apply(Object[] args) {
-                        Iterators.mapReduce((Iterator<Integer>) args[0],
-                                            (IntMapper<Integer>) args[1],
-                                            0, (IntBinaryOperator) args[2]);
-                    }
-                }, new Object[] { list.iterator(), imDoubler, irPlus }
-                },
-                { "Iterators.mapReduce(long)", new Block<Object[]>() {
-                    public void apply(Object[] args) {
-                        Iterators.mapReduce((Iterator<Integer>) args[0],
-                                            (LongMapper<Integer>) args[1],
-                                            0, (LongBinaryOperator) args[2]);
-                    }
-                }, new Object[] { asLongs(list).iterator(), lmDoubler, lrPlus }
-                },
-                { "Iterators.mapReduce(double)", new Block<Object[]>() {
-                    public void apply(Object[] args) {
-                        Iterators.mapReduce((Iterator<Integer>) args[0],
-                                            (DoubleMapper<Integer>) args[1],
-                                            0, (DoubleBinaryOperator) args[2]);
-                    }
-                }, new Object[] { asDoubles(list).iterator(), dmDoubler, drPlus }
-                },
-                { "SortedOp.iterator", new Block<Object[]>() {
-                    public void apply(Object[] args) {
-                        SortedOp.iterator((Iterator<Integer>) args[0], Comparators.<Integer>naturalOrder());
-                    }
-                }, new Object[] { list.iterator() }
-                },
-                { "Iterators.sorted(Comparator)", new Block<Object[]>() {
-                    public void apply(Object[] args) {
-                        SortedOp.iterator((Iterator<Integer>) args[0], (Comparator) args[1]);
-                    }
-                }, new Object[] { list.iterator(), cInteger }
-                },
-        };
-    }
-}
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/FilterOpTest.java	Mon Nov 12 10:55:20 2012 +0100
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/FilterOpTest.java	Mon Nov 12 12:45:50 2012 -0500
@@ -27,6 +27,7 @@
 import org.openjdk.tests.java.util.streams.StreamTestDataProvider;
 import org.testng.annotations.Test;
 
+import java.util.Iterators;
 import java.util.streams.ops.FilterOp;
 
 import static org.openjdk.tests.java.util.LambdaTestHelpers.*;
@@ -39,12 +40,12 @@
 @Test
 public class FilterOpTest extends StreamOpTestCase {
     public void testRawIterator() {
-        assertCountSum(FilterOp.iterator(countTo(0).iterator(), pTrue), 0, 0);
-        assertCountSum(FilterOp.iterator(countTo(10).iterator(), pFalse), 0, 0);
-        assertCountSum(FilterOp.iterator(countTo(10).iterator(), pEven), 5, 30);
-        assertCountSum(FilterOp.iterator(countTo(10).iterator(), pOdd), 5, 25);
-        assertCountSum(FilterOp.iterator(countTo(10).iterator(), pTrue), 10, 55);
-        assertCountSum(FilterOp.iterator(FilterOp.iterator(countTo(10).iterator(), pEven), pOdd), 0, 0);
+        assertCountSum(Iterators.filter(countTo(0).iterator(), pTrue), 0, 0);
+        assertCountSum(Iterators.filter(countTo(10).iterator(), pFalse), 0, 0);
+        assertCountSum(Iterators.filter(countTo(10).iterator(), pEven), 5, 30);
+        assertCountSum(Iterators.filter(countTo(10).iterator(), pOdd), 5, 25);
+        assertCountSum(Iterators.filter(countTo(10).iterator(), pTrue), 10, 55);
+        assertCountSum(Iterators.filter(Iterators.filter(countTo(10).iterator(), pEven), pOdd), 0, 0);
     }
 
     public void testFilter() {
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/MapOpTest.java	Mon Nov 12 10:55:20 2012 +0100
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/MapOpTest.java	Mon Nov 12 12:45:50 2012 -0500
@@ -27,6 +27,7 @@
 import org.openjdk.tests.java.util.streams.StreamTestDataProvider;
 import org.testng.annotations.Test;
 
+import java.util.Iterators;
 import java.util.streams.ops.MapOp;
 
 import static org.openjdk.tests.java.util.LambdaTestHelpers.*;
@@ -39,12 +40,12 @@
 @Test
 public class MapOpTest extends StreamOpTestCase {
     public void testRawIterator() {
-        assertCountSum(MapOp.iterator(countTo(0).iterator(), mId), 0, 0);
-        assertCountSum(MapOp.iterator(countTo(10).iterator(), mId), 10, 55);
-        assertCountSum(MapOp.iterator(countTo(10).iterator(), mZero), 10, 0);
-        assertCountSum(MapOp.iterator(countTo(0).iterator(), mDoubler), 0, 0);
-        assertCountSum(MapOp.iterator(countTo(10).iterator(), mDoubler), 10, 110);
-        assertCountSum(MapOp.iterator(MapOp.iterator(countTo(10).iterator(), mDoubler), mDoubler), 10, 220);
+        assertCountSum(Iterators.map(countTo(0).iterator(), mId), 0, 0);
+        assertCountSum(Iterators.map(countTo(10).iterator(), mId), 10, 55);
+        assertCountSum(Iterators.map(countTo(10).iterator(), mZero), 10, 0);
+        assertCountSum(Iterators.map(countTo(0).iterator(), mDoubler), 0, 0);
+        assertCountSum(Iterators.map(countTo(10).iterator(), mDoubler), 10, 110);
+        assertCountSum(Iterators.map(Iterators.map(countTo(10).iterator(), mDoubler), mDoubler), 10, 220);
     }
 
     public void testMap() {
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/TeeOpTest.java	Mon Nov 12 10:55:20 2012 +0100
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/TeeOpTest.java	Mon Nov 12 12:45:50 2012 -0500
@@ -28,11 +28,10 @@
 import org.testng.annotations.Test;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
+import java.util.Iterators;
 import java.util.List;
 import java.util.functions.Block;
-import java.util.streams.TerminalSink;
 import java.util.streams.ops.*;
 
 import static org.openjdk.tests.java.util.LambdaTestHelpers.*;
@@ -56,7 +55,7 @@
         assertCountSum(copy.iterator(), 10, 55);
 
         copy.clear();
-        assertCountSum(TeeOp.iterator(MapOp.iterator(countTo(10).iterator(), mDoubler), asBlock(copy)), 10, 110);
+        assertCountSum(TeeOp.iterator(Iterators.map(countTo(10).iterator(), mDoubler), asBlock(copy)), 10, 110);
         assertCountSum(copy.iterator(), 10, 110);
     }