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.Serializable;
21  
22  /**
23   * This filter produces a logical NOT of the filters specified.
24   *
25   * @since 1.0
26   * @version $Id: NotFileFilter.java 1471767 2013-04-24 23:24:19Z sebb $
27   * @see FileFilterUtils#notFileFilter(IOFileFilter)
28   */
29  public class NotFileFilter extends AbstractFileFilter implements Serializable {
30  
31      /** The filter */
32      private final IOFileFilter filter;
33  
34      /**
35       * Constructs a new file filter that NOTs the result of another filter.
36       *
37       * @param filter  the filter, must not be null
38       * @throws IllegalArgumentException if the filter is null
39       */
40      public NotFileFilter(final IOFileFilter filter) {
41          if (filter == null) {
42              throw new IllegalArgumentException("The filter must not be null");
43          }
44          this.filter = filter;
45      }
46  
47      /**
48       * Returns the logical NOT of the underlying filter's return value for the same File.
49       *
50       * @param file  the File to check
51       * @return true if the filter returns false
52       */
53      @Override
54      public boolean accept(final File file) {
55          return ! filter.accept(file);
56      }
57  
58      /**
59       * Returns the logical NOT of the underlying filter's return value for the same arguments.
60       *
61       * @param file  the File directory
62       * @param name  the filename
63       * @return true if the filter returns false
64       */
65      @Override
66      public boolean accept(final File file, final String name) {
67          return ! filter.accept(file, name);
68      }
69  
70      /**
71       * Provide a String representaion of this file filter.
72       *
73       * @return a String representaion
74       */
75      @Override
76      public String toString() {
77          return super.toString() + "(" + filter.toString()  + ")";
78      }
79  
80  }