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.functor.core.composite;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.functor.Predicate;
022    
023    /**
024     * {@link #test Tests} to the logical inverse
025     * of some other predicate.
026     * <p>
027     * Note that although this class implements
028     * {@link Serializable}, a given instance will
029     * only be truly <code>Serializable</code> if the
030     * underlying functor is.  Attempts to serialize
031     * an instance whose delegate is not
032     * <code>Serializable</code> will result in an exception.
033     * </p>
034     * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
035     * @author Rodney Waldhoff
036     */
037    public final class Not implements Predicate, Serializable {
038    
039        /**
040         * serialVersionUID declaration.
041         */
042        private static final long serialVersionUID = 8268713706856765874L;
043        // attributes
044        // ------------------------------------------------------------------------
045        private final Predicate predicate;
046    
047        // constructor
048        // ------------------------------------------------------------------------
049        /**
050         * Create a new Not.
051         * @param predicate Predicate to negate
052         */
053        public Not(Predicate predicate) {
054            if (predicate == null) {
055                throw new IllegalArgumentException("Predicate argument was null");
056            }
057            this.predicate = predicate;
058        }
059    
060        // predicate interface
061        // ------------------------------------------------------------------------
062        /**
063         * {@inheritDoc}
064         */
065        public boolean test() {
066            return !(predicate.test());
067        }
068    
069        /**
070         * {@inheritDoc}
071         */
072        public boolean equals(Object that) {
073            return that == this || (that instanceof Not && equals((Not) that));
074        }
075    
076        /**
077         * Learn whether another Not is equal to this.
078         * @param that the Not to test
079         * @return boolean
080         */
081        public boolean equals(Not that) {
082            return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate));
083        }
084    
085        /**
086         * {@inheritDoc}
087         */
088        public int hashCode() {
089            int hash = "Not".hashCode();
090            if (null != predicate) {
091                hash ^= predicate.hashCode();
092            }
093            return hash;
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        public String toString() {
100            return "Not<" + predicate + ">";
101        }
102    
103        // static
104        // ------------------------------------------------------------------------
105        /**
106         * Get a Not instance for <code>that</code>.
107         * @param that Predicate to negate
108         * @return Not
109         */
110        public static Predicate not(Predicate that) {
111            return null == that ? null : new Not(that);
112        }
113    }