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