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.Procedure; 025 026/** 027 * A {@link Procedure Procedure} 028 * that {@link Procedure#run runs} an ordered 029 * sequence of {@link Procedure Procedures}. 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 * @version $Revision: 1365329 $ $Date: 2012-07-24 18:34:23 -0400 (Tue, 24 Jul 2012) $ 041 */ 042public class Sequence implements Procedure, Serializable { 043 044 /** 045 * serialVersionUID declaration. 046 */ 047 private static final long serialVersionUID = 8041703589149547883L; 048 // attributes 049 // ------------------------------------------------------------------------ 050 /** 051 * The data structure where storing procedures sequence. 052 */ 053 private List<Procedure> list = new ArrayList<Procedure>(); 054 055 // constructor 056 // ------------------------------------------------------------------------ 057 /** 058 * Create a new Sequence. 059 */ 060 public Sequence() { 061 super(); 062 } 063 064 /** 065 * Create a new Sequence instance. 066 * 067 * @param procedures to run sequentially 068 */ 069 public Sequence(Procedure... procedures) { 070 this(); 071 if (procedures != null) { 072 for (Procedure p : procedures) { 073 then(p); 074 } 075 } 076 } 077 078 /** 079 * Create a new Sequence instance. 080 * 081 * @param procedures to run sequentially 082 */ 083 public Sequence(Iterable<Procedure> procedures) { 084 this(); 085 if (procedures != null) { 086 for (Procedure p : procedures) { 087 then(p); 088 } 089 } 090 } 091 092 // modifiers 093 // ------------------------------------------------------------------------ 094 /** 095 * Fluently add a Procedure. 096 * @param p Procedure to add 097 * @return this 098 */ 099 public final Sequence then(Procedure p) { 100 if (p != null) { 101 list.add(p); 102 } 103 return this; 104 } 105 106 // predicate interface 107 // ------------------------------------------------------------------------ 108 /** 109 * {@inheritDoc} 110 */ 111 public final void run() { 112 for (Iterator<Procedure> iter = list.iterator(); iter.hasNext();) { 113 iter.next().run(); 114 } 115 } 116 117 /** 118 * {@inheritDoc} 119 */ 120 @Override 121 public final boolean equals(Object that) { 122 return that == this || (that instanceof Sequence && equals((Sequence) that)); 123 } 124 125 /** 126 * Learn whether a given Sequence is equal to this. 127 * @param that Sequence to test 128 * @return boolean 129 */ 130 public boolean equals(Sequence that) { 131 // by construction, list is never null 132 return null != that && list.equals(that.list); 133 } 134 135 /** 136 * {@inheritDoc} 137 */ 138 @Override 139 public int hashCode() { 140 // by construction, list is never null 141 return "Sequence".hashCode() ^ list.hashCode(); 142 } 143 144 /** 145 * {@inheritDoc} 146 */ 147 @Override 148 public String toString() { 149 return "Sequence<" + list + ">"; 150 } 151 152}