1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.functor.core.composite; 18 19 import java.io.Serializable; 20 import java.util.ArrayList; 21 import java.util.Iterator; 22 import java.util.List; 23 24 import org.apache.commons.functor.BinaryProcedure; 25 26 /** 27 * A {@link BinaryProcedure BinaryProcedure} 28 * that {@link BinaryProcedure#run runs} an ordered 29 * sequence of {@link BinaryProcedure BinaryProcedures}. 30 * When the sequence is empty, this procedure is does 31 * nothing. 32 * <p> 33 * Note that although this class implements 34 * {@link Serializable}, a given instance will 35 * only be truly <code>Serializable</code> if all the 36 * underlying functors are. Attempts to serialize 37 * an instance whose delegates are not all 38 * <code>Serializable</code> will result in an exception. 39 * </p> 40 * 41 * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $ 42 * @author Rodney Waldhoff 43 */ 44 public class BinarySequence<L, R> implements BinaryProcedure<L, R>, Serializable { 45 /** 46 * serialVersionUID declaration. 47 */ 48 private static final long serialVersionUID = 1371075584406178258L; 49 // attributes 50 // ------------------------------------------------------------------------ 51 private final List<BinaryProcedure<? super L, ? super R>> list = 52 new ArrayList<BinaryProcedure<? super L, ? super R>>(); 53 54 // constructor 55 // ------------------------------------------------------------------------ 56 /** 57 * Create a new BinarySequence. 58 */ 59 public BinarySequence() { 60 super(); 61 } 62 63 /** 64 * Create a new BinarySequence instance. 65 * 66 * @param procedures to run sequentially 67 */ 68 public BinarySequence(BinaryProcedure<? super L, ? super R>... procedures) { 69 this(); 70 if (procedures != null) { 71 for (BinaryProcedure<? super L, ? super R> p : procedures) { 72 then(p); 73 } 74 } 75 } 76 77 /** 78 * Create a new BinarySequence instance. 79 * 80 * @param procedures to run sequentially 81 */ 82 public BinarySequence(Iterable<BinaryProcedure<? super L, ? super R>> procedures) { 83 this(); 84 if (procedures != null) { 85 for (BinaryProcedure<? super L, ? super R> p : procedures) { 86 then(p); 87 } 88 } 89 } 90 91 /** 92 * Fluently add a BinaryProcedure. 93 * @param p BinaryProcedure to add 94 * @return this 95 */ 96 public final BinarySequence<L, R> then(BinaryProcedure<? super L, ? super R> p) { 97 list.add(p); 98 return this; 99 } 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 }