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.UnaryProcedure;
25
26 /**
27 * A {@link UnaryProcedure UnaryProcedure}
28 * that {@link UnaryProcedure#run runs} an ordered
29 * sequence of {@link UnaryProcedure UnaryProcedures}.
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 * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
41 * @author Rodney Waldhoff
42 */
43 public class UnarySequence<A> implements UnaryProcedure<A>, Serializable {
44
45 /**
46 * serialVersionUID declaration.
47 */
48 private static final long serialVersionUID = 9194268249717820246L;
49 // attributes
50 // ------------------------------------------------------------------------
51 private List<UnaryProcedure<? super A>> list = new ArrayList<UnaryProcedure<? super A>>();
52
53 // constructor
54 // ------------------------------------------------------------------------
55 /**
56 * Create a new UnarySequence.
57 */
58 public UnarySequence() {
59 super();
60 }
61
62 /**
63 * Create a new UnarySequence instance.
64 *
65 * @param procedures to run sequentially
66 */
67 public UnarySequence(UnaryProcedure<? super A>... procedures) {
68 this();
69 if (procedures != null) {
70 for (UnaryProcedure<? super A> p : procedures) {
71 then(p);
72 }
73 }
74 }
75
76 /**
77 * Create a new UnarySequence instance.
78 *
79 * @param procedures to run sequentially
80 */
81 public UnarySequence(Iterable<UnaryProcedure<? super A>> procedures) {
82 this();
83 if (procedures != null) {
84 for (UnaryProcedure<? super A> p : procedures) {
85 then(p);
86 }
87 }
88 }
89
90 // modifiers
91 // ------------------------------------------------------------------------
92 /**
93 * Fluently add a UnaryProcedure to the sequence.
94 * @param p UnaryProcedure to add
95 * @return this
96 */
97 public UnarySequence<A> then(UnaryProcedure<? super A> p) {
98 list.add(p);
99 return this;
100 }
101
102 // predicate interface
103 // ------------------------------------------------------------------------
104 /**
105 * {@inheritDoc}
106 */
107 public void run(A obj) {
108 for (Iterator<UnaryProcedure<? super A>> iter = list.iterator(); iter.hasNext();) {
109 iter.next().run(obj);
110 }
111 }
112
113 /**
114 * {@inheritDoc}
115 */
116 public boolean equals(Object that) {
117 return that == this || (that instanceof UnarySequence<?> && equals((UnarySequence<?>) that));
118 }
119
120 /**
121 * Learn whether another UnarySequence is equal to this.
122 * @param that UnarySequence to test
123 * @return boolean
124 */
125 public boolean equals(UnarySequence<?> that) {
126 // by construction, list is never null
127 return null != that && list.equals(that.list);
128 }
129
130 /**
131 * {@inheritDoc}
132 */
133 public int hashCode() {
134 // by construction, list is never null
135 return "UnarySequence".hashCode() ^ list.hashCode();
136 }
137
138 /**
139 * {@inheritDoc}
140 */
141 public String toString() {
142 return "UnarySequence<" + list + ">";
143 }
144
145 }