changeset 7753:690eeefac1b4

Fix bug in SortedSet.spliterator. Add tests for default spliterator methods on Collection, List, Set, and SortedSet
author psandoz
date Wed, 27 Mar 2013 22:39:58 +0100
parents 19a6f9abe32f
children 62a94e74d2af
files src/share/classes/java/util/SortedSet.java test/java/util/Spliterator/SpliteratorLateBindingFailFastTest.java test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java
diffstat 3 files changed, 156 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/SortedSet.java	Wed Mar 27 20:55:25 2013 +0100
+++ b/src/share/classes/java/util/SortedSet.java	Wed Mar 27 22:39:58 2013 +0100
@@ -254,7 +254,7 @@
                 this, Spliterator.DISTINCT | Spliterator.SORTED | Spliterator.ORDERED) {
             @Override
             public Comparator<? super E> getComparator() {
-                return this.getComparator();
+                return SortedSet.this.comparator();
             }
         };
     }
--- a/test/java/util/Spliterator/SpliteratorLateBindingFailFastTest.java	Wed Mar 27 20:55:25 2013 +0100
+++ b/test/java/util/Spliterator/SpliteratorLateBindingFailFastTest.java	Wed Mar 27 22:39:58 2013 +0100
@@ -42,7 +42,6 @@
 import java.util.Set;
 import java.util.Spliterator;
 import java.util.Stack;
-import java.util.StringJoiner;
 import java.util.TreeMap;
 import java.util.TreeSet;
 import java.util.Vector;
@@ -166,9 +165,11 @@
             }
         }
 
-        StringJoiner joiner(String description) {
-            return new StringJoiner(", ", description + " {", "}").
-                    add("size=" + exp.size());
+        StringBuilder joiner(String description) {
+            return new StringBuilder(description).
+                    append(" {").
+                    append("size=").append(exp.size()).
+                    append("}");
         }
     }
 
--- a/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java	Wed Mar 27 20:55:25 2013 +0100
+++ b/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java	Wed Mar 27 22:39:58 2013 +0100
@@ -32,11 +32,15 @@
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
+import java.util.AbstractCollection;
+import java.util.AbstractList;
+import java.util.AbstractSet;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Deque;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -48,10 +52,11 @@
 import java.util.List;
 import java.util.Map;
 import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.SortedSet;
 import java.util.Spliterator;
 import java.util.Spliterators;
 import java.util.Stack;
-import java.util.StringJoiner;
 import java.util.TreeMap;
 import java.util.TreeSet;
 import java.util.Vector;
@@ -130,9 +135,11 @@
             add(description + ".entrySet().spliterator()", mExp.entrySet(), () -> m.apply(mExp).entrySet().spliterator());
         }
 
-        StringJoiner joiner(String description) {
-            return new StringJoiner(", ", description + " {", "}").
-                    add("size=" + exp.size());
+        StringBuilder joiner(String description) {
+            return new StringBuilder(description).
+                    append(" {").
+                    append("size=").append(exp.size()).
+                    append("}");
         }
     }
 
@@ -193,6 +200,125 @@
 
             // Collections
 
