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    import org.apache.commons.functor.UnaryProcedure;
023    import org.apache.commons.functor.core.NoOp;
024    
025    /**
026     * A {@link UnaryProcedure UnaryProcedure}
027     * similiar to Java's "ternary"
028     * or "conditional" operator (<code>&#x3F; &#x3A;</code>).
029     * Given a {@link UnaryPredicate predicate}
030     * <i>p</i> and {@link UnaryProcedure procedures}
031     * <i>q</i> and <i>r</i>, {@link #run runs}
032     * <code>if (p.test(x)) { q.run(x); } else { r.run(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: 1170782 $ $Date: 2011-09-14 21:18:31 +0200 (Wed, 14 Sep 2011) $
042     * @author Rodney Waldhoff
043     */
044    public final class ConditionalUnaryProcedure<A> implements UnaryProcedure<A>, Serializable {
045        /**
046         * serialVersionUID declaration.
047         */
048        private static final long serialVersionUID = -895833369740247391L;
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 UnaryProcedure<? super A> thenProc;
056        private final UnaryProcedure<? super A> elseProc;
057    
058        // constructor
059        // ------------------------------------------------------------------------
060        /**
061         * Create a new ConditionalUnaryProcedure.
062         * @param ifPred if
063         * @param thenProc then
064         */
065        public ConditionalUnaryProcedure(UnaryPredicate<? super A> ifPred, UnaryProcedure<? super A> thenProc) {
066            this(ifPred, thenProc, NoOp.instance());
067        }
068    
069        /**
070         * Create a new ConditionalUnaryProcedure.
071         * @param ifPred if
072         * @param thenProc then
073         * @param elseProc else
074         */
075        public ConditionalUnaryProcedure(UnaryPredicate<? super A> ifPred,
076                UnaryProcedure<? super A> thenProc,
077                UnaryProcedure<? super A> elseProc) {
078            if (ifPred == null) {
079                throw new IllegalArgumentException("UnaryPredicate argument was null");
080            }
081            this.ifPred = ifPred;
082            if (thenProc == null || elseProc == null) {
083                throw new IllegalArgumentException("One or more UnaryProcedure arguments was null");
084            }
085            this.thenProc = thenProc;
086            this.elseProc = elseProc;
087        }
088    
089        // predicate interface
090        // ------------------------------------------------------------------------
091        /**
092         * {@inheritDoc}
093         */
094        public void run(A obj) {
095            if (ifPred.test(obj)) {
096                thenProc.run(obj);
097            } else {
098                elseProc.run(obj);
099            }
100        }
101    
102        /**
103         * {@inheritDoc}
104         */
105        public boolean equals(Object that) {
106            return that == this || (that instanceof ConditionalUnaryProcedure<?>
107                                        && equals((ConditionalUnaryProcedure<?>) that));
108        }
109    
110        /**
111         * Learn whether another ConditionalUnaryProcedure is equal to this.
112         * @param that ConditionalUnaryProcedure to test
113         * @return boolean
114         */
115        public boolean equals(ConditionalUnaryProcedure<?> that) {
116            return null != that
117                    && (null == ifPred ? null == that.ifPred : ifPred.equals(that.ifPred))
118                    && (null == thenProc ? null == that.thenProc : thenProc.equals(that.thenProc))
119                    && (null == elseProc ? null == that.elseProc : elseProc.equals(that.elseProc));
120        }
121    
122        /**
123         * {@inheritDoc}
124         */
125        public int hashCode() {
126            int hash = "ConditionalUnaryProcedure".hashCode();
127            if (null != ifPred) {
128                hash <<= HASH_SHIFT;
129                hash ^= ifPred.hashCode();
130            }
131            if (null != thenProc) {
132                hash <<= HASH_SHIFT;
133                hash ^= thenProc.hashCode();
134            }
135            if (null != elseProc) {
136                hash <<= HASH_SHIFT;
137                hash ^= elseProc.hashCode();
138            }
139            return hash;
140        }
141    
142        /**
143         * {@inheritDoc}
144         */
145        public String toString() {
146            return "ConditionalUnaryProcedure<" + ifPred + "?" + thenProc + ":" + elseProc + ">";
147        }
148    
149    }