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