    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;
    024
    025/**
    026 * This filter produces a logical NOT of the filters specified.
    027 *
    028 * @since 1.0
    029 * @see FileFilterUtils#notFileFilter(IOFileFilter)
    030 */
    031public class NotFileFilter extends AbstractFileFilter implements Serializable {
    032
    033    private static final long serialVersionUID = 6131563330944994230L;
    034
    035    /** The filter */
    036    private final IOFileFilter filter;
    037
    038    /**
    039     * Constructs a new file filter that NOTs the result of another filter.
    040     *
    041     * @param filter the filter, must not be null
    042     * @throws IllegalArgumentException if the filter is null
    043     */
    044    public NotFileFilter(final IOFileFilter filter) {
    045        if (filter == null) {
    046            throw new IllegalArgumentException("The filter must not be null");
    047        }
    048        this.filter = filter;
    049    }
    050
    051    /**
    052     * Returns the logical NOT of the underlying filter's return value for the same File.
    053     *
    054     * @param file the File to check
    055     * @return true if the filter returns false
    056     */
    057    @Override
    058    public boolean accept(final File file) {
    059        return !filter.accept(file);
    060    }
    061
    062    /**
    063     * Returns the logical NOT of the underlying filter's return value for the same arguments.
    064     *
    065     * @param file the File directory
    066     * @param name the file name
    067     * @return true if the filter returns false
    068     */
    069    @Override
    070    public boolean accept(final File file, final String name) {
    071        return !filter.accept(file, name);
    072    }
    073
    074    /**
    075     * Returns the logical NOT of the underlying filter's return value for the same File.
    076     * @param file the File to check
    077     *
    078     * @return true if the filter returns false
    079     * @since 2.9.0
    080     */
    081    @Override
    082    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    083        return not(filter.accept(file, attributes));
    084    }
    085
    086    private FileVisitResult not(final FileVisitResult accept) {
    087        return accept == FileVisitResult.CONTINUE ? FileVisitResult.TERMINATE
    088            : FileVisitResult.CONTINUE;
    089    }
    090
    091    /**
    092     * Provide a String representation of this file filter.
    093     *
    094     * @return a String representation
    095     */
    096    @Override
    097    public String toString() {
    098        return "NOT (" + filter.toString() + ")";
    099    }
    100
    101}