1 /*
2 * Copyright (c) 2007, 2011, 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
100 * List<Path> result = new ArrayList<>();
101 * try (DirectoryStream<Path> 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 /**
123 * An interface that is implemented by objects that decide if a directory
124 * entry should be accepted or filtered. A {@code Filter} is passed as the
125 * parameter to the {@link Files#newDirectoryStream(Path,DirectoryStream.Filter)}
126 * method when opening a directory to iterate over the entries in the
127 * directory.
128 *
129 * @param <T> the type of the directory entry
130 *
131 * @since 1.7
132 */
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
141 *
142 * @throws IOException
143 * If an I/O error occurs
144 */
145 boolean accept(T entry) throws IOException;
146 }
147
148 /**
149 * Returns the iterator associated with this {@code DirectoryStream}.
150 *
151 * @return the iterator associated with this {@code DirectoryStream}
152 *
|
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
100 * List<Path> result = new ArrayList<>();
101 * try (DirectoryStream<Path> 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
141 *
142 * @throws IOException
143 * If an I/O error occurs
144 */
145 boolean accept(T entry) throws IOException;
146 }
147
148 /**
149 * Returns the iterator associated with this {@code DirectoryStream}.
150 *
151 * @return the iterator associated with this {@code DirectoryStream}
152 *
|