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 false if the input is null.
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 NullIsFalsePredicate implements Predicate, PredicateDecorator, Serializable {
032    
033        /** Serial version UID */
034        private static final long serialVersionUID = -2997501534564735525L;
035        
036        /** The predicate to decorate */
037        private final Predicate iPredicate;
038        
039        /**
040         * Factory to create the null false 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 NullIsFalsePredicate(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 NullIsFalsePredicate(Predicate predicate) {
060            super();
061            iPredicate = predicate;
062        }
063    
064        /**
065         * Evaluates the predicate returning the result of the decorated predicate
066         * once a null check is performed.
067         * 
068         * @param object  the input object
069         * @return true if decorated predicate returns true, false if input is null
070         */
071        public boolean evaluate(Object object) {
072            if (object == null) {
073                return false;
074            }
075            return iPredicate.evaluate(object);
076        }
077    
078        /**
079         * Gets the predicate being decorated.
080         * 
081         * @return the predicate as the only element in an array
082         * @since Commons Collections 3.1
083         */
084        public Predicate[] getPredicates() {
085            return new Predicate[] {iPredicate};
086        }
087    
088    }