PathVisitorFileFilter.java

  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. package org.apache.commons.io.filefilter;

  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.nio.file.FileVisitResult;
  21. import java.nio.file.Files;
  22. import java.nio.file.Path;
  23. import java.nio.file.attribute.BasicFileAttributes;

  24. import org.apache.commons.io.file.NoopPathVisitor;
  25. import org.apache.commons.io.file.PathUtils;
  26. import org.apache.commons.io.file.PathVisitor;

  27. /**
  28.  * A file filter backed by a path visitor.
  29.  *
  30.  * @since 2.9.0
  31.  */
  32. public class PathVisitorFileFilter extends AbstractFileFilter {

  33.     private final PathVisitor pathVisitor;

  34.     /**
  35.      * Constructs a new instance that will forward calls to the given visitor.
  36.      *
  37.      * @param pathVisitor visit me.
  38.      */
  39.     public PathVisitorFileFilter(final PathVisitor pathVisitor) {
  40.         this.pathVisitor = pathVisitor == null ? NoopPathVisitor.INSTANCE : pathVisitor;
  41.     }

  42.     @Override
  43.     public boolean accept(final File file) {
  44.         try {
  45.             final Path path = file.toPath();
  46.             return visitFile(path, file.exists() ? PathUtils.readBasicFileAttributes(path) : null) == FileVisitResult.CONTINUE;
  47.         } catch (final IOException e) {
  48.             return handle(e) == FileVisitResult.CONTINUE;
  49.         }
  50.     }

  51.     @Override
  52.     public boolean accept(final File dir, final String name) {
  53.         try {
  54.             final Path path = dir.toPath().resolve(name);
  55.             return accept(path, PathUtils.readBasicFileAttributes(path)) == FileVisitResult.CONTINUE;
  56.         } catch (final IOException e) {
  57.             return handle(e) == FileVisitResult.CONTINUE;
  58.         }
  59.     }

  60.     @Override
  61.     public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
  62.         return get(() -> Files.isDirectory(path) ? pathVisitor.postVisitDirectory(path, null) : visitFile(path, attributes));
  63.     }

  64.     @Override
  65.     public FileVisitResult visitFile(final Path path, final BasicFileAttributes attributes) throws IOException {
  66.         return pathVisitor.visitFile(path, attributes);
  67.     }

  68. }