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