01: package dalma.impl;
02:
03: import dalma.Condition;
04: import dalma.spi.ConditionListener;
05:
06: import java.util.Arrays;
07: import java.util.List;
08:
09: /**
10: * @author Kohsuke Kawaguchi
11: */
12: public class OrCondition<T> extends Condition<Condition<? extends T>>
13: implements ConditionListener {
14:
15: /**
16: * {@link Condition}s that this object is waiting on.
17: * If any one of them become active, this object becomes active.
18: */
19: private final List<Condition<? extends T>> conditions;
20:
21: public OrCondition(Condition<? extends T>... conditions) {
22: this (Arrays.asList(conditions));
23: }
24:
25: public OrCondition(List<Condition<? extends T>> conditions) {
26: if (conditions.isEmpty())
27: throw new IllegalArgumentException(
28: "condition list is empty");
29: this .conditions = conditions;
30: }
31:
32: public void onParked() {
33: for (Condition<? extends T> co : conditions) {
34: co.park(this );
35: }
36: }
37:
38: public void interrupt() {
39: for (Condition<? extends T> co : conditions) {
40: co.interrupt();
41: }
42: }
43:
44: public void onLoad() {
45: for (Condition<? extends T> co : conditions) {
46: co.onLoad();
47: }
48: }
49:
50: public synchronized void onActivated(Condition cond) {
51: if (isActive()) {
52: // we are already active. ignore
53: return;
54: }
55: activate(cond);
56: for (Condition<? extends T> c : conditions) {
57: if (c != cond)
58: c.interrupt();
59: }
60: }
61:
62: private static final long serialVersionUID = 1L;
63: }
|