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