    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.io.filefilter;
    018
    019import java.io.File;
    020import java.io.Serializable;
    021import java.nio.file.FileVisitResult;
    022import java.nio.file.Path;
    023import java.nio.file.attribute.BasicFileAttributes;
    024import java.util.Objects;
    025
    026/**
    027 * This filter produces a logical NOT of the filters specified.
    028 * <h2>Deprecating Serialization</h2>
    029 * <p>
    030 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    031 * </p>
    032 *
    033 * @since 1.0
    034 * @see FileFilterUtils#notFileFilter(IOFileFilter)
    035 */
    036public class NotFileFilter extends AbstractFileFilter implements Serializable {
    037
    038    private static final long serialVersionUID = 6131563330944994230L;
    039
    040    /** The filter */
    041    private final IOFileFilter filter;
    042
    043    /**
    044     * Constructs a new file filter that NOTs the result of another filter.
    045     *
    046     * @param filter the filter, must not be null
    047     * @throws NullPointerException if the filter is null
    048     */
    049    public NotFileFilter(final IOFileFilter filter) {
    050        Objects.requireNonNull(filter, "filter");
    051        this.filter = filter;
    052    }
    053
    054    /**
    055     * Returns the logical NOT of the underlying filter's return value for the same File.
    056     *
    057     * @param file the File to check
    058     * @return true if the filter returns false
    059     */
    060    @Override
    061    public boolean accept(final File file) {
    062        return !filter.accept(file);
    063    }
    064
    065    /**
    066     * Returns the logical NOT of the underlying filter's return value for the same arguments.
    067     *
    068     * @param file the File directory
    069     * @param name the file name
    070     * @return true if the filter returns false
    071     */
    072    @Override
    073    public boolean accept(final File file, final String name) {
    074        return !filter.accept(file, name);
    075    }
    076
    077    /**
    078     * Returns the logical NOT of the underlying filter's return value for the same File.
    079     * @param file the File to check
    080     *
    081     * @return true if the filter returns false
    082     * @since 2.9.0
    083     */
    084    @Override
    085    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    086        return not(filter.accept(file, attributes));
    087    }
    088
    089    private FileVisitResult not(final FileVisitResult accept) {
    090        return accept == FileVisitResult.CONTINUE ? FileVisitResult.TERMINATE : FileVisitResult.CONTINUE;
    091    }
    092
    093    /**
    094     * Provide a String representation of this file filter.
    095     *
    096     * @return a String representation
    097     */
    098    @Override
    099    public String toString() {
    100        return "NOT (" + filter.toString() + ")";
    101    }
    102
    103}