NotFileFilter.java

  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. import java.io.File;
  19. import java.io.Serializable;
  20. import java.nio.file.FileVisitResult;
  21. import java.nio.file.Path;
  22. import java.nio.file.attribute.BasicFileAttributes;
  23. import java.util.Objects;

  24. /**
  25.  * This filter produces a logical NOT of the filters specified.
  26.  * <h2>Deprecating Serialization</h2>
  27.  * <p>
  28.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  29.  * </p>
  30.  *
  31.  * @since 1.0
  32.  * @see FileFilterUtils#notFileFilter(IOFileFilter)
  33.  */
  34. public class NotFileFilter extends AbstractFileFilter implements Serializable {

  35.     private static final long serialVersionUID = 6131563330944994230L;

  36.     /** The filter */
  37.     private final IOFileFilter filter;

  38.     /**
  39.      * Constructs a new file filter that NOTs the result of another filter.
  40.      *
  41.      * @param filter the filter, must not be null
  42.      * @throws NullPointerException if the filter is null
  43.      */
  44.     public NotFileFilter(final IOFileFilter filter) {
  45.         Objects.requireNonNull(filter, "filter");
  46.         this.filter = filter;
  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.      * 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 file name
  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.      * Returns the logical NOT of the underlying filter's return value for the same File.
  71.      *
  72.      * @param file the File to check
  73.      * @param attributes the path's basic attributes (may be null).
  74.      * @return true if the filter returns false
  75.      * @since 2.9.0
  76.      */
  77.     @Override
  78.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  79.         return not(filter.accept(file, attributes));
  80.     }

  81.     private FileVisitResult not(final FileVisitResult accept) {
  82.         return accept == FileVisitResult.CONTINUE ? FileVisitResult.TERMINATE : FileVisitResult.CONTINUE;
  83.     }

  84.     /**
  85.      * Provide a String representation of this file filter.
  86.      *
  87.      * @return a String representation
  88.      */
  89.     @Override
  90.     public String toString() {
  91.         return "NOT (" + filter.toString() + ")";
  92.     }

  93. }