< prev index next >

src/java.base/share/classes/java/nio/file/DirectoryStream.java

Print this page
rev 53930 : 8148917: enhanced-for statement should allow streams
Reviewed-by: XXX
   1 /*
   2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio.file;
  27 
  28 import java.util.Iterator;
  29 import java.io.Closeable;
  30 import java.io.IOException;
  31 
  32 /**
  33  * An object to iterate over the entries in a directory. A directory stream
  34  * allows for the convenient use of the for-each construct to iterate over a
  35  * directory.
  36  *
  37  * <p> <b> While {@code DirectoryStream} extends {@code Iterable}, it is not a
  38  * general-purpose {@code Iterable} as it supports only a single {@code
  39  * Iterator}; invoking the {@link #iterator iterator} method to obtain a second
  40  * or subsequent iterator throws {@code IllegalStateException}. </b>
  41  *
  42  * <p> An important property of the directory stream's {@code Iterator} is that
  43  * its {@link Iterator#hasNext() hasNext} method is guaranteed to read-ahead by
  44  * at least one element. If {@code hasNext} method returns {@code true}, and is
  45  * followed by a call to the {@code next} method, it is guaranteed that the
  46  * {@code next} method will not throw an exception due to an I/O error, or
  47  * because the stream has been {@link #close closed}. The {@code Iterator} does
  48  * not support the {@link Iterator#remove remove} operation.
  49  *
  50  * <p> A {@code DirectoryStream} is opened upon creation and is closed by
  51  * invoking the {@code close} method. Closing a directory stream releases any
  52  * resources associated with the stream. Failure to close the stream may result
  53  * in a resource leak. The try-with-resources statement provides a useful
  54  * construct to ensure that the stream is closed:
  55  * <pre>
  56  *   Path dir = ...
  57  *   try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir)) {
  58  *       for (Path entry: stream) {
  59  *           ...
  60  *       }


 100  *       List&lt;Path&gt; result = new ArrayList&lt;&gt;();
 101  *       try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) {
 102  *           for (Path entry: stream) {
 103  *               result.add(entry);
 104  *           }
 105  *       } catch (DirectoryIteratorException ex) {
 106  *           // I/O error encounted during the iteration, the cause is an IOException
 107  *           throw ex.getCause();
 108  *       }
 109  *       return result;
 110  *   }
 111  * </pre>
 112  * @param   <T>     The type of element returned by the iterator
 113  *
 114  * @since 1.7
 115  *
 116  * @see Files#newDirectoryStream(Path)
 117  */
 118 
 119 public interface DirectoryStream<T>
 120     extends Closeable, Iterable<T> {
 121     /**
 122      * An interface that is implemented by objects that decide if a directory
 123      * entry should be accepted or filtered. A {@code Filter} is passed as the
 124      * parameter to the {@link Files#newDirectoryStream(Path,DirectoryStream.Filter)}
 125      * method when opening a directory to iterate over the entries in the
 126      * directory.
 127      *
 128      * @param   <T>     the type of the directory entry
 129      *
 130      * @since 1.7
 131      */
 132     @FunctionalInterface
 133     public static interface Filter<T> {
 134         /**
 135          * Decides if the given directory entry should be accepted or filtered.
 136          *
 137          * @param   entry
 138          *          the directory entry to be tested
 139          *
 140          * @return  {@code true} if the directory entry should be accepted
   1 /*
   2  * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio.file;
  27 
  28 import java.util.Iterator;
  29 import java.io.Closeable;
  30 import java.io.IOException;
  31 
  32 /**
  33  * An object to iterate over the entries in a directory. A directory stream
  34  * allows for the convenient use of the for-each construct to iterate over a
  35  * directory.
  36  *
  37  * <p> {@code DirectoryStream} extends {@code IterableOnce}. It is not a
  38  * general-purpose {@code Iterable} as it supports only a single {@code
  39  * Iterator}; invoking the {@link #iterator iterator} method to obtain a second
  40  * or subsequent iterator throws {@code IllegalStateException}.
  41  *
  42  * <p> An important property of the directory stream's {@code Iterator} is that
  43  * its {@link Iterator#hasNext() hasNext} method is guaranteed to read-ahead by
  44  * at least one element. If {@code hasNext} method returns {@code true}, and is
  45  * followed by a call to the {@code next} method, it is guaranteed that the
  46  * {@code next} method will not throw an exception due to an I/O error, or
  47  * because the stream has been {@link #close closed}. The {@code Iterator} does
  48  * not support the {@link Iterator#remove remove} operation.
  49  *
  50  * <p> A {@code DirectoryStream} is opened upon creation and is closed by
  51  * invoking the {@code close} method. Closing a directory stream releases any
  52  * resources associated with the stream. Failure to close the stream may result
  53  * in a resource leak. The try-with-resources statement provides a useful
  54  * construct to ensure that the stream is closed:
  55  * <pre>
  56  *   Path dir = ...
  57  *   try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir)) {
  58  *       for (Path entry: stream) {
  59  *           ...
  60  *       }


 100  *       List&lt;Path&gt; result = new ArrayList&lt;&gt;();
 101  *       try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) {
 102  *           for (Path entry: stream) {
 103  *               result.add(entry);
 104  *           }
 105  *       } catch (DirectoryIteratorException ex) {
 106  *           // I/O error encounted during the iteration, the cause is an IOException
 107  *           throw ex.getCause();
 108  *       }
 109  *       return result;
 110  *   }
 111  * </pre>
 112  * @param   <T>     The type of element returned by the iterator
 113  *
 114  * @since 1.7
 115  *
 116  * @see Files#newDirectoryStream(Path)
 117  */
 118 
 119 public interface DirectoryStream<T>
 120     extends Closeable, IterableOnce<T> {
 121     /**
 122      * An interface that is implemented by objects that decide if a directory
 123      * entry should be accepted or filtered. A {@code Filter} is passed as the
 124      * parameter to the {@link Files#newDirectoryStream(Path,DirectoryStream.Filter)}
 125      * method when opening a directory to iterate over the entries in the
 126      * directory.
 127      *
 128      * @param   <T>     the type of the directory entry
 129      *
 130      * @since 1.7
 131      */
 132     @FunctionalInterface
 133     public static interface Filter<T> {
 134         /**
 135          * Decides if the given directory entry should be accepted or filtered.
 136          *
 137          * @param   entry
 138          *          the directory entry to be tested
 139          *
 140          * @return  {@code true} if the directory entry should be accepted
< prev index next >