+            // default method implementations
+
+            class AbstractCollectionImpl extends AbstractCollection<Integer> {
+                Collection<Integer> c;
+
+                AbstractCollectionImpl(Collection<Integer> c) {
+                    this.c = c;
+                }
+
+                @Override
+                public Iterator<Integer> iterator() {
+                    return c.iterator();
+                }
+
+                @Override
+                public int size() {
+                    return c.size();
+                }
+            }
+            db.addCollection(
+                    c -> new AbstractCollectionImpl(c));
+
+            class AbstractListImpl extends AbstractList<Integer> {
+                List<Integer> l;
+
+                AbstractListImpl(Collection<Integer> c) {
+                    this.l = new ArrayList<>(c);
+                }
+
+                @Override
+                public Integer get(int index) {
+                    return l.get(index);
+                }
+
+                @Override
+                public int size() {
+                    return l.size();
+                }
+            }
+            db.addCollection(
+                    c -> new AbstractListImpl(c));
+
+            class AbstractSetImpl extends AbstractSet<Integer> {
+                Set<Integer> s;
+
+                AbstractSetImpl(Collection<Integer> c) {
+                    this.s = new HashSet<>(c);
+                }
+
+                @Override
+                public Iterator<Integer> iterator() {
+                    return s.iterator();
+                }
+
+                @Override
+                public int size() {
+                    return s.size();
+                }
+            }
+            db.addCollection(
+                    c -> new AbstractSetImpl(c));
+
+            class AbstractSortedSetImpl extends AbstractSet<Integer> implements SortedSet<Integer> {
+                SortedSet<Integer> s;
+
+                AbstractSortedSetImpl(Collection<Integer> c) {
+                    this.s = new TreeSet<>(c);
+                }
+
+                @Override
+                public Iterator<Integer> iterator() {
+                    return s.iterator();
+                }
+
+                @Override
+                public int size() {
+                    return s.size();
+                }
+
+                @Override
+                public Comparator<? super Integer> comparator() {
+                    return s.comparator();
+                }
+
+                @Override
+                public SortedSet<Integer> subSet(Integer fromElement, Integer toElement) {
+                    return s.subSet(fromElement, toElement);
+                }
+
+                @Override
+                public SortedSet<Integer> headSet(Integer toElement) {
+                    return s.headSet(toElement);
+                }
+
+                @Override
+                public SortedSet<Integer> tailSet(Integer fromElement) {
+                    return s.tailSet(fromElement);
+                }
+
+                @Override
+                public Integer first() {
+                    return s.first();
+                }
+
+                @Override
+                public Integer last() {
+                    return s.last();
+                }
+
+                @Override
+                public Spliterator<Integer> spliterator() {
+                    return SortedSet.super.spliterator();
+                }
+            }
+            db.addCollection(
+                    c -> new AbstractSortedSetImpl(c));
+
+            //
+
             db.add("Arrays.asList().spliterator()",
                    () -> Spliterators.spliterator(Arrays.asList(exp.toArray(new Integer[0])), 0));
 
@@ -339,9 +465,11 @@
             add(description, exp, s);
         }
 
-        StringJoiner joiner(String description) {
-            return new StringJoiner(", ", description + " {", "}").
-                    add("size=" + exp.size());
+        StringBuilder joiner(String description) {
+            return new StringBuilder(description).
+                    append(" {").
+                    append("size=").append(exp.size()).
+                    append("}");
         }
     }
 
@@ -482,9 +610,11 @@
             add(description, exp, s);
         }
 
-        StringJoiner joiner(String description) {
-            return new StringJoiner(", ", description + " {", "}").
-                    add("size=" + exp.size());
+        StringBuilder joiner(String description) {
+            return new StringBuilder(description).
+                    append(" {").
+                    append("size=").append(exp.size()).
+                    append("}");
         }
     }
 
@@ -632,9 +762,11 @@
             add(description, exp, s);
         }
 
-        StringJoiner joiner(String description) {
-            return new StringJoiner(", ", description + " {", "}").
-                    add("size=" + exp.size());
+        StringBuilder joiner(String description) {
+            return new StringBuilder(description).
+                    append(" {").
+                    append("size=").append(exp.size()).
+                    append("}");
         }
     }
 
@@ -927,8 +1059,8 @@
     }
 
     private static <T, S extends Spliterator<T>> void visit(int depth, int curLevel,
-                              List<T> dest, S spliterator, UnaryOperator<Consumer<T>> boxingAdapter,
-                              int rootCharacteristics, boolean useTryAdvance) {
+                                                            List<T> dest, S spliterator, UnaryOperator<Consumer<T>> boxingAdapter,
+                                                            int rootCharacteristics, boolean useTryAdvance) {
         if (curLevel < depth) {
             long beforeSize = spliterator.getExactSizeIfKnown();
             Spliterator<T> split = spliterator.trySplit();
@@ -1057,13 +1189,13 @@
                 assertTrue(leftSplit.estimateSize() < parentEstimateSize,
                            String.format("Left split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
                 assertTrue(parentAndRightSplit.estimateSize() < parentEstimateSize,
-                            String.format("Right split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
+                           String.format("Right split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
             }
             else {
                 assertTrue(leftSplit.estimateSize() <= parentEstimateSize,
-                    String.format("Left split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
+                           String.format("Left split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
                 assertTrue(parentAndRightSplit.estimateSize() <= parentEstimateSize,
-                    String.format("Right split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
+                           String.format("Right split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
             }
 
             long leftSize = leftSplit.getExactSizeIfKnown();