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.Serializable;
21 import java.nio.file.FileVisitResult;
22 import java.nio.file.Path;
23 import java.nio.file.attribute.BasicFileAttributes;
24 import java.util.Objects;
25
26 /**
27 * This filter produces a logical NOT of the filters specified.
28 * <h2>Deprecating Serialization</h2>
29 * <p>
30 * <em>Serialization is deprecated and will be removed in 3.0.</em>
31 * </p>
32 *
33 * @since 1.0
34 * @see FileFilterUtils#notFileFilter(IOFileFilter)
35 */
36 public class NotFileFilter extends AbstractFileFilter implements Serializable {
37
38 private static final long serialVersionUID = 6131563330944994230L;
39
40 /** The filter */
41 private final IOFileFilter filter;
42
43 /**
44 * Constructs a new file filter that NOTs the result of another filter.
45 *
46 * @param filter the filter, must not be null
47 * @throws NullPointerException if the filter is null
48 */
49 public NotFileFilter(final IOFileFilter filter) {
50 Objects.requireNonNull(filter, "filter");
51 this.filter = filter;
52 }
53
54 /**
55 * Returns the logical NOT of the underlying filter's return value for the same File.
56 *
57 * @param file the File to check
58 * @return true if the filter returns false
59 */
60 @Override
61 public boolean accept(final File file) {
62 return !filter.accept(file);
63 }
64
65 /**
66 * Returns the logical NOT of the underlying filter's return value for the same arguments.
67 *
68 * @param file the File directory
69 * @param name the file name
70 * @return true if the filter returns false
71 */
72 @Override
73 public boolean accept(final File file, final String name) {
74 return !filter.accept(file, name);
75 }
76
77 /**
78 * Returns the logical NOT of the underlying filter's return value for the same File.
79 *
80 * @param file the File to check
81 * @param attributes the path's basic attributes (may be null).
82 * @return true if the filter returns false
83 * @since 2.9.0
84 */
85 @Override
86 public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
87 return not(filter.accept(file, attributes));
88 }
89
90 private FileVisitResult not(final FileVisitResult accept) {
91 return accept == FileVisitResult.CONTINUE ? FileVisitResult.TERMINATE : FileVisitResult.CONTINUE;
92 }
93
94 /**
95 * Provide a String representation of this file filter.
96 *
97 * @return a String representation
98 */
99 @Override
100 public String toString() {
101 return "NOT (" + filter.toString() + ")";
102 }
103
104 }