IOFileFilter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io.filefilter;

  18. import java.io.File;
  19. import java.io.FileFilter;
  20. import java.io.FilenameFilter;
  21. import java.nio.file.FileVisitResult;
  22. import java.nio.file.Path;
  23. import java.nio.file.PathMatcher;
  24. import java.nio.file.attribute.BasicFileAttributes;

  25. import org.apache.commons.io.file.PathFilter;

  26. /**
  27.  * An interface which brings the {@link FileFilter}, {@link FilenameFilter}, {@link PathFilter}, and {@link PathMatcher} interfaces together.
  28.  *
  29.  * @since 1.0
  30.  */
  31. public interface IOFileFilter extends FileFilter, FilenameFilter, PathFilter, PathMatcher {

  32.     /**
  33.      * An empty String array.
  34.      */
  35.     String[] EMPTY_STRING_ARRAY = {};

  36.     /**
  37.      * Tests if a File should be accepted by this filter.
  38.      * <p>
  39.      * Defined in {@link FileFilter}.
  40.      * </p>
  41.      *
  42.      * @param file the File to check.
  43.      * @return true if this file matches the test.
  44.      */
  45.     @Override
  46.     boolean accept(File file);

  47.     /**
  48.      * Tests if a File should be accepted by this filter.
  49.      * <p>
  50.      * Defined in {@link FilenameFilter}.
  51.      * </p>
  52.      *
  53.      * @param dir  the directory File to check.
  54.      * @param name the file name within the directory to check.
  55.      * @return true if this file matches the test.
  56.      */
  57.     @Override
  58.     boolean accept(File dir, String name);

  59.     /**
  60.      * Checks to see if a Path should be accepted by this filter.
  61.      *
  62.      * @param path the Path to check.
  63.      * @param attributes the path's basic attributes (may be null).
  64.      * @return true if this path matches the test.
  65.      * @since 2.9.0
  66.      */
  67.     @Override
  68.     default FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
  69.         return AbstractFileFilter.toDefaultFileVisitResult(path != null && accept(path.toFile()));
  70.     }

  71.     /**
  72.      * Constructs a new "and" filter with this filter.
  73.      *
  74.      * @param fileFilter the filter to "and".
  75.      * @return a new filter.
  76.      * @since 2.9.0
  77.      */
  78.     default IOFileFilter and(final IOFileFilter fileFilter) {
  79.         return new AndFileFilter(this, fileFilter);
  80.     }

  81.     /**
  82.      * Tests if a Path should be accepted by this filter.
  83.      *
  84.      * @param path the Path to check.
  85.      * @return true if this path matches the test.
  86.      * @since 2.14.0
  87.      */
  88.     @Override
  89.     default boolean matches(final Path path) {
  90.         return accept(path, null) != FileVisitResult.TERMINATE;
  91.     }

  92.     /**
  93.      * Constructs a new "not" filter with this filter.
  94.      *
  95.      * @return a new filter.
  96.      * @since 2.9.0
  97.      */
  98.     default IOFileFilter negate() {
  99.         return new NotFileFilter(this);
  100.     }

  101.     /**
  102.      * Constructs a new "or" filter with this filter.
  103.      *
  104.      * @param fileFilter the filter to "or".
  105.      * @return a new filter.
  106.      * @since 2.9.0
  107.      */
  108.     default IOFileFilter or(final IOFileFilter fileFilter) {
  109.         return new OrFileFilter(this, fileFilter);
  110.     }

  111. }