changeset 5853:cc0054ebcba5 it2-bootstrap

GroupBy tests Some additional scaffolding. Return type of groupBy changed from Map<K, Streamable<T>> to Map<K, SizedStreamable<T>>wq
author mduigou
date Wed, 15 Aug 2012 13:19:46 -0700
parents 009f78681526
children 2632635aea6d 75622b3b1007
files src/share/classes/java/util/streams/ParallelPipeline.java src/share/classes/java/util/streams/ParallelStreamOps.java src/share/classes/java/util/streams/ParallelStreamable.java src/share/classes/java/util/streams/SequentialPipeline.java src/share/classes/java/util/streams/StreamBuilder.java src/share/classes/java/util/streams/StreamBuilders.java src/share/classes/java/util/streams/StreamOps.java src/share/classes/java/util/streams/Streamable.java src/share/classes/java/util/streams/Streams.java src/share/classes/java/util/streams/ops/GroupByOp.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/GroupByOpTest.java
diffstat 11 files changed, 149 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/streams/ParallelPipeline.java	Tue Aug 14 17:08:56 2012 -0700
+++ b/src/share/classes/java/util/streams/ParallelPipeline.java	Wed Aug 15 13:19:46 2012 -0700
@@ -145,7 +145,7 @@
     }
 
     @Override
