Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BaseActivity |
|
| 1.7777777777777777;1.778 |
1 | /* | |
2 | * Copyright 1999-2001,2004 The Apache Software Foundation. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package org.apache.commons.workflow.base; | |
18 | ||
19 | ||
20 | import java.util.ArrayList; | |
21 | import org.apache.commons.workflow.Activity; | |
22 | import org.apache.commons.workflow.Step; | |
23 | ||
24 | ||
25 | /** | |
26 | * <p><strong>BaseActivity</strong> is a convenient base class for more | |
27 | * sophisticated <code>Activity</code> implementations. It includes | |
28 | * management of the static relationships of Steps to each other as part | |
29 | * of an owning Activity.</p> | |
30 | * | |
31 | * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $ | |
32 | * @author Craig R. McClanahan | |
33 | */ | |
34 | ||
35 | 0 | public class BaseActivity implements Activity { |
36 | ||
37 | ||
38 | // ----------------------------------------------------- Instance Variables | |
39 | ||
40 | ||
41 | /** | |
42 | * The first Step associated with this Activity. | |
43 | */ | |
44 | 0 | protected Step firstStep = null; |
45 | ||
46 | ||
47 | /** | |
48 | * The unique identifier of this Activity. | |
49 | */ | |
50 | 0 | protected String id = null; |
51 | ||
52 | ||
53 | /** | |
54 | * The last Step associated with this Activity. | |
55 | */ | |
56 | 0 | protected Step lastStep = null; |
57 | ||
58 | ||
59 | // ------------------------------------------------------------- Properties | |
60 | ||
61 | ||
62 | /** | |
63 | * Return the first Step associated with this Activity. | |
64 | */ | |
65 | public Step getFirstStep() { | |
66 | ||
67 | 0 | return (this.firstStep); |
68 | ||
69 | } | |
70 | ||
71 | ||
72 | /** | |
73 | * Return the unique identifier of this Activity. | |
74 | */ | |
75 | public String getId() { | |
76 | ||
77 | 0 | return (this.id); |
78 | ||
79 | } | |
80 | ||
81 | ||
82 | /** | |
83 | * Set the unique identifier of this Activity. | |
84 | * | |
85 | * @param id The new unique identifier | |
86 | */ | |
87 | public void setId(String id) { | |
88 | ||
89 | 0 | this.id = id; |
90 | ||
91 | 0 | } |
92 | ||
93 | ||
94 | /** | |
95 | * Return the last Step associated with this Activity. | |
96 | */ | |
97 | public Step getLastStep() { | |
98 | ||
99 | 0 | return (this.lastStep); |
100 | ||
101 | } | |
102 | ||
103 | ||
104 | // ---------------------------------------------------------- Owner Methods | |
105 | ||
106 | ||
107 | /** | |
108 | * Add a new Step to the end of the sequence of Steps associated with | |
109 | * this Activity. | |
110 | * | |
111 | * @param step The new step to be added | |
112 | */ | |
113 | public void addStep(Step step) { | |
114 | ||
115 | 0 | step.setOwner(this); |
116 | 0 | if (firstStep == null) { |
117 | 0 | step.setPreviousStep(null); |
118 | 0 | step.setNextStep(null); |
119 | 0 | firstStep = step; |
120 | 0 | lastStep = step; |
121 | } else { | |
122 | 0 | step.setPreviousStep(lastStep); |
123 | 0 | step.setNextStep(null); |
124 | 0 | lastStep.setNextStep(step); |
125 | 0 | lastStep = step; |
126 | } | |
127 | ||
128 | 0 | } |
129 | ||
130 | ||
131 | /** | |
132 | * Clear any existing Steps associated with this Activity. | |
133 | */ | |
134 | public void clearSteps() { | |
135 | ||
136 | 0 | Step current = firstStep; |
137 | 0 | while (current != null) { |
138 | 0 | Step next = current.getNextStep(); |
139 | 0 | current.setOwner(null); |
140 | 0 | current.setPreviousStep(null); |
141 | 0 | current.setNextStep(null); |
142 | 0 | current = next; |
143 | 0 | } |
144 | 0 | firstStep = null; |
145 | 0 | lastStep = null; |
146 | ||
147 | 0 | } |
148 | ||
149 | ||
150 | /** | |
151 | * Return the identified Step from this Activity, if it exists. | |
152 | * Otherwise, return <code>null</code>. | |
153 | * | |
154 | * @param id Identifier of the desired Step | |
155 | */ | |
156 | public Step findStep(String id) { | |
157 | ||
158 | 0 | Step current = getFirstStep(); |
159 | 0 | while (current != null) { |
160 | 0 | if (id.equals(current.getId())) |
161 | 0 | return (current); |
162 | 0 | current = current.getNextStep(); |
163 | } | |
164 | 0 | return (null); |
165 | ||
166 | } | |
167 | ||
168 | ||
169 | /** | |
170 | * Return the set of Steps associated with this Activity. | |
171 | */ | |
172 | public Step[] getSteps() { | |
173 | ||
174 | 0 | ArrayList list = new ArrayList(); |
175 | 0 | Step currentStep = firstStep; |
176 | 0 | while (currentStep != null) { |
177 | 0 | list.add(currentStep); |
178 | 0 | currentStep = currentStep.getNextStep(); |
179 | } | |
180 | 0 | Step steps[] = new Step[list.size()]; |
181 | 0 | return ((Step[]) list.toArray(steps)); |
182 | ||
183 | } | |
184 | ||
185 | ||
186 | /** | |
187 | * Set the set of Steps associated with this Activity, replacing any | |
188 | * existing ones. | |
189 | * | |
190 | * @param steps The new set of steps. | |
191 | */ | |
192 | public void setSteps(Step steps[]) { | |
193 | ||
194 | 0 | clearSteps(); |
195 | 0 | for (int i = 0; i < steps.length; i++) |
196 | 0 | addStep(steps[i]); |
197 | ||
198 | 0 | } |
199 | ||
200 | ||
201 | } |