changeset 8419:6e58ff50ba5b

inner class for iterator/enumerator for ZipFile refine Spliterator characters based on feedback
author henryjen
date Mon, 29 Apr 2013 21:19:36 -0700
parents 95948a87a8e0
children b168e7e5eb62
files src/share/classes/java/util/BitSet.java src/share/classes/java/util/jar/JarFile.java src/share/classes/java/util/zip/ZipFile.java
diffstat 3 files changed, 89 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/BitSet.java	Mon Apr 29 10:05:50 2013 -0700
+++ b/src/share/classes/java/util/BitSet.java	Mon Apr 29 21:19:36 2013 -0700
@@ -1197,7 +1197,11 @@
      * in order, from lowest to highest. The size of the stream
      * is the number of bits in the set state, equal to the value
      * returned by the {@link #cardinality()} method.
-
+     *
+     * <p>The bit set must remain constant during the execution of the
+     * terminal stream operation.  Otherwise, the result of the terminal
+     * stream operation is undefined.
+     *
      * @return a stream of integers representing set indices
      * @since 1.8
      */
--- a/src/share/classes/java/util/jar/JarFile.java	Mon Apr 29 10:05:50 2013 -0700
+++ b/src/share/classes/java/util/jar/JarFile.java	Mon Apr 29 21:19:36 2013 -0700
@@ -237,36 +237,41 @@
         return null;
     }
 
+    protected class JarEntryIterator implements Enumeration<JarEntry>,
+            Iterator<JarEntry>
+    {
+        final Iterator<ZipEntry> e = new ZipFile.ZipEntryIterator();
+
+        public boolean hasMoreElements() {
+            return e.hasNext();
+        }
+
+        public JarEntry nextElement() {
+            return next();
+        }
+
+        public boolean hasNext() {
+            return e.hasNext();
+        }
+
+        public JarEntry next() {
+            ZipEntry ze = e.next();
+            return new JarFileEntry(ze);
+        }
+    }
+
     /**
      * Returns an enumeration of the zip file entries.
      */
     public Enumeration<JarEntry> entries() {
-        final Enumeration<? extends ZipEntry> enum_ = super.entries();
-        return new Enumeration<JarEntry>() {
-            public boolean hasMoreElements() {
-                return enum_.hasMoreElements();
-            }
-            public JarFileEntry nextElement() {
-                ZipEntry ze = enum_.nextElement();
-                return new JarFileEntry(ze);
-            }
-        };
+        return new JarEntryIterator();
     }
 
     @Override
     public Stream<JarEntry> stream() {
-        final Enumeration<JarEntry> entries = entries();
-        return StreamSupport.stream(Spliterators.spliterator(new Iterator<JarEntry>() {
-            @Override
-            public boolean hasNext() {
-                return entries.hasMoreElements();
-            }
-
-            @Override
-            public JarEntry next() {
-                return entries.nextElement();
-            }
-        }, size(), Spliterator.ORDERED));
+        return StreamSupport.stream(Spliterators.spliterator(
+                new JarEntryIterator(), size(),
+                Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL));
     }
 
     private class JarFileEntry extends JarEntry {
--- a/src/share/classes/java/util/zip/ZipFile.java	Mon Apr 29 10:05:50 2013 -0700
+++ b/src/share/classes/java/util/zip/ZipFile.java	Mon Apr 29 21:19:36 2013 -0700
@@ -475,73 +475,79 @@
         return name;
     }
 
+    protected class ZipEntryIterator implements Enumeration<ZipEntry>, Iterator<ZipEntry> {
+        private int i = 0;
+
+        public ZipEntryIterator() {
+            ensureOpen();
+        }
+
+        public boolean hasMoreElements() {
+            return hasNext();
+        }
+
+        public boolean hasNext() {
+            synchronized (ZipFile.this) {
+                ensureOpen();
+                return i < total;
+            }
+        }
+
+        public ZipEntry nextElement() {
+            return next();
+        }
+
+        public ZipEntry next() {
+            synchronized (ZipFile.this) {
+                ensureOpen();
+                if (i >= total) {
+                    throw new NoSuchElementException();
+                }
+                long jzentry = getNextEntry(jzfile, i++);
+                if (jzentry == 0) {
+                    String message;
+                    if (closeRequested) {
+                        message = "ZipFile concurrently closed";
+                    } else {
+                        message = getZipMessage(ZipFile.this.jzfile);
+                    }
+                    throw new ZipError("jzentry == 0" +
+                                       ",\n jzfile = " + ZipFile.this.jzfile +
+                                       ",\n total = " + ZipFile.this.total +
+                                       ",\n name = " + ZipFile.this.name +
+                                       ",\n i = " + i +
+                                       ",\n message = " + message
+                        );
+                }
+                ZipEntry ze = getZipEntry(null, jzentry);
+                freeEntry(jzfile, jzentry);
+                return ze;
+            }
+        }
+    }
+
     /**
      * Returns an enumeration of the ZIP file entries.
      * @return an enumeration of the ZIP file entries
      * @throws IllegalStateException if the zip file has been closed
      */
     public Enumeration<? extends ZipEntry> entries() {
-        ensureOpen();
-        return new Enumeration<ZipEntry>() {
-                private int i = 0;
-                public boolean hasMoreElements() {
-                    synchronized (ZipFile.this) {
-                        ensureOpen();
-                        return i < total;
-                    }
-                }
-                public ZipEntry nextElement() throws NoSuchElementException {
-                    synchronized (ZipFile.this) {
-                        ensureOpen();
-                        if (i >= total) {
-                            throw new NoSuchElementException();
-                        }
-                        long jzentry = getNextEntry(jzfile, i++);
-                        if (jzentry == 0) {
-                            String message;
-                            if (closeRequested) {
-                                message = "ZipFile concurrently closed";
-                            } else {
-                                message = getZipMessage(ZipFile.this.jzfile);
-                            }
-                            throw new ZipError("jzentry == 0" +
-                                               ",\n jzfile = " + ZipFile.this.jzfile +
-                                               ",\n total = " + ZipFile.this.total +
-                                               ",\n name = " + ZipFile.this.name +
-                                               ",\n i = " + i +
-                                               ",\n message = " + message
-                                );
-                        }
-                        ZipEntry ze = getZipEntry(null, jzentry);
-                        freeEntry(jzfile, jzentry);
-                        return ze;
-                    }
-                }
-            };
+        return new ZipEntryIterator();
     }
 
     /**
-     * Create an ordered {@code Stream} over the ZIP file entries.
+     * Return an ordered {@code Stream} over the ZIP file entries.
      * Entries appear in the {@code Stream} in the order they appear in
      * the ZIP file.
      *
-     * @return a {@code Stream} of entries in this ZIP file
+     * @return an ordered {@code Stream} of entries in this ZIP file
      * @throws IllegalStateException if the zip file has been closed
      * @since 1.8
      */
     public Stream<? extends ZipEntry> stream() {
-        final Enumeration<? extends ZipEntry> entries = entries();
-        return StreamSupport.stream(Spliterators.spliterator(new Iterator<ZipEntry>() {
-            @Override
-            public boolean hasNext() {
-                return entries.hasMoreElements();
-            }
-
-            @Override
-            public ZipEntry next() {
-                return entries.nextElement();
-            }
-        }, size(), Spliterator.ORDERED));
+        return StreamSupport.stream(Spliterators.spliterator(
+                new ZipEntryIterator(), size(),
+                Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL));
     }
 
     private ZipEntry getZipEntry(String name, long jzentry) {