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
23  * questions.
24  */
25 
26 package java.nio.file;
27 
28 import java.nio.file.attribute.BasicFileAttributes;
29 import java.io.IOException;
30 import java.util.Objects;
31 
32 /**
33  * A simple visitor of files with default behavior to visit all files and to
34  * re-throw I/O errors.
35  *
36  * <p> Methods in this class may be overridden subject to their general contract.
37  *
38  * @param   <T>     The type of reference to the files
39  *
40  * @since 1.7
41  */
42 
43 public class SimpleFileVisitor<T> implements FileVisitor<T> {
44     /**
45      * Initializes a new instance of this class.
46      */
SimpleFileVisitor()47     protected SimpleFileVisitor() {
48     }
49 
50     /**
51      * Invoked for a directory before entries in the directory are visited.
52      *
53      * <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
54      * CONTINUE}.
55      */
56     @Override
preVisitDirectory(T dir, BasicFileAttributes attrs)57     public FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs)
58         throws IOException
59     {
60         Objects.requireNonNull(dir);
61         Objects.requireNonNull(attrs);
62         return FileVisitResult.CONTINUE;
63     }
64 
65     /**
66      * Invoked for a file in a directory.
67      *
68      * <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
69      * CONTINUE}.
70      */
71     @Override
visitFile(T file, BasicFileAttributes attrs)72     public FileVisitResult visitFile(T file, BasicFileAttributes attrs)
73         throws IOException
74     {
75         Objects.requireNonNull(file);
76         Objects.requireNonNull(attrs);
77         return FileVisitResult.CONTINUE;
78     }
79 
80     /**
81      * Invoked for a file that could not be visited.
82      *
83      * <p> Unless overridden, this method re-throws the I/O exception that prevented
84      * the file from being visited.
85      */
86     @Override
visitFileFailed(T file, IOException exc)87     public FileVisitResult visitFileFailed(T file, IOException exc)
88         throws IOException
89     {
90         Objects.requireNonNull(file);
91         throw exc;
92     }
93 
94     /**
95      * Invoked for a directory after entries in the directory, and all of their
96      * descendants, have been visited.
97      *
98      * <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
99      * CONTINUE} if the directory iteration completes without an I/O exception;
100      * otherwise this method re-throws the I/O exception that caused the iteration
101      * of the directory to terminate prematurely.
102      */
103     @Override
postVisitDirectory(T dir, IOException exc)104     public FileVisitResult postVisitDirectory(T dir, IOException exc)
105         throws IOException
106     {
107         Objects.requireNonNull(dir);
108         if (exc != null)
109             throw exc;
110         return FileVisitResult.CONTINUE;
111     }
112 }
113