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,
054                file.exists() ? PathUtils.readBasicFileAttributes(path) : null) == FileVisitResult.CONTINUE;
055        } catch (final IOException e) {
056            return handle(e) == FileVisitResult.CONTINUE;
057        }
058    }
059
060    @Override
061    public boolean accept(final File dir, final String name) {
062        try {
063            final Path path = dir.toPath().resolve(name);
064            return accept(path, PathUtils.readBasicFileAttributes(path)) == FileVisitResult.CONTINUE;
065        } catch (final IOException e) {
066            return handle(e) == FileVisitResult.CONTINUE;
067        }
068    }
069
070    @Override
071    public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
072        try {
073            return Files.isDirectory(path) ? pathVisitor.postVisitDirectory(path, null) : visitFile(path, attributes);
074        } catch (final IOException e) {
075            return handle(e);
076        }
077    }
078
079    @Override
080    public FileVisitResult visitFile(final Path path, final BasicFileAttributes attributes) throws IOException {
081        return pathVisitor.visitFile(path, attributes);
082    }
083
084}