1 | package com.framsticks.util.dispatching; |
---|
2 | |
---|
3 | import java.util.LinkedList; |
---|
4 | import java.util.ListIterator; |
---|
5 | import java.util.TimerTask; |
---|
6 | |
---|
7 | import org.apache.logging.log4j.Logger; |
---|
8 | import org.apache.logging.log4j.LogManager; |
---|
9 | |
---|
10 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
11 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
12 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
13 | |
---|
14 | @FramsClassAnnotation |
---|
15 | public class StackedJoinable extends AbstractJoinable implements JoinableParent { |
---|
16 | private static final Logger log = LogManager.getLogger(StackedJoinable.class); |
---|
17 | |
---|
18 | |
---|
19 | protected final LinkedList<Joinable> joinables = new LinkedList<Joinable>(); |
---|
20 | protected ListIterator<Joinable> iterator; |
---|
21 | protected Joinable currentJoinable; |
---|
22 | |
---|
23 | protected double interval = 1; |
---|
24 | |
---|
25 | /** |
---|
26 | * |
---|
27 | */ |
---|
28 | public StackedJoinable() { |
---|
29 | super(); |
---|
30 | } |
---|
31 | |
---|
32 | @AutoAppendAnnotation |
---|
33 | public void addJoinable(Joinable joinable) { |
---|
34 | joinables.add(joinable); |
---|
35 | } |
---|
36 | |
---|
37 | @Override |
---|
38 | public String getName() { |
---|
39 | return "stacked"; |
---|
40 | } |
---|
41 | |
---|
42 | @Override |
---|
43 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
44 | // log.error("{} notified {} about changing state to {}", joinable, this, state); |
---|
45 | if (state.equals(JoinableState.RUNNING)) { |
---|
46 | if (joinable == currentJoinable) { |
---|
47 | Dispatching.getTimer().schedule(new TimerTask() { |
---|
48 | @Override |
---|
49 | public void run() { |
---|
50 | startNext(); |
---|
51 | } |
---|
52 | }, (int)(interval * 1000)); |
---|
53 | } |
---|
54 | return; |
---|
55 | } |
---|
56 | if (state.equals(JoinableState.FINISHING)) { |
---|
57 | if (joinable == currentJoinable) { |
---|
58 | Dispatching.getTimer().schedule(new TimerTask() { |
---|
59 | @Override |
---|
60 | public void run() { |
---|
61 | stopNext(); |
---|
62 | } |
---|
63 | }, (int)(interval * 1000)); |
---|
64 | } |
---|
65 | return; |
---|
66 | } |
---|
67 | |
---|
68 | } |
---|
69 | |
---|
70 | protected void startNext() { |
---|
71 | if (!iterator.hasNext()) { |
---|
72 | currentJoinable = null; |
---|
73 | log.debug("started all"); |
---|
74 | return; |
---|
75 | } |
---|
76 | currentJoinable = iterator.next(); |
---|
77 | log.debug("starting {}", currentJoinable); |
---|
78 | Dispatching.use(currentJoinable, this); |
---|
79 | } |
---|
80 | |
---|
81 | protected void stopNext() { |
---|
82 | if (!iterator.hasPrevious()) { |
---|
83 | finishJoinable(); |
---|
84 | return; |
---|
85 | } |
---|
86 | currentJoinable = iterator.previous(); |
---|
87 | log.debug("stoping {}", currentJoinable); |
---|
88 | Dispatching.drop(currentJoinable, this); |
---|
89 | } |
---|
90 | |
---|
91 | @Override |
---|
92 | protected void joinableStart() { |
---|
93 | iterator = joinables.listIterator(); |
---|
94 | startNext(); |
---|
95 | } |
---|
96 | |
---|
97 | @Override |
---|
98 | protected void joinableInterrupt() { |
---|
99 | log.debug("interupting"); |
---|
100 | stopNext(); |
---|
101 | } |
---|
102 | |
---|
103 | @Override |
---|
104 | protected void joinableFinish() { |
---|
105 | // here process ends and StackedJoinable transists to the joinable state |
---|
106 | |
---|
107 | } |
---|
108 | |
---|
109 | @Override |
---|
110 | protected void joinableJoin() throws InterruptedException { |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * @return the interval |
---|
116 | */ |
---|
117 | @ParamAnnotation |
---|
118 | public double getInterval() { |
---|
119 | return interval; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * @param interval the interval to set |
---|
124 | */ |
---|
125 | @ParamAnnotation |
---|
126 | public void setInterval(double interval) { |
---|
127 | this.interval = interval; |
---|
128 | } |
---|
129 | |
---|
130 | public Joinable get(int number) { |
---|
131 | return joinables.get(number); |
---|
132 | } |
---|
133 | |
---|
134 | public int size() { |
---|
135 | return joinables.size(); |
---|
136 | } |
---|
137 | } |
---|