    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 */
    017package org.apache.commons.io.filefilter;
    018
    019import java.io.File;
    020import java.io.FileFilter;
    021import java.io.FilenameFilter;
    022import java.io.IOException;
    023import java.nio.file.FileVisitResult;
    024import java.nio.file.Path;
    025import java.nio.file.attribute.BasicFileAttributes;
    026import java.util.Objects;
    027
    028import org.apache.commons.io.file.PathFilter;
    029import org.apache.commons.io.file.PathVisitor;
    030
    031/**
    032 * Abstracts the implementation of the {@link FileFilter} (IO), {@link FilenameFilter} (IO), {@link PathFilter} (NIO)
    033 * interfaces via our own {@link IOFileFilter} interface.
    034 * <p>
    035 * Note that a subclass MUST override one of the {@code accept} methods, otherwise that subclass will infinitely loop.
    036 * </p>
    037 *
    038 * @since 1.0
    039 */
    040public abstract class AbstractFileFilter implements IOFileFilter, PathVisitor {
    041
    042    static FileVisitResult toFileVisitResult(final boolean accept, final Path path) {
    043        return accept ? FileVisitResult.CONTINUE : FileVisitResult.TERMINATE;
    044    }
    045
    046    /**
    047     * Checks to see if the File should be accepted by this filter.
    048     *
    049     * @param file the File to check
    050     * @return true if this file matches the test
    051     */
    052    @Override
    053    public boolean accept(final File file) {
    054        Objects.requireNonNull(file, "file");
    055        return accept(file.getParentFile(), file.getName());
    056    }
    057
    058    /**
    059     * Checks to see if the File should be accepted by this filter.
    060     *
    061     * @param dir the directory File to check
    062     * @param name the file name within the directory to check
    063     * @return true if this file matches the test
    064     */
    065    @Override
    066    public boolean accept(final File dir, final String name) {
    067        Objects.requireNonNull(name, "name");
    068        return accept(new File(dir, name));
    069    }
    070
    071    /**
    072     * Handles exceptions caught while accepting.
    073     *
    074     * @param t the caught Throwable.
    075     * @return the given Throwable.
    076     * @since 2.9.0
    077     */
    078    protected FileVisitResult handle(final Throwable t) {
    079        return FileVisitResult.TERMINATE;
    080    }
    081
    082    @Override
    083    public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
    084        return FileVisitResult.CONTINUE;
    085    }
    086
    087    @Override
    088    public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attributes) throws IOException {
    089        return accept(dir, attributes);
    090    }
    091
    092    /**
    093     * Provides a String representation of this file filter.
    094     *
    095     * @return a String representation
    096     */
    097    @Override
    098    public String toString() {
    099        return getClass().getSimpleName();
    100    }
    101
    102    @Override
    103    public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
    104        return accept(file, attributes);
    105    }
    106
    107    @Override
    108    public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException {
    109        return FileVisitResult.CONTINUE;
    110    }
    111
    112}