changeset 6357:b93e380a7834

Merge Traversable into Iterable
author briangoetz
date Mon, 29 Oct 2012 10:39:46 -0400
parents 4e7f6a6e8369
children b0f979b37bff
files src/share/classes/java/lang/CharSequence.java src/share/classes/java/lang/Iterable.java src/share/classes/java/util/Collection.java src/share/classes/java/util/Traversable.java src/share/classes/java/util/streams/Streams.java src/share/classes/java/util/streams/ops/Node.java src/share/classes/java/util/streams/ops/NodeBuilder.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/StreamOpTestCase.java
diffstat 8 files changed, 37 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/lang/CharSequence.java	Fri Oct 26 11:07:34 2012 -0400
+++ b/src/share/classes/java/lang/CharSequence.java	Mon Oct 29 10:39:46 2012 -0400
@@ -27,7 +27,6 @@
 
 import java.util.Iterator;
 import java.util.NoSuchElementException;
-import java.util.Traversable;
 import java.util.functions.Block;
 import java.util.streams.Stream;
 import java.util.streams.Streams;
@@ -121,7 +120,7 @@
      * @return an Iterable of Character values from this sequence
      */
     public Stream<Character> asChars() default {
-        return Streams.stream(new CharacterTraversable(this), length());
+        return Streams.stream(new CharacterIterable(this), length());
     }
 
     /**
@@ -140,14 +139,14 @@
      * @return an Iterable of Unicode code points from this sequence
      */
     public Stream<Integer> asCodePoints() default {
-        return Streams.stream(new CodePointTraversable(this));
+        return Streams.stream(new CodePointIterable(this));
     }
 }
 
