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.util.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022
023import org.apache.commons.functor.Procedure;
024
025/**
026 * A {@link Procedure Procedure}
027 * that {@link Procedure#run runs} an ordered
028 * sequence of {@link Procedure Procedures}.
029 * When the sequence is empty, this procedure is does
030 * nothing.
031 * @param <A> the argument type.
032 * @version $Revision: 1537602 $ $Date: 2013-10-31 20:53:09 +0100 (Do, 31 Okt 2013) $
033 */
034public class Sequence<A> implements Procedure<A> {
035
036    // attributes
037    // ------------------------------------------------------------------------
038    /**
039     * The data structure to store the procedure sequence.
040     */
041    private final List<Procedure<? super A>> list = new ArrayList<Procedure<? super A>>();
042
043    // constructor
044    // ------------------------------------------------------------------------
045    /**
046     * Create a new Sequence.
047     */
048    public Sequence() {
049        super();
050    }
051
052    /**
053     * Create a new Sequence instance.
054     *
055     * @param procedures to run sequentially
056     */
057    public Sequence(Procedure<? super A>... procedures) {
058        this();
059        if (procedures != null) {
060            for (Procedure<? super A> p : procedures) {
061                then(p);
062            }
063        }
064    }
065
066    /**
067     * Create a new Sequence instance.
068     *
069     * @param procedures to run sequentially
070     */
071    public Sequence(Iterable<Procedure<? super A>> procedures) {
072        this();
073        if (procedures != null) {
074            for (Procedure<? super A> p : procedures) {
075                then(p);
076            }
077        }
078    }
079
080    // modifiers
081    // ------------------------------------------------------------------------
082    /**
083     * Fluently add a Procedure to the sequence.
084     * @param p Procedure to add
085     * @return this
086     */
087    public Sequence<A> then(Procedure<? super A> p) {
088        if (p != null) {
089            list.add(p);
090        }
091        return this;
092    }
093
094    // predicate interface
095    // ------------------------------------------------------------------------
096    /**
097     * {@inheritDoc}
098     */
099    public void run(A obj) {
100        for (Iterator<Procedure<? super A>> iter = list.iterator(); iter.hasNext();) {
101            iter.next().run(obj);
102        }
103    }
104
105    /**
106     * {@inheritDoc}
107     */
108    @Override
109    public boolean equals(Object obj) {
110        if (obj == this) {
111            return true;
112        }
113        if (!(obj instanceof Sequence<?>)) {
114            return false;
115        }
116        Sequence<?> that = (Sequence<?>) obj;
117        return this.list.equals(that.list);
118    }
119
120    /**
121     * {@inheritDoc}
122     */
123    @Override
124    public int hashCode() {
125        // by construction, list is never null
126        return "Sequence".hashCode() ^ list.hashCode();
127    }
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public String toString() {
134        return "Sequence<" + list + ">";
135    }
136
137}