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.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 java.io.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 java.io.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     * @return true if this path matches the test.
071     * @since 2.9.0
072     */
073    @Override
074    default FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
075        return AbstractFileFilter.toDefaultFileVisitResult(path != null && accept(path.toFile()));
076    }
077
078    /**
079     * Constructs a new "and" filter with this filter.
080     *
081     * @param fileFilter the filter to "and".
082     * @return a new filter.
083     * @since 2.9.0
084     */
085    default IOFileFilter and(final IOFileFilter fileFilter) {
086        return new AndFileFilter(this, fileFilter);
087    }
088
089    /**
090     * Tests if a Path should be accepted by this filter.
091     *
092     * @param path the Path to check.
093     * @return true if this path matches the test.
094     * @since 2.14.0
095     */
096    @Override
097    default boolean matches(final Path path) {
098        return accept(path, null) != FileVisitResult.TERMINATE;
099    }
100
101    /**
102     * Constructs a new "not" filter with this filter.
103     *
104     * @return a new filter.
105     * @since 2.9.0
106     */
107    default IOFileFilter negate() {
108        return new NotFileFilter(this);
109    }
110
111    /**
112     * Constructs a new "or" filter with this filter.
113     *
114     * @param fileFilter the filter to "or".
115     * @return a new filter.
116     * @since 2.9.0
117     */
118    default IOFileFilter or(final IOFileFilter fileFilter) {
119        return new OrFileFilter(this, fileFilter);
120    }
121
122}