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;
021
022/**
023 * This filter produces a logical NOT of the filters specified.
024 *
025 * @since 1.0
026 * @version $Id: NotFileFilter.java 1642757 2014-12-01 21:09:30Z sebb $
027 * @see FileFilterUtils#notFileFilter(IOFileFilter)
028 */
029public class NotFileFilter extends AbstractFileFilter implements Serializable {
030
031    private static final long serialVersionUID = 6131563330944994230L;
032    /** The filter */
033    private final IOFileFilter filter;
034
035    /**
036     * Constructs a new file filter that NOTs the result of another filter.
037     *
038     * @param filter  the filter, must not be null
039     * @throws IllegalArgumentException if the filter is null
040     */
041    public NotFileFilter(final IOFileFilter filter) {
042        if (filter == null) {
043            throw new IllegalArgumentException("The filter must not be null");
044        }
045        this.filter = filter;
046    }
047
048    /**
049     * Returns the logical NOT of the underlying filter's return value for the same File.
050     *
051     * @param file  the File to check
052     * @return true if the filter returns false
053     */
054    @Override
055    public boolean accept(final File file) {
056        return ! filter.accept(file);
057    }
058
059    /**
060     * Returns the logical NOT of the underlying filter's return value for the same arguments.
061     *
062     * @param file  the File directory
063     * @param name  the filename
064     * @return true if the filter returns false
065     */
066    @Override
067    public boolean accept(final File file, final String name) {
068        return ! filter.accept(file, name);
069    }
070
071    /**
072     * Provide a String representaion of this file filter.
073     *
074     * @return a String representaion
075     */
076    @Override
077    public String toString() {
078        return super.toString() + "(" + filter.toString()  + ")";
079    }
080
081}