View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.io.filefilter;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.nio.file.FileVisitResult;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.attribute.BasicFileAttributes;
26  
27  import org.apache.commons.io.file.NoopPathVisitor;
28  import org.apache.commons.io.file.PathUtils;
29  import org.apache.commons.io.file.PathVisitor;
30  
31  /**
32   * A file filter backed by a path visitor.
33   *
34   * @since 2.9.0
35   */
36  public class PathVisitorFileFilter extends AbstractFileFilter {
37  
38      private final PathVisitor pathVisitor;
39  
40      /**
41       * Constructs a new instance that will forward calls to the given visitor.
42       *
43       * @param pathVisitor visit me.
44       */
45      public PathVisitorFileFilter(final PathVisitor pathVisitor) {
46          this.pathVisitor = pathVisitor == null ? NoopPathVisitor.INSTANCE : pathVisitor;
47      }
48  
49      @Override
50      public boolean accept(final File file) {
51          try {
52              final Path path = file.toPath();
53              return visitFile(path, file.exists() ? PathUtils.readBasicFileAttributes(path) : null) == FileVisitResult.CONTINUE;
54          } catch (final IOException e) {
55              return handle(e) == FileVisitResult.CONTINUE;
56          }
57      }
58  
59      @Override
60      public boolean accept(final File dir, final String name) {
61          try {
62              final Path path = dir.toPath().resolve(name);
63              return accept(path, PathUtils.readBasicFileAttributes(path)) == FileVisitResult.CONTINUE;
64          } catch (final IOException e) {
65              return handle(e) == FileVisitResult.CONTINUE;
66          }
67      }
68  
69      @Override
70      public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
71          return get(() -> Files.isDirectory(path) ? pathVisitor.postVisitDirectory(path, null) : visitFile(path, attributes));
72      }
73  
74      @Override
75      public FileVisitResult visitFile(final Path path, final BasicFileAttributes attributes) throws IOException {
76          return pathVisitor.visitFile(path, attributes);
77      }
78  
79  }