-class CharacterTraversable implements Traversable<Character> {
+class CharacterIterable implements Iterable<Character> {
     final CharSequence cs;
 
-    CharacterTraversable(CharSequence cs) {
+    CharacterIterable(CharSequence cs) {
         this.cs = cs;
     }
 
@@ -175,10 +174,10 @@
     }
 }
 
-class CodePointTraversable implements Traversable<Integer> {
+class CodePointIterable implements Iterable<Integer> {
     final CharSequence cs;
 
-    CodePointTraversable(CharSequence cs) {
+    CodePointIterable(CharSequence cs) {
         this.cs = cs;
     }
 
--- a/src/share/classes/java/lang/Iterable.java	Fri Oct 26 11:07:34 2012 -0400
+++ b/src/share/classes/java/lang/Iterable.java	Mon Oct 29 10:39:46 2012 -0400
@@ -45,5 +45,16 @@
      * @return an Iterator.
      */
     Iterator<T> iterator();
+
+    /**
+     * Execute the specified Block for each element
+     *
+     * @param block The Block to which elements will be provided
+     */
+    void forEach(Block<? super T> block) default {
+        for (T t : this) {
+            block.apply(t);
+        }
+    }
 }
 
--- a/src/share/classes/java/util/Collection.java	Fri Oct 26 11:07:34 2012 -0400
+++ b/src/share/classes/java/util/Collection.java	Mon Oct 29 10:39:46 2012 -0400
@@ -25,7 +25,6 @@
 
 package java.util;
 
-import java.util.functions.Block;
 import java.util.functions.Predicate;
 import java.util.streams.Stream;
 import java.util.streams.Streamable;
@@ -130,7 +129,7 @@
  * @since 1.2
  */
 
-public interface Collection<E> extends Sized, Streamable<Stream<E>>, Traversable<E>, Stream.Destination<E> {
+public interface Collection<E> extends Sized, Streamable<Stream<E>>, Iterable<E>, Stream.Destination<E> {
     // Query Operations
 
     /**
@@ -495,13 +494,6 @@
     }
 
     @Override
-    void forEach(Block<? super E> block) default {
-        for (E e: this) {
-            block.apply(e);
-        }
-    }
-
-    @Override
     void addAll(Stream<? extends E> stream) default {
         if (stream.isParallel())
             stream = stream.sequential();
--- a/src/share/classes/java/util/Traversable.java	Fri Oct 26 11:07:34 2012 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-/*
- * Copyright (c) 2012, 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 java.util;
-
-import java.util.functions.Block;
-
-/**
- * Implementing this interface allows an object to indicate that it provides
- * the {@code forEach} method for internal iteration.
- *
- * @param <T> type of elements to be traversed.
- *
- * @author Brian Goetz
- */
-public interface Traversable<T> extends Iterable<T> {
-    /**
-     * Each element of the object will be provided to the specified Sink.
-     *
-     * @param block The Sink to which elements will be provided.
-     */
-    public void forEach(Block<? super T> block) default {
-        for (T t : this) {
-            block.apply(t);
-        }
-    }
-}
--- a/src/share/classes/java/util/streams/Streams.java	Fri Oct 26 11:07:34 2012 -0400
+++ b/src/share/classes/java/util/streams/Streams.java	Mon Oct 29 10:39:46 2012 -0400
@@ -42,35 +42,23 @@
     // Stream
 
     public static<T> Stream<T> stream(Collection<T> source) {
-        return new ValuePipeline<>(new TraversableStreamAccessor<>(source, source.size()));
+        return new ValuePipeline<>(new IterableStreamAccessor<>(source, source.size()));
     }
 
     public static<T> Stream<T> stream(Collection<T> source, int flags) {
-        return new ValuePipeline<>(new TraversableStreamAccessor<>(source, source.size(), flags));
-    }
-
-    public static<T> Stream<T> stream(Traversable<T> source) {
-        return new ValuePipeline<>(new TraversableStreamAccessor<>(source));
-    }
-
-    public static<T> Stream<T> stream(Traversable<T> source, int sizeOrUnknown) {
-        return new ValuePipeline<>(new TraversableStreamAccessor<>(source, sizeOrUnknown));
-    }
-
-    public static<T> Stream<T> stream(Traversable<T> source, int sizeOrUnknown, int flags) {
-        return new ValuePipeline<>(new TraversableStreamAccessor<>(source, sizeOrUnknown, flags));
+        return new ValuePipeline<>(new IterableStreamAccessor<>(source, source.size(), flags));
     }
 
     public static<T> Stream<T> stream(Iterable<T> source) {
-        return stream(source.iterator());
+        return new ValuePipeline<>(new IterableStreamAccessor<>(source));
     }
 
     public static<T> Stream<T> stream(Iterable<T> source, int sizeOrUnknown) {
-        return stream(source.iterator(), sizeOrUnknown);
+        return new ValuePipeline<>(new IterableStreamAccessor<>(source, sizeOrUnknown));
     }
 
     public static<T> Stream<T> stream(Iterable<T> source, int sizeOrUnknown, int flags) {
-        return stream(source.iterator(), sizeOrUnknown, flags);
+        return new ValuePipeline<>(new IterableStreamAccessor<>(source, sizeOrUnknown, flags));
     }
 
     public static<T> Stream<T> stream(Iterator<T> source) {
@@ -441,23 +429,23 @@
         }
     }
 
-    private static class TraversableStreamAccessor<T>
+    private static class IterableStreamAccessor<T>
             implements StreamAccessor.ForSequential<T>, Iterator<T> {
-        private final Traversable<T> traversable;
+        private final Iterable<T> iterable;
         private final int flags;
         private final int sizeOrUnknown;
         Iterator<T> iterator = null;
 
-        TraversableStreamAccessor(Traversable<T> traversable) {
-            this(traversable, -1, 0);
+        IterableStreamAccessor(Iterable<T> iterable) {
+            this(iterable, -1, 0);
         }
 
-        TraversableStreamAccessor(Traversable<T> traversable, int sizeOrUnknown) {
-            this(traversable, sizeOrUnknown, 0);
+        IterableStreamAccessor(Iterable<T> iterable, int sizeOrUnknown) {
+            this(iterable, sizeOrUnknown, 0);
         }
 
-        TraversableStreamAccessor(Traversable<T> traversable, int sizeOrUnknown, int flags) {
-            this.traversable = traversable;
+        IterableStreamAccessor(Iterable<T> iterable, int sizeOrUnknown, int flags) {
+            this.iterable = iterable;
             this.flags = (sizeOrUnknown >= 0 ? StreamOpFlags.IS_SIZED : 0) |
                          (flags & (StreamOpFlags.IS_DISTINCT | StreamOpFlags.IS_SORTED | StreamOpFlags.IS_ORDERED));
             this.sizeOrUnknown = sizeOrUnknown;
@@ -465,14 +453,14 @@
 
         public Iterator<T> iterator() {
             if (iterator == null)
-                iterator = traversable.iterator();
+                iterator = iterable.iterator();
             return iterator;
         }
 
         @Override
         public void forEach(Block<T> sink) {
             if (iterator == null) {
-                traversable.forEach(sink);
+                iterable.forEach(sink);
                 iterator = Collections.emptyIterator();
             }
             else {
--- a/src/share/classes/java/util/streams/ops/Node.java	Fri Oct 26 11:07:34 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/Node.java	Mon Oct 29 10:39:46 2012 -0400
@@ -27,7 +27,7 @@
 import java.util.*;
 import java.util.streams.Spliterator;
 
-public interface Node<T> extends Traversable<T>, Sized {
+public interface Node<T> extends Iterable<T>, Sized {
 
     /**
      *
--- a/src/share/classes/java/util/streams/ops/NodeBuilder.java	Fri Oct 26 11:07:34 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/NodeBuilder.java	Mon Oct 29 10:39:46 2012 -0400
@@ -25,7 +25,6 @@
 package java.util.streams.ops;
 
 import java.util.Sized;
-import java.util.Traversable;
 import java.util.functions.UnaryOperator;
 import java.util.streams.Sink;
 
@@ -35,7 +34,7 @@
  * @author Brian Goetz
  */
 // @@@ Implement Stream.Destination for bulk adding with addAll ? (see Nodes.node(Stream ) )
-public interface NodeBuilder<T> extends Traversable<T>, Sized, Sink.OfValue<T> {
+public interface NodeBuilder<T> extends Iterable<T>, Sized, Sink.OfValue<T> {
 
     /**
      * Build the node.
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/StreamOpTestCase.java	Fri Oct 26 11:07:34 2012 -0400
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/StreamOpTestCase.java	Mon Oct 29 10:39:46 2012 -0400
@@ -358,7 +358,7 @@
         return refResult;
     }
 
-    static class StreamResult<T> implements Sink.OfValue<T>, Traversable<T>, Sized {
+    static class StreamResult<T> implements Sink.OfValue<T>, Iterable<T>, Sized {
 
         private T[] array;
 
@@ -468,7 +468,7 @@
         return (Stream<U>) pipe;
     }
 
-    public static interface TestData<T> extends Traversable<T>, Sized {
+    public static interface TestData<T> extends Iterable<T>, Sized {
         AbstractPipeline<?, T> seq();
         AbstractPipeline<?, T> par();
         Iterator<T> iterator();