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

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

HIGHLIGHTS:

  • improve tree side notes
  • improve GUI layout
  • add foldable list of occured events to EventControl?
  • improve automatic type conversion in proxy listeners
  • implement several Access functionalities as algorithms independent of Access type
  • introduce draft base classes for distributed experiments
  • automatically register dependant Java classes to FramsClass? registry
  • add testing prime experiment and configuration
  • simplify and improve task dispatching

CHANGELOG:
Improve task dispatching in RemoteTree?.

GUI no longer hangs on connection problems.

Make all dispatchers joinables.

Refactorize Thread dispatcher.

Remove Task and PeriodicTask?.

Use Java utilities in those situations.

Reworking tasks dispatching.

Fix bug in EventControl? listener dispatching.

Minor improvements.

Add testing configuration for ExternalProcess? in GUI.

More improvement to prime.

Support for USERREADONLY in GUI.

Add that flag to various params in Java classes.

Remove redundant register clauses from several FramsClassAnnotations?.

Automatically gather and register dependant classes.

Add configuration for prime.

Improve Simulator class.

Add prime.xml configuration.

Introduce draft Experiment and Simulator classes.

Add prime experiment tests.

Enclose typical map with listeners into SimpleUniqueList?.

Needfile works in GUI.

Improve needfile handling in Browser.

More improvement with NeedFile?.

Implementing needfile.

Update test.

Rename ChangeEvent? to TestChangeEvent?.

Automatic argument type search in RemoteTree? listeners.

MultiParamLoader? uses AccessProvider?. By default old implementation
enclosed in AccessStash? or Registry.

Minor changes.

Rename SourceInterface? to Source.

Also improve toString of File and ListSource?.

Remove unused SimpleSource? class.

Add clearing in HistoryControl?.

Show entries in table at EventControl?.

Improve EventControl?.

Add listeners registration to EventControl?.

Add foldable table to HistoryControl?.

Add control row to Procedure and Event controls.

Improve layout of controls.

Another minor change to gui layout.

Minor improvement in the SliderControl?.

Minor changes.

Move ReflectionAccess?.Backend to separate file.

It was to cluttered.

Cleanup in ReflectionAccess?.

Move setMin, setMax, setDef to AccessOperations?.

Extract loading operation into AccessOperations?.

Append Framsticks to name of UnsupportedOperationException?.

The java.lang.UnsupportedOperationException? was shadowing this class.

Rename params.Util to params.ParamsUtil?.

Several improvements.

Minor changes.

Implement revert functionality.

Improve local changes management.

Minor improvement.

Remove methods rendered superfluous after SideNoteKey? improvement.

Improve SideNoteKey?.

It is now generic type, so explicit type specification at
call site is no more needed.

Introduce SideNoteKey? interface.

Only Objects implementing that key may be used as side note keys.

Minor improvements.

Use strings instead of ValueControls? in several gui mappings.

File size: 4.4 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        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 synchronized void add(T joinable) {
37                if (this.state.ordinal() > JoinableState.RUNNING.ordinal()) {
38                        throw new FramsticksException().msg("failed to add joinable - collection is passed running state").arg("joinable", joinable).arg("collection", this);
39                }
40
41                if (joinables.contains(joinable)) {
42                        throw new FramsticksException().msg("joinable is already observed").arg("joinable", joinable).arg("in", this);
43                }
44                joinables.add(joinable);
45
46                if (this.state.equals(JoinableState.RUNNING)) {
47                        Dispatching.use(joinable, this);
48                }
49        }
50
51        public synchronized void remove(T joinable) {
52                if (this.state.ordinal() > JoinableState.RUNNING.ordinal()) {
53                        throw new FramsticksException().msg("failed to remote joinable - collection is passed running state").arg("joinable", joinable).arg("collection", this);
54                }
55                if (!joinables.contains(joinable)) {
56                        throw new FramsticksException().msg("joinable is not observed").arg("joinable", joinable).arg("in", this);
57                }
58
59                joinables.remove(joinable);
60
61                if (this.state.equals(JoinableState.RUNNING)) {
62                        Dispatching.drop(joinable, this);
63                }
64        }
65
66        @Override
67        protected void joinableStart() {
68                for (T j : joinables) {
69                        Dispatching.use(j, this);
70                }
71        }
72
73        @Override
74        protected void joinableInterrupt() {
75                if (joinables.isEmpty()) {
76                        finishJoinable();
77                        return;
78                }
79
80                for (T j : joinables) {
81                        Dispatching.drop(j, this);
82                }
83        }
84
85        @Override
86        protected void joinableFinish() {
87        }
88
89        @Override
90        protected void joinableJoin() throws InterruptedException {
91                for (T j : joinables) {
92                        Dispatching.join(j);
93                }
94        }
95
96        protected JoinableState getNextState() {
97                if (joinables.isEmpty()) {
98                        return state;
99                }
100                JoinableState result = finishIfOne ? JoinableState.INITILIAZED : JoinableState.JOINED;
101                for (Joinable j : joinables) {
102                        JoinableState s = j.getState();
103                        if (finishIfOne) {
104                                if (s.ordinal() > result.ordinal()) {
105                                        result = s;
106                                }
107                        } else {
108                                if (s.ordinal() < result.ordinal()) {
109                                        result = s;
110                                }
111                        }
112                }
113                return result;
114        }
115
116        @Override
117        public void childChangedState(Joinable joinable, JoinableState state) {
118                proceedToState(getNextState());
119        }
120
121        @Override
122        public Iterator<T> iterator() {
123                return Collections.unmodifiableSet(joinables).iterator();
124        }
125
126        @Override
127        public String toString() {
128                return Misc.returnNotNull(observableName, "collection");
129        }
130
131        /**
132         * @param observableName the observableName to set
133         */
134        public JoinableCollection<T> setObservableName(String observableName) {
135                this.observableName = observableName;
136                return this;
137        }
138
139        public T get(String name) {
140                for (T j : joinables) {
141                        if (name.equals(j.getName())) {
142                                return j;
143                        }
144                }
145                return null;
146        }
147
148        public int size() {
149                return joinables.size();
150        }
151
152        public boolean contains(T joinable) {
153                return joinables.contains(joinable);
154        }
155
156        @Override
157        @ParamAnnotation
158        public String getName() {
159                return observableName;
160        }
161
162        @ParamAnnotation(flags = ParamFlags.USERREADONLY)
163        public final void setName(String name) {
164                observableName = name;
165        }
166
167        public Collection<T> asCollection() {
168                return new AbstractCollection<T>() {
169
170                        @Override
171                        public Iterator<T> iterator() {
172                                return JoinableCollection.this.iterator();
173                        }
174
175                        @Override
176                        public int size() {
177                                return JoinableCollection.this.size();
178                        }
179
180                        @Override
181                        public boolean add(T joinable) {
182                                JoinableCollection.this.add(joinable);
183                                return true;
184                        }
185
186                        @SuppressWarnings("unchecked")
187                        @Override
188                        public boolean remove(Object joinable) {
189                                JoinableCollection.this.remove((T) joinable);
190                                return true;
191                        }
192                };
193        }
194
195
196
197}
Note: See TracBrowser for help on using the repository browser.