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.collections.functors;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.collections.Predicate;
022    
023    /**
024     * Predicate implementation that returns the opposite of the decorated predicate.
025     * 
026     * @since Commons Collections 3.0
027     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
028     *
029     * @author Stephen Colebourne
030     */
031    public final class NotPredicate implements Predicate, PredicateDecorator, Serializable {
032    
033        /** Serial version UID */
034        private static final long serialVersionUID = -2654603322338049674L;
035        
036        /** The predicate to decorate */
037        private final Predicate iPredicate;
038        
039        /**
040         * Factory to create the not predicate.
041         * 
042         * @param predicate  the predicate to decorate, not null
043         * @return the predicate
044         * @throws IllegalArgumentException if the predicate is null
045         */
046        public static Predicate getInstance(Predicate predicate) {
047            if (predicate == null) {
048                throw new IllegalArgumentException("Predicate must not be null");
049            }
050            return new NotPredicate(predicate);
051        }
052    
053        /**
054         * Constructor that performs no validation.
055         * Use <code>getInstance</code> if you want that.
056         * 
057         * @param predicate  the predicate to call after the null check
058         */
059        public NotPredicate(Predicate predicate) {
060            super();
061            iPredicate = predicate;
062        }
063    
064        /**
065         * Evaluates the predicate returning the opposite to the stored predicate.
066         * 
067         * @param object  the input object
068         * @return true if predicate returns false
069         */
070        public boolean evaluate(Object object) {
071            return !(iPredicate.evaluate(object));
072        }
073    
074        /**
075         * Gets the predicate being decorated.
076         * 
077         * @return the predicate as the only element in an array
078         * @since Commons Collections 3.1
079         */
080        public Predicate[] getPredicates() {
081            return new Predicate[] {iPredicate};
082        }
083    
084    }