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

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

HIGHLIGHTS:

  • loading f0 schema with XmlLoader?
  • use XmlLoader? to load configuration
  • introduce unified fork-join model of various entities

(Instances, Connections, GUI Frames, etc.),
all those entities clean up gracefully on
shutdown, which may be initialized by user
or by some entity

  • basing on above, simplify several organizing classes

(Observer, main class)

(to host native frams server process from Java level)

CHANGELOG:
Remove redundant Observer class.

Clean up in AbstractJoinable?.

Update ExternalProcess? class to changes in joining model.

Another sweep through code with FindBugs?.

Find bug with not joining RemoteInstance?.

Joining almost works.

Much improved joining model.

More improvement to joining model.

Add logging messages around joinable operations.

Rename methods in AbstractJoinable?.

Improve Joinable.

Rewrite of entity structure.

More simplifications with entities.

Further improve joinables.

Let Frame compose from JFrame instead of inheriting.

Add join classes.

Improvements of closing.

Add Builder interface.

Add FramsServerTest?.xml

FramsServer? may be configured through xml.

Make Framsticks main class an Observer of Entities.

Make Observer a generic type.

Remove variables regarding to removed endpoint.

Simplify observer (remove endpoints).

More changes to Observer and Endpoint.

Minor improvements.

Add OutputListener? to ExternalProcess?.

Improve testing of ExternalProcess?.

Add ExternalProcess? runner.

Rename the Program class to Framsticks.

Migrate Program to use XmlLoader? configuration.

First steps with configuration using XmlLoader?.

Fix several bugs.

Move all f0 classes to apriopriate package.

XmlLoader? is able to load Schema.

XmlLoader? is loading classes and props.

Add GroupBuilder?.

File size: 3.1 KB
Line 
1package com.framsticks.util.dispatching;
2
3import java.util.Collections;
4import java.util.HashMap;
5import java.util.HashSet;
6import java.util.Iterator;
7import java.util.Map;
8import java.util.Set;
9
10import com.framsticks.core.Entity;
11import com.framsticks.params.annotations.AutoAppendAnnotation;
12import com.framsticks.params.annotations.FramsClassAnnotation;
13import com.framsticks.util.FramsticksException;
14import com.framsticks.util.Misc;
15
16
17@FramsClassAnnotation
18public 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}
Note: See TracBrowser for help on using the repository browser.