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 import java.util.ArrayList;
021 import java.util.Iterator;
022 import java.util.List;
023
024 import 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 * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $
042 * @author Rodney Waldhoff
043 */
044 public class BinarySequence<L, R> implements BinaryProcedure<L, R>, Serializable {
045 /**
046 * serialVersionUID declaration.
047 */
048 private static final long serialVersionUID = 1371075584406178258L;
049 // attributes
050 // ------------------------------------------------------------------------
051 private final List<BinaryProcedure<? super L, ? super R>> list =
052 new ArrayList<BinaryProcedure<? super L, ? super R>>();
053
054 // constructor
055 // ------------------------------------------------------------------------
056 /**
057 * Create a new BinarySequence.
058 */
059 public BinarySequence() {
060 super();
061 }
062
063 /**
064 * Create a new BinarySequence instance.
065 *
066 * @param procedures to run sequentially
067 */
068 public BinarySequence(BinaryProcedure<? super L, ? super R>... procedures) {
069 this();
070 if (procedures != null) {
071 for (BinaryProcedure<? super L, ? super R> p : procedures) {
072 then(p);
073 }
074 }
075 }
076
077 /**
078 * Create a new BinarySequence instance.
079 *
080 * @param procedures to run sequentially
081 */
082 public BinarySequence(Iterable<BinaryProcedure<? super L, ? super R>> procedures) {
083 this();
084 if (procedures != null) {
085 for (BinaryProcedure<? super L, ? super R> p : procedures) {
086 then(p);
087 }
088 }
089 }
090
091 /**
092 * Fluently add a BinaryProcedure.
093 * @param p BinaryProcedure to add
094 * @return this
095 */
096 public final BinarySequence<L, R> then(BinaryProcedure<? super L, ? super R> p) {
097 list.add(p);
098 return this;
099 }
100
101 // predicate interface
102 // ------------------------------------------------------------------------
103 /**
104 * {@inheritDoc}
105 */
106 public final void run(L left, R right) {
107 for (Iterator<BinaryProcedure<? super L, ? super R>> iter = list.iterator(); iter.hasNext();) {
108 iter.next().run(left, right);
109 }
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 public final boolean equals(Object that) {
116 return that == this || (that instanceof BinarySequence<?, ?> && equals((BinarySequence<?, ?>) that));
117 }
118
119 /**
120 * Learn whether another BinarySequence is equal to this.
121 * @param that BinarySequence to test
122 * @return boolean
123 */
124 public final boolean equals(BinarySequence<?, ?> that) {
125 // by construction, list is never null
126 return null != that && list.equals(that.list);
127 }
128
129 /**
130 * {@inheritDoc}
131 */
132 public int hashCode() {
133 // by construction, list is never null
134 return "BinarySequence".hashCode() ^ list.hashCode();
135 }
136
137 /**
138 * {@inheritDoc}
139 */
140 public String toString() {
141 return "BinarySequence<" + list + ">";
142 }
143
144 }