View Javadoc
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  
19  import java.io.File;
20  import java.io.FileFilter;
21  import java.io.FilenameFilter;
22  import java.nio.file.FileVisitResult;
23  import java.nio.file.Path;
24  import java.nio.file.PathMatcher;
25  import java.nio.file.attribute.BasicFileAttributes;
26  
27  import org.apache.commons.io.file.PathFilter;
28  
29  /**
30   * An interface which brings the {@link FileFilter}, {@link FilenameFilter}, {@link PathFilter}, and {@link PathMatcher} interfaces together.
31   *
32   * @since 1.0
33   */
34  public interface IOFileFilter extends FileFilter, FilenameFilter, PathFilter, PathMatcher {
35  
36      /**
37       * An empty String array.
38       */
39      String[] EMPTY_STRING_ARRAY = {};
40  
41      /**
42       * Tests if a File should be accepted by this filter.
43       * <p>
44       * Defined in {@link java.io.FileFilter}.
45       * </p>
46       *
47       * @param file the File to check.
48       * @return true if this file matches the test.
49       */
50      @Override
51      boolean accept(File file);
52  
53      /**
54       * Tests if a File should be accepted by this filter.
55       * <p>
56       * Defined in {@link java.io.FilenameFilter}.
57       * </p>
58       *
59       * @param dir  the directory File to check.
60       * @param name the file name within the directory to check.
61       * @return true if this file matches the test.
62       */
63      @Override
64      boolean accept(File dir, String name);
65  
66      /**
67       * Checks to see if a Path should be accepted by this filter.
68       *
69       * @param path the Path to check.
70       * @return true if this path matches the test.
71       * @since 2.9.0
72       */
73      @Override
74      default FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
75          return AbstractFileFilter.toDefaultFileVisitResult(path != null && accept(path.toFile()));
76      }
77  
78      /**
79       * Constructs a new "and" filter with this filter.
80       *
81       * @param fileFilter the filter to "and".
82       * @return a new filter.
83       * @since 2.9.0
84       */
85      default IOFileFilter and(final IOFileFilter fileFilter) {
86          return new AndFileFilter(this, fileFilter);
87      }
88  
89      /**
90       * Tests if a Path should be accepted by this filter.
91       *
92       * @param path the Path to check.
93       * @return true if this path matches the test.
94       * @since 2.14.0
95       */
96      @Override
97      default boolean matches(final Path path) {
98          return accept(path, null) != FileVisitResult.TERMINATE;
99      }
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 }