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 */
017package org.apache.commons.functor.core.composite;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.Iterator;
022import java.util.List;
023
024import org.apache.commons.functor.BinaryProcedure;
025
026/**
027 * A {@link BinaryProcedure BinaryProcedure}
028 * that {@link BinaryProcedure#run runs} an ordered
029 * sequence of {@link BinaryProcedure BinaryProcedures}.
030 * When the sequence is empty, this procedure is does
031 * nothing.
032 * <p>
033 * Note that although this class implements
034 * {@link Serializable}, a given instance will
035 * only be truly <code>Serializable</code> if all the
036 * underlying functors are.  Attempts to serialize
037 * an instance whose delegates are not all
038 * <code>Serializable</code> will result in an exception.
039 * </p>
040 *
041 * @param <L> the procedure left argument type.
042 * @param <R> the procedure right argument type.
043 * @version $Revision: 1345136 $ $Date: 2012-06-01 08:47:06 -0400 (Fri, 01 Jun 2012) $
044 */
045public class BinarySequence<L, R> implements BinaryProcedure<L, R>, Serializable {
046    /**
047     * serialVersionUID declaration.
048     */
049    private static final long serialVersionUID = 1371075584406178258L;
050    // attributes
051    // ------------------------------------------------------------------------
052    /**
053     * A list where storing all the procedures references.
054     */
055    private final List<BinaryProcedure<? super L, ? super R>> list =
056        new ArrayList<BinaryProcedure<? super L, ? super R>>();
057
058    // constructor
059    // ------------------------------------------------------------------------
060    /**
061     * Create a new BinarySequence.
062     */
063    public BinarySequence() {
064        super();
065    }
066
067    /**
068     * Create a new BinarySequence instance.
069     *
070     * @param procedures to run sequentially
071     */
072    public BinarySequence(BinaryProcedure<? super L, ? super R>... procedures) {
073        this();
074        if (procedures != null) {
075            for (BinaryProcedure<? super L, ? super R> p : procedures) {
076                then(p);
077            }
078        }
079    }
080
081    /**
082     * Create a new BinarySequence instance.
083     *
084     * @param procedures to run sequentially
085     */
086    public BinarySequence(Iterable<BinaryProcedure<? super L, ? super R>> procedures) {
087        this();
088        if (procedures != null) {
089            for (BinaryProcedure<? super L, ? super R> p : procedures) {
090                then(p);
091            }
092        }
093    }
094
095    /**
096     * Fluently add a BinaryProcedure.
097     * @param p BinaryProcedure to add
098     * @return this
099     */
100    public final BinarySequence<L, R> then(BinaryProcedure<? super L, ? super R> p) {
101        list.add(p);
102        return this;
103    }
104
105    // predicate interface
106    // ------------------------------------------------------------------------
107    /**
108     * {@inheritDoc}
109     */
110    public final void run(L left, R right) {
111        for (Iterator<BinaryProcedure<? super L, ? super R>> iter = list.iterator(); iter.hasNext();) {
112            iter.next().run(left, right);
113        }
114    }
115
116    /**
117     * {@inheritDoc}
118     */
119    @Override
120    public final boolean equals(Object that) {
121        return that == this || (that instanceof BinarySequence<?, ?> && equals((BinarySequence<?, ?>) that));
122    }
123
124    /**
125     * Learn whether another BinarySequence is equal to this.
126     * @param that BinarySequence to test
127     * @return boolean
128     */
129    public final boolean equals(BinarySequence<?, ?> that) {
130        // by construction, list is never null
131        return null != that && list.equals(that.list);
132    }
133
134    /**
135     * {@inheritDoc}
136     */
137    @Override
138    public int hashCode() {
139        // by construction, list is never null
140        return "BinarySequence".hashCode() ^ list.hashCode();
141    }
142
143    /**
144     * {@inheritDoc}
145     */
146    @Override
147    public String toString() {
148        return "BinarySequence<" + list + ">";
149    }
150
151}