1 package fulmine.event;
2
3 import fulmine.util.Utils;
4 import fulmine.util.reference.is;
5
6 /**
7 * Wraps an event that is placed onto an event queue. This encapsulates timings
8 * of event creation to the microsecond.
9 *
10 * @author Ramon Servadei
11 * @param <EVENT>
12 * the event class that is wrapped
13 */
14 public final class EventQueueItem<EVENT>
15 {
16
17 /** The event being wrapped */
18 private final EVENT event;
19
20 /** The creation time of the queue item in microseconds */
21 private final long createTimeMicro = System.nanoTime() / 1000;
22
23 /**
24 * Flag to indicate if this is a statistics event and should have no
25 * statistics logging of the event
26 */
27 private final boolean statisticsEvent;
28
29 /**
30 * Thread local for a thread to 'tag' an event being queued onto the
31 * {@link IEventManager#queueEvent(fulmine.event.IEvent)} as being a
32 * statistics event and should thus not log statistics about the event.
33 */
34 public static ThreadLocal<Boolean> isStatisticsEvent =
35 new ThreadLocal<Boolean>();
36
37 /**
38 * Construct the event queue item wrapping an event
39 *
40 * @param event
41 * the event to wrap
42 */
43 public EventQueueItem(EVENT event)
44 {
45 super();
46 this.event = event;
47 final Boolean isStatistics = isStatisticsEvent.get();
48 if (isStatistics != null)
49 {
50 this.statisticsEvent = isStatistics.booleanValue();
51 }
52 else
53 {
54 this.statisticsEvent = false;
55 }
56 }
57
58 /**
59 * Get the event
60 *
61 * @return the event
62 */
63 public EVENT getEvent()
64 {
65 return this.event;
66 }
67
68 /**
69 * Get the time this item was created in microseconds
70 *
71 * @return the time of creation in microseconds
72 */
73 public long getCreateTimeMicro()
74 {
75 return this.createTimeMicro;
76 }
77
78 /**
79 * Determine if this event is a statistics event. These events should not
80 * have statistics logged (else we get into a recursive loop).
81 *
82 * @return <code>true</code> if this is a statistics event
83 */
84 public boolean isStatisticsEvent()
85 {
86 return this.statisticsEvent;
87 }
88
89 /**
90 * Get the elapsed time in microseconds between now and when the item was
91 * created
92 *
93 * @return the elapsed time in microseconds between now and when the item
94 * was created
95 */
96 public long getElapsedTimeMicro()
97 {
98 return (System.nanoTime() / 1000) - getCreateTimeMicro();
99 }
100
101 @Override
102 public int hashCode()
103 {
104 final int prime = 31;
105 int result = 1;
106 result =
107 prime * result + (int) (createTimeMicro ^ (createTimeMicro >>> 32));
108 result = prime * result + ((event == null) ? 0 : event.hashCode());
109 return result;
110 }
111
112 @SuppressWarnings("unchecked")
113 @Override
114 public boolean equals(Object obj)
115 {
116 if (is.same(this, obj))
117 {
118 return true;
119 }
120 if (is.differentClass(this, obj))
121 {
122 return false;
123 }
124 EventQueueItem other = (EventQueueItem) obj;
125 return is.eq(this.createTimeMicro, other.createTimeMicro)
126 && is.eq(this.event, other.event);
127 }
128
129 @Override
130 public String toString()
131 {
132 return Utils.string(this, "createTimeMicro=" + getCreateTimeMicro()
133 + ", event=" + getEvent());
134 }
135 }