changeset 5994:51c6636447cd it2-bootstrap

avoid creating Mapping in cases where it's not actually needed.
author mduigou
date Wed, 12 Sep 2012 19:13:40 -0700
parents c94321c10623
children 78d4439127f2
files src/share/classes/java/util/MapIterator.java src/share/classes/java/util/streams/ops/BiFilterOp.java src/share/classes/java/util/streams/ops/BiMapOp.java src/share/classes/java/util/streams/ops/MapFilterKeysOp.java src/share/classes/java/util/streams/ops/MapFilterValuesOp.java src/share/classes/java/util/streams/ops/MapMapValuesOp.java
diffstat 6 files changed, 297 insertions(+), 144 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/MapIterator.java	Wed Sep 12 13:29:57 2012 -0700
+++ b/src/share/classes/java/util/MapIterator.java	Wed Sep 12 19:13:40 2012 -0700
@@ -38,18 +38,14 @@
      *
      * @return The current key after advancing the iterator or the first key.
      */
-    K nextKey() default {
-        return next().getKey();
-    }
+    K nextKey();
 
     /**
      * Advance the iterator and return the current value.
      *
      * @return The current key after advancing the iterator or the first key.
      */
-    V nextValue() default {
-        return next().getValue();
-    }
+    V nextValue();
 
     /**
      * Return the current key.
@@ -92,6 +88,16 @@
         }
 
         @Override
+        public K nextKey() {
+            return next().getKey();
+        }
+
+        @Override
+        public V nextValue() {
+            return next().getValue();
+        }
+
+        @Override
         public K curKey() {
             if (null != current) {
                 return current.getKey();
--- a/src/share/classes/java/util/streams/ops/BiFilterOp.java	Wed Sep 12 13:29:57 2012 -0700
+++ b/src/share/classes/java/util/streams/ops/BiFilterOp.java	Wed Sep 12 19:13:40 2012 -0700
@@ -85,26 +85,33 @@
         return new FilteredMapIterator<>(source, predicate);
     }
 
-    private static class FilteredMapIterator<K, V> implements MapIterator<K,V> {
+    private static class FilteredMapIterator<K, V> implements MapIterator<K, V> {
 
-        final Iterator<Mapping<K, V>> source;
-        final BiPredicate<? super K, ? super V> predicate;
-        Mapping<K,V> current;
-        Mapping<K,V> next;
+        private static final Object NO_CURRENT = new Object();
+        private final MapIterator<K, V> source;
+        private final BiPredicate<? super K, ? super V> predicate;
+        Object nextKey = NO_CURRENT;
+        Object nextValue;
+        Object currentKey = NO_CURRENT;
+        Object currentValue;
 
-        FilteredMapIterator(Iterator<Mapping<K, V>> source, BiPredicate<? super K, ? super V> predicate) {
+        public FilteredMapIterator(MapIterator<K, V> source,
+                BiPredicate<? super K, ? super V> predicate) {
             this.source = Objects.requireNonNull(source);
             this.predicate = Objects.requireNonNull(predicate);
         }
 
         @Override
         public boolean hasNext() {
-            while ((null == next) && source.hasNext()) {
-                next = source.next();
-                next = predicate.test(next.getKey(), next.getValue()) ? next : null;
+            while ((NO_CURRENT == nextKey) && source.hasNext()) {
+                nextKey = source.nextKey();
+                nextValue = source.curValue();
+                if(!predicate.test((K)nextKey, (V) nextValue)) {
+                    nextKey = nextValue = NO_CURRENT;
+                }
             }
 
-            return null != next;
+            return NO_CURRENT != nextKey;
         }
 
         @Override
@@ -113,28 +120,55 @@
                  throw new NoSuchElementException();
             }
 
-            current = next;
-            next = null;
+            currentKey = nextKey;
+            currentValue = nextValue;
+            nextKey = nextValue = NO_CURRENT;
 
-            return current;
+            return new Mapping.MappingValue<>((K) currentKey, (V) currentValue);
+        }
+
+        @Override
+        public K nextKey() {
+            if(!hasNext()) {
+                 throw new NoSuchElementException();
+            }
+
+            currentKey = nextKey;
+            currentValue = nextValue;
+            nextKey = nextValue = NO_CURRENT;
+
+            return (K) currentKey;
+        }
+
+        @Override
+        public V nextValue() {
+            if(!hasNext()) {
+                 throw new NoSuchElementException();
+            }
+
+            currentKey = nextKey;
+            currentValue = nextValue;
+            nextKey = nextValue = NO_CURRENT;
+
+            return (V) currentValue;
         }
 
         @Override
         public K curKey() {
-            if (null == current) {
+            if (NO_CURRENT == currentKey) {
                 throw new NoSuchElementException("No Current Element");
             }
 
-            return current.getKey();
+            return (K) currentKey;
         }
 
         @Override
         public V curValue() {
-           if (null == current) {
+            if (NO_CURRENT == currentKey) {
                 throw new NoSuchElementException("No Current Element");
             }
 
-            return current.getValue();
+            return (V) currentValue;
         }
     }
 }
--- a/src/share/classes/java/util/streams/ops/BiMapOp.java	Wed Sep 12 13:29:57 2012 -0700
+++ b/src/share/classes/java/util/streams/ops/BiMapOp.java	Wed Sep 12 19:13:40 2012 -0700
@@ -36,12 +36,12 @@
 
 /**
  * A mapping operation utilizing a BiMapper.
- *
+ * <p/>
  * @param <K> Type of mapping keys.
  * @param <V> Type of input mapping values.
  * @param <U> Type of output mapping values.
  */
