1 | package com.framsticks.util.dispatching; |
---|
2 | |
---|
3 | import java.util.Collections; |
---|
4 | import java.util.HashMap; |
---|
5 | import java.util.HashSet; |
---|
6 | import java.util.Iterator; |
---|
7 | import java.util.Map; |
---|
8 | import java.util.Set; |
---|
9 | |
---|
10 | import com.framsticks.core.Entity; |
---|
11 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
12 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
13 | import com.framsticks.util.FramsticksException; |
---|
14 | import com.framsticks.util.Misc; |
---|
15 | |
---|
16 | |
---|
17 | @FramsClassAnnotation |
---|
18 | public class JoinableCollection<T extends Joinable> extends AbstractJoinable implements JoinableParent, Iterable<T> { |
---|
19 | |
---|
20 | protected final Set<T> joinables = new HashSet<T>(); |
---|
21 | protected final Map<String, T> namedJoinables = new HashMap<>(); |
---|
22 | |
---|
23 | protected boolean finishIfOne; |
---|
24 | |
---|
25 | protected String observableName; |
---|
26 | |
---|
27 | public JoinableCollection() { |
---|
28 | this(false); |
---|
29 | } |
---|
30 | |
---|
31 | public JoinableCollection(boolean finishIfOne) { |
---|
32 | this.finishIfOne = finishIfOne; |
---|
33 | } |
---|
34 | |
---|
35 | @AutoAppendAnnotation |
---|
36 | public void add(T joinable) { |
---|
37 | assert isInState(JoinableState.INITILIAZED); |
---|
38 | |
---|
39 | if (joinables.contains(joinable)) { |
---|
40 | throw new FramsticksException().msg("joinable is already observed").arg("joinable", joinable).arg("in", this); |
---|
41 | } |
---|
42 | // if (observables.containsKey(observable.getName())) { |
---|
43 | // throw new FramsticksException().msg("observable with given name already exists").arg("name", observable.getName()).arg("in", this); |
---|
44 | // } |
---|
45 | |
---|
46 | if (joinable instanceof Entity) { |
---|
47 | Entity e = (Entity) joinable; |
---|
48 | if (!namedJoinables.containsKey(e.getName())) { |
---|
49 | namedJoinables.put(e.getName(), joinable); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | joinables.add(joinable); |
---|
54 | } |
---|
55 | |
---|
56 | @Override |
---|
57 | protected void joinableStart() { |
---|
58 | for (T j : joinables) { |
---|
59 | Dispatching.use(j, this); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | @Override |
---|
64 | protected void joinableInterrupt() { |
---|
65 | if (joinables.isEmpty()) { |
---|
66 | finish(); |
---|
67 | return; |
---|
68 | } |
---|
69 | |
---|
70 | for (T j : joinables) { |
---|
71 | Dispatching.drop(j, this); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | @Override |
---|
76 | protected void joinableFinish() { |
---|
77 | } |
---|
78 | |
---|
79 | @Override |
---|
80 | protected void joinableJoin() throws InterruptedException { |
---|
81 | for (T j : joinables) { |
---|
82 | Dispatching.join(j); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | protected JoinableState getNextState() { |
---|
87 | if (joinables.isEmpty()) { |
---|
88 | return state; |
---|
89 | } |
---|
90 | JoinableState result = finishIfOne ? JoinableState.INITILIAZED : JoinableState.JOINED; |
---|
91 | for (Joinable j : joinables) { |
---|
92 | JoinableState s = j.getState(); |
---|
93 | if (finishIfOne) { |
---|
94 | if (s.ordinal() > result.ordinal()) { |
---|
95 | result = s; |
---|
96 | } |
---|
97 | } else { |
---|
98 | if (s.ordinal() < result.ordinal()) { |
---|
99 | result = s; |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | return result; |
---|
104 | } |
---|
105 | |
---|
106 | @Override |
---|
107 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
108 | proceedToState(getNextState()); |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | @Override |
---|
113 | public Iterator<T> iterator() { |
---|
114 | return Collections.unmodifiableSet(joinables).iterator(); |
---|
115 | } |
---|
116 | |
---|
117 | @Override |
---|
118 | public String toString() { |
---|
119 | return Misc.returnNotNull(observableName, "collection"); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * @param observableName the observableName to set |
---|
124 | */ |
---|
125 | public JoinableCollection<T> setObservableName(String observableName) { |
---|
126 | this.observableName = observableName; |
---|
127 | return this; |
---|
128 | } |
---|
129 | |
---|
130 | public T get(String name) { |
---|
131 | return namedJoinables.get(name); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | public Map<String, T> getObservables() { |
---|
136 | return Collections.unmodifiableMap(namedJoinables); |
---|
137 | } |
---|
138 | |
---|
139 | } |
---|