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