1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package fulmine.event.listener;
17
18 import static fulmine.util.Utils.COLON;
19 import static fulmine.util.Utils.nullCheck;
20
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import fulmine.AbstractLifeCycle;
27 import fulmine.event.IEvent;
28 import fulmine.event.IEventManager;
29 import fulmine.event.IEventSource;
30 import fulmine.event.system.AbstractSystemEvent;
31 import fulmine.event.system.ISystemEvent;
32 import fulmine.util.collection.CollectionFactory;
33 import fulmine.util.log.AsyncLog;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class MultiEventListener extends AbstractLifeCycle implements
59 ILifeCycleEventListener
60 {
61 private final static AsyncLog LOG = new AsyncLog(MultiEventListener.class);
62
63
64 final Map<Class<? extends IEvent>, IEventListener> listeners;
65
66
67 private String name;
68
69
70 private final IEventManager context;
71
72
73
74
75
76 private final Class<? extends IEvent>[] filter;
77
78
79
80
81
82
83
84
85
86
87
88
89 @SuppressWarnings("unchecked")
90 public MultiEventListener(String name, IEventManager context,
91 Map<Class<? extends IEvent>, IEventListener> listeners)
92 {
93 super();
94 nullCheck(name, "Null name");
95 nullCheck(listeners, "No listeners provided");
96 this.context = context;
97 this.name = name;
98 this.listeners = listeners;
99 List<Class<? extends IEvent>> filterList = CollectionFactory.newList();
100 for (IEventListener listener : listeners.values())
101 {
102 filterList.addAll(Arrays.asList(listener.getEventTypeFilter()));
103 }
104 this.filter = filterList.toArray(new Class[filterList.size()]);
105 }
106
107 @SuppressWarnings("unchecked")
108 @Override
109 protected void doDestroy()
110 {
111 final Set<Class<? extends IEvent>> eventTypes = getListeners().keySet();
112 for (Class<? extends IEvent> eventType : eventTypes)
113 {
114 if (AbstractSystemEvent.class.isAssignableFrom(eventType))
115 {
116 getContext().getSystemEventSource(
117 (Class<? extends ISystemEvent>) eventType).removeListener(
118 this);
119 }
120 }
121 getListeners().clear();
122 }
123
124 @Override
125 protected AsyncLog getLog()
126 {
127 return LOG;
128 }
129
130 @SuppressWarnings("unchecked")
131 @Override
132 protected void doStart()
133 {
134 final Set<Class<? extends IEvent>> eventTypes = getListeners().keySet();
135 for (Class<? extends IEvent> eventType : eventTypes)
136 {
137 if (AbstractSystemEvent.class.isAssignableFrom(eventType))
138 {
139 getContext().getSystemEventSource(
140 (Class<? extends ISystemEvent>) eventType).addListener(this);
141 }
142 }
143 }
144
145 public final void update(IEvent event)
146 {
147 if (getLog().isTraceEnabled())
148 {
149 getLog().trace("update event=" + event);
150 }
151 for (int i = 0; i < getEventTypeFilter().length; i++)
152 {
153 Class<? extends IEvent> eventType = getEventTypeFilter()[i];
154 if (eventType.isInstance(event))
155 {
156 final IEventListener eventListener =
157 getListeners().get(eventType);
158 if (eventListener != null)
159 {
160 eventListener.update(event);
161 }
162 }
163 }
164 }
165
166 public void addedAsListenerFor(IEventSource source)
167 {
168
169 }
170
171 public void removedAsListenerFrom(IEventSource source)
172 {
173
174 }
175
176 public Class<? extends IEvent>[] getEventTypeFilter()
177 {
178 return this.filter;
179 }
180
181 Map<Class<? extends IEvent>, IEventListener> getListeners()
182 {
183 return this.listeners;
184 }
185
186 String getName()
187 {
188 return this.name;
189 }
190
191 IEventManager getContext()
192 {
193 return this.context;
194 }
195
196 @Override
197 public final String toString()
198 {
199 return getClass().getSimpleName() + COLON + name;
200 }
201 }