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