-    public <K extends Object> Map<K, Streamable<U>> groupBy(Mapper<? super U, ? extends K> mapper) {
+    public <K> Map<K, SizedStreamable<U>> groupBy(Mapper<? super U, ? extends K> mapper) {
         return pipeline(new GroupByOp<>(mapper));
     }
 
--- a/src/share/classes/java/util/streams/ParallelStreamOps.java	Tue Aug 14 17:08:56 2012 -0700
+++ b/src/share/classes/java/util/streams/ParallelStreamOps.java	Wed Aug 15 13:19:46 2012 -0700
@@ -50,7 +50,7 @@
 
     Object[] toArray();
 
-    <K> Map<K, Streamable<T>> groupBy(Mapper<? super T, ? extends K> mapper);
+    <K> Map<K, SizedStreamable<T>> groupBy(Mapper<? super T, ? extends K> mapper);
 
     /**
      * Produce an {@link Iterable} representing the contents of this {@code Splittable}.  In general, this method is
--- a/src/share/classes/java/util/streams/ParallelStreamable.java	Tue Aug 14 17:08:56 2012 -0700
+++ b/src/share/classes/java/util/streams/ParallelStreamable.java	Wed Aug 15 13:19:46 2012 -0700
@@ -84,7 +84,7 @@
     }
 
     @Override
-    <K> Map<K, Streamable<T>> groupBy(Mapper<? super T, ? extends K> mapper) default {
+    <K> Map<K, SizedStreamable<T>> groupBy(Mapper<? super T, ? extends K> mapper) default {
         return ParallelPipeline.wrap(this).groupBy(mapper);
     }
 
--- a/src/share/classes/java/util/streams/SequentialPipeline.java	Tue Aug 14 17:08:56 2012 -0700
+++ b/src/share/classes/java/util/streams/SequentialPipeline.java	Wed Aug 15 13:19:46 2012 -0700
@@ -131,7 +131,7 @@
     }
 
     @Override
-    public <K> Map<K, Streamable<U>> groupBy(Mapper<? super U, ? extends K> mapper) {
+    public <K> Map<K, SizedStreamable<U>> groupBy(Mapper<? super U, ? extends K> mapper) {
         return pipeline(new GroupByOp<>(mapper));
     }
 
--- a/src/share/classes/java/util/streams/StreamBuilder.java	Tue Aug 14 17:08:56 2012 -0700
+++ b/src/share/classes/java/util/streams/StreamBuilder.java	Wed Aug 15 13:19:46 2012 -0700
@@ -32,7 +32,7 @@
  *
  * @author Brian Goetz
  */
-public interface StreamBuilder<T> extends SizedStreamable<T>, Sink<T>, Sized /* , Fillable<T> */ {
+public interface StreamBuilder<T> extends SizedStreamable<T>, Sink<T> /* , Fillable<T> */ {
     @Override
     int getStreamState();
 
--- a/src/share/classes/java/util/streams/StreamBuilders.java	Tue Aug 14 17:08:56 2012 -0700
+++ b/src/share/classes/java/util/streams/StreamBuilders.java	Wed Aug 15 13:19:46 2012 -0700
@@ -28,6 +28,7 @@
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 import java.util.functions.Sink;
 
 /**
@@ -60,6 +61,20 @@
         }
 
         @Override
+        public boolean equals(Object obj) {
+            if(obj instanceof Streamable) {
+                return Streams.equals(this, (Streamable<?>) obj);
+            }
+
+            return false;
+        }
+
+        @Override
+        public int hashCode() {
+            return Streams.hashCode(this);
+        }
+
+        @Override
         public int getStreamState() {
             return Stream.STATE_SIZED;
         }
@@ -82,7 +97,7 @@
 
         @Override
         public Iterator<T> iterator() {
-            return Arrays.iterator( (T[]) array, 0, curSize);
+            return (Iterator<T>) Arrays.iterator( array, 0, curSize);
         }
 
         @Override
@@ -119,6 +134,20 @@
         }
 
         @Override
+        public boolean equals(Object obj) {
+            if(obj instanceof Streamable) {
+                return Streams.equals(this, (Streamable<?>) obj);
+            }
+
+            return false;
+        }
+
+        @Override
+        public int hashCode() {
+            return Streams.hashCode(this);
+        }
+
+        @Override
         public int getStreamState() {
             return Stream.STATE_SIZED;
         }
--- a/src/share/classes/java/util/streams/StreamOps.java	Tue Aug 14 17:08:56 2012 -0700
+++ b/src/share/classes/java/util/streams/StreamOps.java	Wed Aug 15 13:19:46 2012 -0700
@@ -56,7 +56,7 @@
 
     Object[] toArray();
 
-    <U> Map<U, Streamable<T>> groupBy(Mapper<? super T, ? extends U> mapper);
+    <U> Map<U, SizedStreamable<T>> groupBy(Mapper<? super T, ? extends U> mapper);
 
 //    <U> Map<U, StreamSource<T>> groupByMulti(FlatMapper<? super T, ? extends U> mapper);
 
--- a/src/share/classes/java/util/streams/Streamable.java	Tue Aug 14 17:08:56 2012 -0700
+++ b/src/share/classes/java/util/streams/Streamable.java	Wed Aug 15 13:19:46 2012 -0700
@@ -65,7 +65,7 @@
     }
 
     @Override
-    <K> Map<K, Streamable<T>> groupBy(Mapper<? super T, ? extends K> mapper) default {
+    <K> Map<K, SizedStreamable<T>> groupBy(Mapper<? super T, ? extends K> mapper) default {
         return SequentialPipeline.eagerOp(this, new GroupByOp<>(mapper));
     }
 
--- a/src/share/classes/java/util/streams/Streams.java	Tue Aug 14 17:08:56 2012 -0700
+++ b/src/share/classes/java/util/streams/Streams.java	Wed Aug 15 13:19:46 2012 -0700
@@ -182,4 +182,35 @@
             return iterator().next();
         }
     }
+
+    public static boolean equals(Streamable<?> one, Streamable<?> two) {
+        Iterator<?> each = two.iterator();
+        for (Object first : one) {
+            if (!each.hasNext()) {
+                return false;
+            }
+
+            Object second = each.next();
+
+            if (!Objects.equals(first, second)) {
+                return false;
+            }
+        }
+
+        if (each.hasNext()) {
+            return false;
+        }
+
+        return true;
+    }
+
+    public static int hashCode(Streamable<?> streamable) {
+        int result = -1;
+
+        for(Object each : streamable) {
+            result = 17 * (result ^ Objects.hashCode(each));
+        }
+
+        return result;
+    }
 }
--- a/src/share/classes/java/util/streams/ops/GroupByOp.java	Tue Aug 14 17:08:56 2012 -0700
+++ b/src/share/classes/java/util/streams/ops/GroupByOp.java	Wed Aug 15 13:19:46 2012 -0700
@@ -37,7 +37,7 @@
  *
  * @author Brian Goetz
  */
-public class GroupByOp<T, K> implements EagerOp<T, Map<K, Streamable<T>>> {
+public class GroupByOp<T, K> implements EagerOp<T, Map<K, SizedStreamable<T>>> {
     private final Mapper<? super T, ? extends K> mapper;
 
     public GroupByOp(Mapper<? super T, ? extends K> mapper) {
@@ -45,8 +45,8 @@
     }
 
     @Override
-    public StatefulSink<T, Map<K, Streamable<T>>> sink() {
-        return new StatefulSink<T, Map<K, Streamable<T>>>() {
+    public StatefulSink<T, Map<K, SizedStreamable<T>>> sink() {
+        return new StatefulSink<T, Map<K, SizedStreamable<T>>>() {
             private final Map<K, StreamBuilder<T>> map = new HashMap<>();
 
             @Override
@@ -55,7 +55,7 @@
             }
 
             @Override
-            public Map<K, Streamable<T>> end() {
+            public Map<K, SizedStreamable<T>> end() {
                 return (Map) map;
             }
 
@@ -73,7 +73,7 @@
     }
 //
 //    @Override
-//    public <V> Map<K, Streamable<T>> computeParallel(ParallelStreamable<V> source, ParallelOpHelper<T, V> helper) {
+//    public <V> Map<K, SizedStreamable<T>> computeParallel(ParallelStreamable<V> source, ParallelOpHelper<T, V> helper) {
 //        throw new UnsupportedOperationException("nyi");
 //    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/GroupByOpTest.java	Wed Aug 15 13:19:46 2012 -0700
@@ -0,0 +1,76 @@
+/*
+ * 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 org.openjdk.tests.java.util.streams.ops;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.functions.Mappers;
+import java.util.streams.SizedStreamable;
+import java.util.streams.Streamable;
+import org.testng.annotations.Test;
+
+import java.util.streams.ops.FilterOp;
+import java.util.streams.ops.GroupByOp;
+import java.util.streams.ops.MapOp;
+
+import static org.openjdk.tests.java.util.LambdaTestHelpers.*;
+import static org.openjdk.tests.java.util.LambdaTestHelpers.pEven;
+import static org.openjdk.tests.java.util.LambdaTestHelpers.pOdd;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.assertEquals;
+/**
+ * MapOpTest
+ *
+ * @author Brian Goetz
+ */
+public class GroupByOpTest extends StreamOpTestCase {
+
+    public void testRawIterator() {
+        GroupByOp<Integer, Boolean> grouping = new GroupByOp<>(Mappers.forPredicate(pEven, true, false));
+
+        Map<Boolean,SizedStreamable<Integer>> result = iteratorToStatefulSink(countTo(10).iterator(), grouping.sink());
+
+        assertEquals(2, result.keySet().size());
+        for(SizedStreamable<Integer> group : result.values()) {
+            assertEquals(5, group.size());
+        }
+    }
+
+    @Test(dataProvider = "opArrays")
+    public void testOps(Integer[] data) {
+        assertConsistentOpBehavior(data, new GroupByOp<>(mId));
+        assertConsistentOpBehavior(data, new GroupByOp<>(mZero));
+        assertConsistentOpBehavior(data, new GroupByOp<>(mDoubler));
+        assertConsistentOpBehavior(data, new GroupByOp<>(mId.compose(mDoubler)));
+        assertConsistentOpBehavior(data, new GroupByOp<>(mDoubler.compose(mDoubler)));
+        assertConsistentOpBehavior(data, new GroupByOp<>(Mappers.forPredicate(pFalse, true, false)));
+        assertConsistentOpBehavior(data, new GroupByOp<>(Mappers.forPredicate(pTrue, true, false)));
+        assertConsistentOpBehavior(data, new GroupByOp<>(Mappers.forPredicate(pEven, true, false)));
+        assertConsistentOpBehavior(data, new GroupByOp<>(Mappers.forPredicate(pOdd, true, false)));
+    }
+}