    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.io.filefilter;
    019
    020import java.io.File;
    021import java.io.IOException;
    022import java.nio.file.FileVisitResult;
    023import java.nio.file.Files;
    024import java.nio.file.Path;
    025import java.nio.file.attribute.BasicFileAttributes;
    026
    027import org.apache.commons.io.file.NoopPathVisitor;
    028import org.apache.commons.io.file.PathUtils;
    029import org.apache.commons.io.file.PathVisitor;
    030
    031/**
    032 * A file filter backed by a path visitor.
    033 *
    034 * @since 2.9.0
    035 */
    036public class PathVisitorFileFilter extends AbstractFileFilter {
    037
    038    private final PathVisitor pathVisitor;
    039
    040    /**
    041     * Constructs a new instance that will forward calls to the given visitor.
    042     *
    043     * @param pathVisitor visit me.
    044     */
    045    public PathVisitorFileFilter(final PathVisitor pathVisitor) {
    046        this.pathVisitor = pathVisitor == null ? NoopPathVisitor.INSTANCE : pathVisitor;
    047    }
    048
    049    @Override
    050    public boolean accept(final File file) {
    051        try {
    052            final Path path = file.toPath();
    053            return visitFile(path, file.exists() ? PathUtils.readBasicFileAttributes(path) : null) == FileVisitResult.CONTINUE;
    054        } catch (final IOException e) {
    055            return handle(e) == FileVisitResult.CONTINUE;
    056        }
    057    }
    058
    059    @Override
    060    public boolean accept(final File dir, final String name) {
    061        try {
    062            final Path path = dir.toPath().resolve(name);
    063            return accept(path, PathUtils.readBasicFileAttributes(path)) == FileVisitResult.CONTINUE;
    064        } catch (final IOException e) {
    065            return handle(e) == FileVisitResult.CONTINUE;
    066        }
    067    }
    068
    069    @Override
    070    public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
    071        return get(() -> Files.isDirectory(path) ? pathVisitor.postVisitDirectory(path, null) : visitFile(path, attributes));
    072    }
    073
    074    @Override
    075    public FileVisitResult visitFile(final Path path, final BasicFileAttributes attributes) throws IOException {
    076        return pathVisitor.visitFile(path, attributes);
    077    }
    078
    079}