    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 *      https://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.nio.file.FileVisitResult;
    023import java.nio.file.Path;
    024import java.nio.file.PathMatcher;
    025import java.nio.file.attribute.BasicFileAttributes;
    026
    027import org.apache.commons.io.file.PathFilter;
    028
    029/**
    030 * An interface which brings the {@link FileFilter}, {@link FilenameFilter}, {@link PathFilter}, and {@link PathMatcher} interfaces together.
    031 *
    032 * @since 1.0
    033 */
    034public interface IOFileFilter extends FileFilter, FilenameFilter, PathFilter, PathMatcher {
    035
    036    /**
    037     * An empty String array.
    038     */
    039    String[] EMPTY_STRING_ARRAY = {};
    040
    041    /**
    042     * Tests if a File should be accepted by this filter.
    043     * <p>
    044     * Defined in {@link FileFilter}.
    045     * </p>
    046     *
    047     * @param file the File to check.
    048     * @return true if this file matches the test.
    049     */
    050    @Override
    051    boolean accept(File file);
    052
    053    /**
    054     * Tests if a File should be accepted by this filter.
    055     * <p>
    056     * Defined in {@link FilenameFilter}.
    057     * </p>
    058     *
    059     * @param dir  the directory File to check.
    060     * @param name the file name within the directory to check.
    061     * @return true if this file matches the test.
    062     */
    063    @Override
    064    boolean accept(File dir, String name);
    065
    066    /**
    067     * Checks to see if a Path should be accepted by this filter.
    068     *
    069     * @param path the Path to check.
    070     * @param attributes the path's basic attributes (may be null).
    071     * @return true if this path matches the test.
    072     * @since 2.9.0
    073     */
    074    @Override
    075    default FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
    076        return AbstractFileFilter.toDefaultFileVisitResult(path != null && accept(path.toFile()));
    077    }
    078
    079    /**
    080     * Constructs a new "and" filter with this filter.
    081     *
    082     * @param fileFilter the filter to "and".
    083     * @return a new filter.
    084     * @since 2.9.0
    085     */
    086    default IOFileFilter and(final IOFileFilter fileFilter) {
    087        return new AndFileFilter(this, fileFilter);
    088    }
    089
    090    /**
    091     * Tests if a Path should be accepted by this filter.
    092     *
    093     * @param path the Path to check.
    094     * @return true if this path matches the test.
    095     * @since 2.14.0
    096     */
    097    @Override
    098    default boolean matches(final Path path) {
    099        return accept(path, null) != FileVisitResult.TERMINATE;
    100    }
    101
    102    /**
    103     * Constructs a new "not" filter with this filter.
    104     *
    105     * @return a new filter.
    106     * @since 2.9.0
    107     */
    108    default IOFileFilter negate() {
    109        return new NotFileFilter(this);
    110    }
    111
    112    /**
    113     * Constructs a new "or" filter with this filter.
    114     *
    115     * @param fileFilter the filter to "or".
    116     * @return a new filter.
    117     * @since 2.9.0
    118     */
    119    default IOFileFilter or(final IOFileFilter fileFilter) {
    120        return new OrFileFilter(this, fileFilter);
    121    }
    122
    123}