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