]> git.somenet.org - pub/jan/dst18.git/blob - ass2-aop/src/test/java/dst/ass2/aop/event/Event.java
Add template for assignment 2
[pub/jan/dst18.git] / ass2-aop / src / test / java / dst / ass2 / aop / event / Event.java
1 package dst.ass2.aop.event;
2
3 import org.springframework.util.Assert;
4
5 import dst.ass2.aop.IPluginExecutable;
6
7 /**
8  * Events triggered by {@link IPluginExecutable}s.
9  */
10 public class Event {
11     private final long time = System.currentTimeMillis();
12     private Class<? extends IPluginExecutable> pluginClass;
13     private EventType type;
14     private String message;
15
16     public Event(EventType type, Class<? extends IPluginExecutable> pluginClass, String message) {
17         this.type = type;
18         this.pluginClass = pluginClass;
19         this.message = message;
20
21         StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
22         int pos = stackTrace[1].getMethodName().equals("<init>") ? 1 : 2;
23         Assert.state(stackTrace[pos].getMethodName().equals("<init>"), "Invalid Event Creation");
24         Assert.state(stackTrace[pos + 1].getClassName().equals(EventBus.class.getName()), "Invalid Event Creation");
25         Assert.state(stackTrace[pos + 1].getMethodName().equals("add"), "Invalid Event Creation");
26     }
27
28     /**
29      * Returns the time when the event occurred.
30      *
31      * @return the event creation time
32      */
33     public long getTime() {
34         return time;
35     }
36
37     /**
38      * Returns the type of the plugin that triggered the event.
39      *
40      * @return the plugin type
41      */
42     public Class<? extends IPluginExecutable> getPluginClass() {
43         return pluginClass;
44     }
45
46     /**
47      * Returns the type of the event.
48      *
49      * @return the event type
50      */
51     public EventType getType() {
52         return type;
53     }
54
55     /**
56      * Returns the message of the event
57      *
58      * @return the event message
59      */
60     public String getMessage() {
61         return message;
62     }
63
64     @Override
65     public String toString() {
66         final StringBuilder sb = new StringBuilder();
67         sb.append("Event");
68         sb.append("{time=").append(time);
69         sb.append(", pluginClass=").append(pluginClass);
70         sb.append(", type=").append(type);
71         sb.append('}');
72         return sb.toString();
73     }
74 }