source: java/main/src/main/java/com/framsticks/util/dispatching/JoinableCollection.java @ 102

Last change on this file since 102 was 102, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

for Joinables running

CHANGELOG:
Add WorkPackageLogic? and classes representing prime experiment state.

Add classes for PrimeExperiment? state.

Extract single netload routine in Simulator.

Working netload with dummy content in PrimeExperiment?.

More development with NetLoadSaveLogic? and PrimeExperiment?.

Improvement around prime.

Improve BufferedDispatcher?.isActive logic.

Add prime-all.xml configuration.

Manual connecting to existing simulators from GUI.

Guard in SimulatorConnector? against expdef mismatch.

Guard against empty target dispatcher in BufferedDispatcher?.

Make BufferedDispatcher? a Dispatcher (and Joinable).

Minor improvements.

Done StackedJoinable?, improve Experiment.

Develop StackedJoinable?.

Add StackedJoinable? utility joinables controller.

Add dependency on apache-commons-lang.

Add ready ListChange? on Simulators.

Improve hints in ListChange?.

Several improvements.

Found bug with dispatching in Experiment.

Minor improvements.

Fix bug with early finishing Server.

Many changes in Dispatching.

Fix bug with connection.

Do not obfuscate log with socket related exceptions.

Add SocketClosedException?.

Add SimulatorConnector?.

Work out conception of experiment composing of logics building blocks.

Rename SinkInterface? to Sink.

Move saving of Accesses into AccessOperations?.

Some improvements to Experiment.

Improve joinables.

Fix issue with joinables closing.

Add direct and managed consoles to popup menu.

File size: 4.7 KB
Line 
1package com.framsticks.util.dispatching;
2
3import java.util.AbstractCollection;
4import java.util.Collection;
5import java.util.Collections;
6import java.util.HashSet;
7import java.util.Iterator;
8import java.util.Set;
9
10import com.framsticks.params.ParamFlags;
11import com.framsticks.params.annotations.AutoAppendAnnotation;
12import com.framsticks.params.annotations.FramsClassAnnotation;
13import com.framsticks.params.annotations.ParamAnnotation;
14import com.framsticks.util.FramsticksException;
15import com.framsticks.util.Misc;
16
17
18@FramsClassAnnotation
19public class JoinableCollection<T extends Joinable> extends AbstractJoinable implements JoinableParent, Iterable<T> {
20
21        protected final Set<T> joinables = new HashSet<T>();
22
23        public static enum FinishPolicy {
24                Never,
25                OnFirst,
26                OnAll
27        };
28
29        protected final FinishPolicy finishPolicy;
30
31        protected String observableName;
32
33        public JoinableCollection() {
34                this(FinishPolicy.OnAll);
35        }
36
37
38        /**
39         * @param finishPolicy
40         */
41        public JoinableCollection(FinishPolicy finishPolicy) {
42                this.finishPolicy = finishPolicy;
43        }
44
45        @AutoAppendAnnotation
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                }
50
51                if (joinables.contains(joinable)) {
52                        throw new FramsticksException().msg("joinable is already observed").arg("joinable", joinable).arg("in", this);
53                }
54                joinables.add(joinable);
55
56                if (this.state.equals(JoinableState.RUNNING)) {
57                        Dispatching.use(joinable, this);
58                }
59        }
60
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
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()) {
86                        finishJoinable();
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() {
107                if ((finishPolicy == FinishPolicy.Never && state == JoinableState.RUNNING) || joinables.isEmpty()) {
108                        return state;
109                }
110                boolean oneIsEnough = (finishPolicy == FinishPolicy.OnFirst);
111                JoinableState result = oneIsEnough ? JoinableState.INITILIAZED : JoinableState.JOINED;
112                for (Joinable j : joinables) {
113                        JoinableState s = j.getState();
114                        if (oneIsEnough) {
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) {
151                for (T j : joinables) {
152                        if (name.equals(j.getName())) {
153                                return j;
154                        }
155                }
156                return null;
157        }
158
159        public int size() {
160                return joinables.size();
161        }
162
163        public boolean contains(T joinable) {
164                return joinables.contains(joinable);
165        }
166
167        @Override
168        @ParamAnnotation
169        public String getName() {
170                return observableName;
171        }
172
173        @ParamAnnotation(flags = ParamFlags.USERREADONLY)
174        public final void setName(String name) {
175                observableName = name;
176        }
177
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
206
207
208}
Note: See TracBrowser for help on using the repository browser.