[88] | 1 | package com.framsticks.util.dispatching; |
---|
| 2 | |
---|
[97] | 3 | import java.util.AbstractCollection; |
---|
| 4 | import java.util.Collection; |
---|
[88] | 5 | import java.util.Collections; |
---|
| 6 | import java.util.HashSet; |
---|
| 7 | import java.util.Iterator; |
---|
| 8 | import java.util.Set; |
---|
| 9 | |
---|
[101] | 10 | import com.framsticks.params.ParamFlags; |
---|
[88] | 11 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
| 12 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
[101] | 13 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
[88] | 14 | import com.framsticks.util.FramsticksException; |
---|
| 15 | import com.framsticks.util.Misc; |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | @FramsClassAnnotation |
---|
| 19 | public class JoinableCollection<T extends Joinable> extends AbstractJoinable implements JoinableParent, Iterable<T> { |
---|
| 20 | |
---|
| 21 | protected final Set<T> joinables = new HashSet<T>(); |
---|
| 22 | |
---|
[102] | 23 | public static enum FinishPolicy { |
---|
| 24 | Never, |
---|
| 25 | OnFirst, |
---|
| 26 | OnAll |
---|
| 27 | }; |
---|
[88] | 28 | |
---|
[102] | 29 | protected final FinishPolicy finishPolicy; |
---|
| 30 | |
---|
[88] | 31 | protected String observableName; |
---|
| 32 | |
---|
| 33 | public JoinableCollection() { |
---|
[102] | 34 | this(FinishPolicy.OnAll); |
---|
[88] | 35 | } |
---|
| 36 | |
---|
[102] | 37 | |
---|
| 38 | /** |
---|
| 39 | * @param finishPolicy |
---|
| 40 | */ |
---|
| 41 | public JoinableCollection(FinishPolicy finishPolicy) { |
---|
| 42 | this.finishPolicy = finishPolicy; |
---|
[88] | 43 | } |
---|
| 44 | |
---|
| 45 | @AutoAppendAnnotation |
---|
[96] | 46 | public synchronized void add(T joinable) { |
---|
| 47 | if (this.state.ordinal() > JoinableState.RUNNING.ordinal()) { |
---|
| 48 | throw new FramsticksException().msg("failed to add joinable - collection is passed running state").arg("joinable", joinable).arg("collection", this); |
---|
| 49 | } |
---|
[88] | 50 | |
---|
| 51 | if (joinables.contains(joinable)) { |
---|
| 52 | throw new FramsticksException().msg("joinable is already observed").arg("joinable", joinable).arg("in", this); |
---|
| 53 | } |
---|
[96] | 54 | joinables.add(joinable); |
---|
[88] | 55 | |
---|
[96] | 56 | if (this.state.equals(JoinableState.RUNNING)) { |
---|
| 57 | Dispatching.use(joinable, this); |
---|
[88] | 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
[97] | 61 | public synchronized void remove(T joinable) { |
---|
| 62 | if (this.state.ordinal() > JoinableState.RUNNING.ordinal()) { |
---|
| 63 | throw new FramsticksException().msg("failed to remote joinable - collection is passed running state").arg("joinable", joinable).arg("collection", this); |
---|
| 64 | } |
---|
| 65 | if (!joinables.contains(joinable)) { |
---|
| 66 | throw new FramsticksException().msg("joinable is not observed").arg("joinable", joinable).arg("in", this); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | joinables.remove(joinable); |
---|
| 70 | |
---|
| 71 | if (this.state.equals(JoinableState.RUNNING)) { |
---|
| 72 | Dispatching.drop(joinable, this); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
[88] | 76 | @Override |
---|
| 77 | protected void joinableStart() { |
---|
| 78 | for (T j : joinables) { |
---|
| 79 | Dispatching.use(j, this); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | @Override |
---|
| 84 | protected void joinableInterrupt() { |
---|
| 85 | if (joinables.isEmpty()) { |
---|
[101] | 86 | finishJoinable(); |
---|
[88] | 87 | return; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | for (T j : joinables) { |
---|
| 91 | Dispatching.drop(j, this); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | @Override |
---|
| 96 | protected void joinableFinish() { |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | @Override |
---|
| 100 | protected void joinableJoin() throws InterruptedException { |
---|
| 101 | for (T j : joinables) { |
---|
| 102 | Dispatching.join(j); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | protected JoinableState getNextState() { |
---|
[102] | 107 | if ((finishPolicy == FinishPolicy.Never && state == JoinableState.RUNNING) || joinables.isEmpty()) { |
---|
[88] | 108 | return state; |
---|
| 109 | } |
---|
[102] | 110 | boolean oneIsEnough = (finishPolicy == FinishPolicy.OnFirst); |
---|
| 111 | JoinableState result = oneIsEnough ? JoinableState.INITILIAZED : JoinableState.JOINED; |
---|
[88] | 112 | for (Joinable j : joinables) { |
---|
| 113 | JoinableState s = j.getState(); |
---|
[102] | 114 | if (oneIsEnough) { |
---|
[88] | 115 | if (s.ordinal() > result.ordinal()) { |
---|
| 116 | result = s; |
---|
| 117 | } |
---|
| 118 | } else { |
---|
| 119 | if (s.ordinal() < result.ordinal()) { |
---|
| 120 | result = s; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | return result; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | @Override |
---|
| 128 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
| 129 | proceedToState(getNextState()); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | @Override |
---|
| 133 | public Iterator<T> iterator() { |
---|
| 134 | return Collections.unmodifiableSet(joinables).iterator(); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | @Override |
---|
| 138 | public String toString() { |
---|
| 139 | return Misc.returnNotNull(observableName, "collection"); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | * @param observableName the observableName to set |
---|
| 144 | */ |
---|
| 145 | public JoinableCollection<T> setObservableName(String observableName) { |
---|
| 146 | this.observableName = observableName; |
---|
| 147 | return this; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | public T get(String name) { |
---|
[96] | 151 | for (T j : joinables) { |
---|
[101] | 152 | if (name.equals(j.getName())) { |
---|
[96] | 153 | return j; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | return null; |
---|
[88] | 157 | } |
---|
| 158 | |
---|
[96] | 159 | public int size() { |
---|
| 160 | return joinables.size(); |
---|
| 161 | } |
---|
[88] | 162 | |
---|
[96] | 163 | public boolean contains(T joinable) { |
---|
| 164 | return joinables.contains(joinable); |
---|
[88] | 165 | } |
---|
| 166 | |
---|
[96] | 167 | @Override |
---|
[101] | 168 | @ParamAnnotation |
---|
[96] | 169 | public String getName() { |
---|
| 170 | return observableName; |
---|
| 171 | } |
---|
| 172 | |
---|
[101] | 173 | @ParamAnnotation(flags = ParamFlags.USERREADONLY) |
---|
| 174 | public final void setName(String name) { |
---|
| 175 | observableName = name; |
---|
| 176 | } |
---|
| 177 | |
---|
[97] | 178 | public Collection<T> asCollection() { |
---|
| 179 | return new AbstractCollection<T>() { |
---|
| 180 | |
---|
| 181 | @Override |
---|
| 182 | public Iterator<T> iterator() { |
---|
| 183 | return JoinableCollection.this.iterator(); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | @Override |
---|
| 187 | public int size() { |
---|
| 188 | return JoinableCollection.this.size(); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | @Override |
---|
| 192 | public boolean add(T joinable) { |
---|
| 193 | JoinableCollection.this.add(joinable); |
---|
| 194 | return true; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | @SuppressWarnings("unchecked") |
---|
| 198 | @Override |
---|
| 199 | public boolean remove(Object joinable) { |
---|
| 200 | JoinableCollection.this.remove((T) joinable); |
---|
| 201 | return true; |
---|
| 202 | } |
---|
| 203 | }; |
---|
| 204 | } |
---|
| 205 | |
---|
[101] | 206 | |
---|
| 207 | |
---|
[88] | 208 | } |
---|