1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml2;
18
19 import java.util.HashMap;
20 import java.util.LinkedHashSet;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.apache.commons.scxml2.model.EnterableState;
25 import org.apache.commons.scxml2.model.Observable;
26 import org.apache.commons.scxml2.model.Transition;
27 import org.apache.commons.scxml2.model.TransitionTarget;
28
29
30
31
32
33
34
35
36
37
38 public final class NotificationRegistry {
39
40
41
42
43 private final Map<Integer, Set<SCXMLListener>> regs;
44
45
46
47
48 public NotificationRegistry() {
49 this.regs = new HashMap<Integer, Set<SCXMLListener>>();
50 }
51
52
53
54
55
56
57
58 synchronized void addListener(final Observable source, final SCXMLListener lst) {
59 if (source != null && source.getObservableId() != null) {
60 Set<SCXMLListener> entries = regs.get(source.getObservableId());
61 if (entries == null) {
62 entries = new LinkedHashSet<SCXMLListener>();
63 regs.put(source.getObservableId(), entries);
64 }
65 entries.add(lst);
66 }
67 }
68
69
70
71
72
73
74
75 synchronized void removeListener(final Observable source, final SCXMLListener lst) {
76 if (source != null && source.getObservableId() != null) {
77 Set<SCXMLListener> entries = regs.get(source.getObservableId());
78 if (entries != null) {
79 entries.remove(lst);
80 if (entries.size() == 0) {
81 regs.remove(source.getObservableId());
82 }
83 }
84 }
85 }
86
87
88
89
90
91
92
93
94 public synchronized void fireOnEntry(final Observable source,
95 final EnterableState state) {
96 if (source != null && source.getObservableId() != null) {
97 Set<SCXMLListener> entries = regs.get(source.getObservableId());
98 if (entries != null) {
99 for (SCXMLListener lst : entries) {
100 lst.onEntry(state);
101 }
102 }
103 }
104 }
105
106
107
108
109
110
111
112
113 public synchronized void fireOnExit(final Observable source,
114 final EnterableState state) {
115 if (source != null && source.getObservableId() != null) {
116 Set<SCXMLListener> entries = regs.get(source.getObservableId());
117 if (entries != null) {
118 for (SCXMLListener lst : entries) {
119 lst.onExit(state);
120 }
121 }
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134 public synchronized void fireOnTransition(final Observable source,
135 final TransitionTarget from, final TransitionTarget to,
136 final Transition transition, final String event) {
137 if (source != null && source.getObservableId() != null) {
138 Set<SCXMLListener> entries = regs.get(source.getObservableId());
139 if (entries != null) {
140 for (SCXMLListener lst : entries) {
141 lst.onTransition(from, to, transition, event);
142 }
143 }
144 }
145 }
146 }
147