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.UnaryPredicate;
022    
023    /**
024     * A {@link UnaryPredicate UnaryPredicate}
025     * similiar to Java's "ternary"
026     * or "conditional" operator (<code>&#x3F; &#x3A;</code>).
027     * Given three {@link UnaryPredicate predicate}
028     * <i>p</i>, <i>q</i>, <i>r</i>,
029     * {@link #test tests}
030     * to
031     * <code>p.test(x) ? q.test(x) : r.test(x)</code>.
032     * <p>
033     * Note that although this class implements
034     * {@link Serializable}, a given instance will
035     * only be truly <code>Serializable</code> if all the
036     * underlying functors are.  Attempts to serialize
037     * an instance whose delegates are not all
038     * <code>Serializable</code> will result in an exception.
039     * </p>
040     * @version $Revision: 1170779 $ $Date: 2011-09-14 21:16:47 +0200 (Wed, 14 Sep 2011) $
041     * @author Rodney Waldhoff
042     */
043    public final class ConditionalUnaryPredicate<A> implements UnaryPredicate<A>, Serializable {
044        /**
045         * serialVersionUID declaration.
046         */
047        private static final long serialVersionUID = 1214714029872180155L;
048    
049        /** Base hash integer used to shift hash */
050        private static final int HASH_SHIFT = 4;
051        // attributes
052        // ------------------------------------------------------------------------
053        private final UnaryPredicate<? super A> ifPred;
054        private final UnaryPredicate<? super A> thenPred;
055        private final UnaryPredicate<? super A> elsePred;
056    
057        // constructor
058        // ------------------------------------------------------------------------
059        /**
060         * Create a new ConditionalUnaryPredicate.
061         * @param ifPred if
062         * @param thenPred then
063         * @param elsePred else
064         */
065        public ConditionalUnaryPredicate(UnaryPredicate<? super A> ifPred, UnaryPredicate<? super A> thenPred,
066                UnaryPredicate<? super A> elsePred) {
067            if (ifPred == null || thenPred == null || elsePred == null) {
068                throw new IllegalArgumentException("One or more UnaryPredicate arguments was null");
069            }
070            this.ifPred = ifPred;
071            this.thenPred = thenPred;
072            this.elsePred = elsePred;
073        }
074    
075        // predicate interface
076        // ------------------------------------------------------------------------
077        /**
078         * {@inheritDoc}
079         */
080        public boolean test(A obj) {
081            return ifPred.test(obj) ? thenPred.test(obj) : elsePred.test(obj);
082        }
083    
084        /**
085         * {@inheritDoc}
086         */
087        public boolean equals(Object that) {
088            return that == this || (that instanceof ConditionalUnaryPredicate<?>
089                                        && equals((ConditionalUnaryPredicate<?>) that));
090        }
091    
092        /**
093         * Learn whether another ConditionalUnaryPredicate is equal to this.
094         * @param that ConditionalUnaryPredicate to test
095         * @return boolean
096         */
097        public boolean equals(ConditionalUnaryPredicate<?> that) {
098            return null != that
099                    && (null == ifPred ? null == that.ifPred : ifPred.equals(that.ifPred))
100                    && (null == thenPred ? null == that.thenPred : thenPred.equals(that.thenPred))
101                    && (null == elsePred ? null == that.elsePred : elsePred.equals(that.elsePred));
102        }
103    
104        /**
105         * {@inheritDoc}
106         */
107        public int hashCode() {
108            int hash = "ConditionalUnaryPredicate".hashCode();
109            if (null != ifPred) {
110                hash <<= HASH_SHIFT;
111                hash ^= ifPred.hashCode();
112            }
113            if (null != thenPred) {
114                hash <<= HASH_SHIFT;
115                hash ^= thenPred.hashCode();
116            }
117            if (null != elsePred) {
118                hash <<= HASH_SHIFT;
119                hash ^= elsePred.hashCode();
120            }
121            return hash;
122        }
123    
124        /**
125         * {@inheritDoc}
126         */
127        public String toString() {
128            return "ConditionalUnaryPredicate<" + ifPred + "?" + thenPred + ":" + elsePred + ">";
129        }
130    
131    }