]> git.somenet.org - pub/jan/dst18.git/blob - ass2-aop/src/test/java/dst/ass2/aop/event/EventBus.java
Add template for assignment 2
[pub/jan/dst18.git] / ass2-aop / src / test / java / dst / ass2 / aop / event / EventBus.java
1 package dst.ass2.aop.event;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.springframework.aop.support.AopUtils;
7
8 import dst.ass2.aop.IPluginExecutable;
9
10 /**
11  * Stateful event bus that stores events triggered by executable plugins.
12  * <p/>
13  * Note that this implementation is thread safe.
14  */
15 public class EventBus {
16     private static final EventBus instance = new EventBus();
17     private final List<Event> events = new ArrayList<Event>();
18
19     public static EventBus getInstance() {
20         return instance;
21     }
22
23     private EventBus() {
24     }
25
26     /**
27      * Returns all events of the certain type(s).<br/>
28      * If no types are specified, all events are returned instead.
29      *
30      * @param types the event types
31      * @return list of all events of the given types.
32      */
33     public List<Event> getEvents(EventType... types) {
34         synchronized (events) {
35             if (types == null || types.length == 0) {
36                 return new ArrayList<Event>(events);
37             } else {
38                 List<Event> list = new ArrayList<Event>();
39                 for (Event event : events) {
40                     for (EventType type : types) {
41                         if (type == event.getType()) {
42                             list.add(event);
43                         }
44                     }
45                 }
46                 return list;
47             }
48         }
49     }
50
51     /**
52      * Resets the event bus by purging the event history.
53      */
54     public synchronized void reset() {
55         synchronized (events) {
56             events.clear();
57         }
58     }
59
60     /**
61      * Adds a new event of a certain type triggered by the given plugin.
62      *
63      * @param type the event type
64      * @param pluginExecutable the plugin that triggered the event
65      * @param message the event message
66      */
67     @SuppressWarnings("unchecked")
68     public void add(EventType type, IPluginExecutable pluginExecutable, String message) {
69         add(type, (Class<? extends IPluginExecutable>) AopUtils.getTargetClass(pluginExecutable), message);
70     }
71
72     /**
73      * Adds a new event of a certain type triggered by a plugin of the given type.
74      *
75      * @param type the event type
76      * @param pluginType the type of the plugin
77      * @param message the event message
78      */
79     public void add(EventType type, Class<? extends IPluginExecutable> pluginType, String message) {
80         Event event = new Event(type, pluginType, message);
81         synchronized (events) {
82             events.add(event);
83         }
84     }
85
86     /**
87      * Returns the number of events of a certain type fired by this event bus.
88      *
89      * @param type the event type
90      * @return number of events
91      */
92     public int count(EventType type) {
93         int counter = 0;
94         synchronized (events) {
95             for (Event event : events) {
96                 if (event.getType() == type) {
97                     counter++;
98                 }
99             }
100         }
101         return counter;
102     }
103
104     /**
105      * Returns the number of events fired so far.
106      *
107      * @return number of events
108      */
109     public int size() {
110         return events.size();
111     }
112
113     /**
114      * Checks if there was at least one event of a certain type triggered by a plugin with the given full-qualified
115      * class name.
116      *
117      * If {@code pluginType} is {@code null}, the type of the plugin is not checked. The same is true for {@code type}.
118      * If all parameters are {@code null}, {@code true} is returned if there is at least one event.
119      *
120      * @param pluginType the class name of the plugin
121      * @param type the type of the event
122      * @return {@code true} if there is at least one event matching the criteria, {@code false} otherwise
123      */
124     public boolean has(String pluginType, EventType type) {
125         synchronized (events) {
126             for (Event event : events) {
127                 if ((pluginType == null || pluginType.equals(event.getPluginClass().getName()))
128                         && (type == null || type == event.getType())) {
129                     return true;
130                 }
131             }
132         }
133         return false;
134     }
135 }