-public class BiMapOp<K, V, U> implements StatelessOp<Mapping<K,V>, Mapping<K,U>> {
+public class BiMapOp<K, V, U> implements StatelessOp<Mapping<K, V>, Mapping<K, U>> {
 
     /**
      * Mapping for this operation.
@@ -68,21 +68,21 @@
     }
 
     @Override
-    public MapIterator<K,U> wrapIterator(final Iterator<Mapping<K, V>> source) {
-        if(source instanceof MapIterator) {
-            return iterator((MapIterator<K,V>) source, mapper);
+    public MapIterator<K, U> wrapIterator(final Iterator<Mapping<K, V>> source) {
+        if (source instanceof MapIterator) {
+            return iterator((MapIterator<K, V>)source, mapper);
         }
 
         return iterator(new MapIterator.IteratorAdapter(source), mapper);
     }
 
-    public MapIterator<K,U> wrapIterator(final MapIterator<K, V> source) {
+    public MapIterator<K, U> wrapIterator(final MapIterator<K, V> source) {
         return iterator(source, mapper);
     }
 
     @Override
     public Sink<Mapping<K, V>, K, V> wrapSink(Sink sink) {
-        return new Sink.ChainedMap<K,V>(sink) {
+        return new Sink.ChainedMap<K, V>(sink) {
             @Override
             public void accept(K k, V v) {
                 downstream.accept(k, mapper.map(k, v));
@@ -90,53 +90,68 @@
         };
     }
 
-    public static <K, V, VV> MapIterator<K, VV> iterator(final MapIterator<K, V> source,
-                                                         final BiMapper<? super K, ? super V, ? extends VV> mapper) {
-        Objects.requireNonNull(source);
-        Objects.requireNonNull(mapper);
-        return new MapIterator<K, VV>() {
-            Mapping<K, VV> current;
+    public static <K, V, VV> MapIterator<K, VV> iterator(MapIterator<K, V> source,
+            BiMapper<? super K, ? super V, ? extends VV> mapper) {
 
-            @Override
-            public boolean hasNext() {
-                return source.hasNext();
+        return new MappedMapIterator<>(source, mapper);
+    }
+
+    private static class MappedMapIterator<K, V, VV> implements MapIterator<K, VV> {
+
+        private static final Object NO_CURRENT = new Object();
+        private final MapIterator<K, V> source;
+        private final BiMapper<? super K, ? super V, ? extends VV> mapper;
+        Object currentKey = NO_CURRENT;
+        VV currentValue;
+
+        public MappedMapIterator(MapIterator<K, V> source,
+                BiMapper<? super K, ? super V, ? extends VV> mapper) {
+            this.source = Objects.requireNonNull(source);
+            this.mapper = Objects.requireNonNull(mapper);
+        }
+
+        @Override
+        public boolean hasNext() {
+            return source.hasNext();
+        }
+
+        @Override
+        public Mapping<K, VV> next() {
+            currentKey = source.nextKey();
+            currentValue = mapper.map((K) currentKey, source.curValue());
+            return new Mapping.MappingValue<>((K) currentKey, currentValue);
+        }
+
+        @Override
+        public K nextKey() {
+            currentKey = source.nextKey();
+            currentValue = mapper.map((K) currentKey, source.curValue());
+            return (K) currentKey;
+        }
+
+        @Override
+        public VV nextValue() {
+            currentKey = source.nextKey();
+            currentValue = mapper.map((K) currentKey, source.curValue());
+            return currentValue;
+        }
+
+        @Override
+        public K curKey() {
+            if (NO_CURRENT == currentKey) {
+                throw new NoSuchElementException("No Current Element");
             }
 
-            @Override
-            public Mapping<K, VV> next() {
-                K k = source.nextKey();
-                V v = source.curValue();
-                current = new Mapping.MappingValue<>(k, mapper.map(k, v));
-                return current;
+            return (K) currentKey;
+        }
+
+        @Override
+        public VV curValue() {
+            if (NO_CURRENT == currentKey) {
+                throw new NoSuchElementException("No Current Element");
             }
 
-            @Override
-            public K nextKey() {
-                return next().getKey();
-            }
-
-            @Override
-            public VV nextValue() {
-                return next().getValue();
-            }
-
-            @Override
-            public K curKey() {
-                if(null == current) {
-                    throw new NoSuchElementException("No Current Element");
-                }
-
-                return current.getKey();
-            }
-
-            @Override
-            public VV curValue() {
-                 if(null == current) {
-                    throw new NoSuchElementException("No Current Element");
-                }
-
-                return current.getValue();
-            }
-        };
-    }
+            return currentValue;
+        }
+    };
 }
--- a/src/share/classes/java/util/streams/ops/MapFilterKeysOp.java	Wed Sep 12 13:29:57 2012 -0700
+++ b/src/share/classes/java/util/streams/ops/MapFilterKeysOp.java	Wed Sep 12 19:13:40 2012 -0700
@@ -90,26 +90,33 @@
         return new FilteredMapIterator<>(source, predicate);
     }
 
-    private static class FilteredMapIterator<K, V> implements MapIterator<K,V> {
+    private static class FilteredMapIterator<K, V> implements MapIterator<K, V> {
 
-        final Iterator<Mapping<K, V>> source;
-        final Predicate<? super K> predicate;
-        Mapping<K,V> current;
-        Mapping<K,V> next;
+        private static final Object NO_CURRENT = new Object();
+        private final MapIterator<K, V> source;
+        private final Predicate<? super K> predicate;
+        Object nextKey = NO_CURRENT;
+        Object nextValue;
+        Object currentKey = NO_CURRENT;
+        Object currentValue;
 
-        FilteredMapIterator(Iterator<Mapping<K, V>> source, Predicate<? super K> predicate) {
+        public FilteredMapIterator(MapIterator<K, V> source,
+                Predicate<? super K> predicate) {
             this.source = Objects.requireNonNull(source);
             this.predicate = Objects.requireNonNull(predicate);
         }
 
         @Override
         public boolean hasNext() {
-            while ((null == next) && source.hasNext()) {
-                next = source.next();
-                next = predicate.test(next.getKey()) ? next : null;
+            while ((NO_CURRENT == nextKey) && source.hasNext()) {
+                nextKey = source.nextKey();
+                nextValue = source.curValue();
+                if(!predicate.test((K)nextKey)) {
+                    nextKey = nextValue = NO_CURRENT;
+                }
             }
 
-            return null != next;
+            return NO_CURRENT != nextKey;
         }
 
         @Override
@@ -118,28 +125,55 @@
                  throw new NoSuchElementException();
             }
 
-            current = next;
-            next = null;
+            currentKey = nextKey;
+            currentValue = nextValue;
+            nextKey = nextValue = NO_CURRENT;
 
-            return current;
+            return new Mapping.MappingValue<>((K) currentKey, (V) currentValue);
+        }
+
+        @Override
+        public K nextKey() {
+            if(!hasNext()) {
+                 throw new NoSuchElementException();
+            }
+
+            currentKey = nextKey;
+            currentValue = nextValue;
+            nextKey = nextValue = NO_CURRENT;
+
+            return (K) currentKey;
+        }
+
+        @Override
+        public V nextValue() {
+            if(!hasNext()) {
+                 throw new NoSuchElementException();
+            }
+
+            currentKey = nextKey;
+            currentValue = nextValue;
+            nextKey = nextValue = NO_CURRENT;
+
+            return (V) currentValue;
         }
 
         @Override
         public K curKey() {
-            if (null == current) {
+            if (NO_CURRENT == currentKey) {
                 throw new NoSuchElementException("No Current Element");
             }
 
-            return current.getKey();
+            return (K) currentKey;
         }
 
         @Override
         public V curValue() {
-           if (null == current) {
+            if (NO_CURRENT == currentKey) {
                 throw new NoSuchElementException("No Current Element");
             }
 
-            return current.getValue();
+            return (V) currentValue;
         }
     }
 }
--- a/src/share/classes/java/util/streams/ops/MapFilterValuesOp.java	Wed Sep 12 13:29:57 2012 -0700
+++ b/src/share/classes/java/util/streams/ops/MapFilterValuesOp.java	Wed Sep 12 19:13:40 2012 -0700
@@ -61,7 +61,16 @@
         return StreamShape.KEY_VALUE;
     }
 
+    @Override
     public MapIterator<K,V> wrapIterator(final Iterator<Mapping<K, V>> source) {
+        if(source instanceof MapIterator) {
+            return iterator((MapIterator<K,V>) source, predicate);
+        }
+
+        return iterator(new MapIterator.IteratorAdapter(source), predicate);
+    }
+
+    public MapIterator<K,V> wrapIterator(final MapIterator<K, V> source) {
         return iterator(source, predicate);
     }
 
@@ -77,34 +86,37 @@
         };
     }
 
-    public static <K, V> MapIterator<K, V> iterator(Iterator<Mapping<K, V>> source, Predicate<? super V> predicate) {
+    public static <K, V> MapIterator<K, V> iterator(MapIterator<K, V> source, Predicate<? super V> predicate) {
         return new FilteredMapIterator<>(source, predicate);
     }
 
-    /**
-     * @param <K> Type of element keys.
-     * @param <V> Type of element values.
-     */
-        private static class FilteredMapIterator<K, V> implements MapIterator<K,V> {
+        private static class FilteredMapIterator<K, V> implements MapIterator<K, V> {
 
-        final Iterator<Mapping<K, V>> source;
-        final Predicate<? super V> predicate;
-        Mapping<K,V> current;
-        Mapping<K,V> next;
+        private static final Object NO_CURRENT = new Object();
+        private final MapIterator<K, V> source;
+        private final Predicate<? super V> predicate;
+        Object nextKey = NO_CURRENT;
+        Object nextValue;
+        Object currentKey = NO_CURRENT;
+        Object currentValue;
 
-        FilteredMapIterator(Iterator<Mapping<K, V>> source, Predicate<? super V> predicate) {
+        public FilteredMapIterator(MapIterator<K, V> source,
+                Predicate<? super V> predicate) {
             this.source = Objects.requireNonNull(source);
             this.predicate = Objects.requireNonNull(predicate);
         }
 
         @Override
         public boolean hasNext() {
-            while ((null == next) && source.hasNext()) {
-                next = source.next();
-                next = predicate.test(next.getValue()) ? next : null;
+            while ((NO_CURRENT == nextKey) && source.hasNext()) {
+                nextKey = source.nextKey();
+                nextValue = source.curValue();
+                if(!predicate.test((V) nextValue)) {
+                    nextKey = nextValue = NO_CURRENT;
+                }
             }
 
-            return null != next;
+            return NO_CURRENT != nextKey;
         }
 
         @Override
@@ -113,28 +125,55 @@
                  throw new NoSuchElementException();
             }
 
-            current = next;
-            next = null;
+            currentKey = nextKey;
+            currentValue = nextValue;
+            nextKey = nextValue = NO_CURRENT;
 
-            return current;
+            return new Mapping.MappingValue<>((K) currentKey, (V) currentValue);
+        }
+
+        @Override
+        public K nextKey() {
+            if(!hasNext()) {
+                 throw new NoSuchElementException();
+            }
+
+            currentKey = nextKey;
+            currentValue = nextValue;
+            nextKey = nextValue = NO_CURRENT;
+
+            return (K) currentKey;
+        }
+
+        @Override
+        public V nextValue() {
+            if(!hasNext()) {
+                 throw new NoSuchElementException();
+            }
+
+            currentKey = nextKey;
+            currentValue = nextValue;
+            nextKey = nextValue = NO_CURRENT;
+
+            return (V) currentValue;
         }
 
         @Override
         public K curKey() {
-            if (null == current) {
+            if (NO_CURRENT == currentKey) {
                 throw new NoSuchElementException("No Current Element");
             }
 
-            return current.getKey();
+            return (K) currentKey;
         }
 
         @Override
         public V curValue() {
-           if (null == current) {
+            if (NO_CURRENT == currentKey) {
                 throw new NoSuchElementException("No Current Element");
             }
 
-            return current.getValue();
+            return (V) currentValue;
         }
     }
 }
--- a/src/share/classes/java/util/streams/ops/MapMapValuesOp.java	Wed Sep 12 13:29:57 2012 -0700
+++ b/src/share/classes/java/util/streams/ops/MapMapValuesOp.java	Wed Sep 12 19:13:40 2012 -0700
@@ -90,41 +90,66 @@
 
     public static <K, V, U> MapIterator<K, U> iterator(final MapIterator<K, V> source,
                                                          final Mapper<? super V, ? extends U> mapper) {
-        Objects.requireNonNull(source);
-        Objects.requireNonNull(mapper);
-        return new MapIterator<K, U>() {
-            Mapping<K, U> current;
+        return new MappedMapIterator(source, mapper);
 
-            @Override
-            public boolean hasNext() {
-                return source.hasNext();
+    }
+
+    private static class MappedMapIterator<K, V, VV> implements MapIterator<K, VV> {
+
+        private static final Object NO_CURRENT = new Object();
+        private final MapIterator<K, V> source;
+        private final Mapper<? super V, ? extends VV> mapper;
+        Object currentKey = NO_CURRENT;
+        VV currentValue;
+
+        public MappedMapIterator(MapIterator<K, V> source,
+                Mapper<? super V, ? extends VV> mapper) {
+            this.source = Objects.requireNonNull(source);
+            this.mapper = Objects.requireNonNull(mapper);
+        }
+
+        @Override
+        public boolean hasNext() {
+            return source.hasNext();
+        }
+
+        @Override
+        public Mapping<K, VV> next() {
+            currentKey = source.nextKey();
+            currentValue = mapper.map(source.curValue());
+            return new Mapping.MappingValue<>((K) currentKey, currentValue);
+        }
+
+        @Override
+        public K nextKey() {
+            currentKey = source.nextKey();
+            currentValue = mapper.map(source.curValue());
+            return (K) currentKey;
+        }
+
+        @Override
+        public VV nextValue() {
+            currentKey = source.nextKey();
+            currentValue = mapper.map(source.curValue());
+            return currentValue;
+        }
+
+        @Override
+        public K curKey() {
+            if (NO_CURRENT == currentKey) {
+                throw new NoSuchElementException("No Current Element");
             }
 
-            @Override
-            public Mapping<K, U> next() {
-                K k = source.nextKey();
-                V v = source.curValue();
-                current = new Mapping.MappingValue<>(k, mapper.map(v));
-                return current;
+            return (K) currentKey;
+        }
+
+        @Override
+        public VV curValue() {
+            if (NO_CURRENT == currentKey) {
+                throw new NoSuchElementException("No Current Element");
             }
 
-            @Override
-            public K curKey() {
-                if(null == current) {
-                    throw new NoSuchElementException("No Current Element");
-                }
-
-                return current.getKey();
-            }
-
-            @Override
-            public U curValue() {
-                 if(null == current) {
-                    throw new NoSuchElementException("No Current Element");
-                }
-
-                return current.getValue();
-            }
-        };
+            return currentValue;
+        }
     }
 }