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 * https://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 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 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 * @param attributes the path's basic attributes (may be null).
71 * @return true if this path matches the test.
72 * @since 2.9.0
73 */
74 @Override
75 default FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
76 return AbstractFileFilter.toDefaultFileVisitResult(path != null && accept(path.toFile()));
77 }
78
79 /**
80 * Constructs a new "and" filter with this filter.
81 *
82 * @param fileFilter the filter to "and".
83 * @return a new filter.
84 * @since 2.9.0
85 */
86 default IOFileFilter and(final IOFileFilter fileFilter) {
87 return new AndFileFilter(this, fileFilter);
88 }
89
90 /**
91 * Tests if a Path should be accepted by this filter.
92 *
93 * @param path the Path to check.
94 * @return true if this path matches the test.
95 * @since 2.14.0
96 */
97 @Override
98 default boolean matches(final Path path) {
99 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 }