Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
TimedRegisteredListener.java
1 package org.bukkit.plugin;
2 
3 import org.bukkit.event.Event;
6 import org.bukkit.event.Listener;
7 
8 /**
9  * Extends RegisteredListener to include timing information
10  */
12  private int count;
13  private long totalTime;
14  private Class<? extends Event> eventClass;
15  private boolean multiple = false;
16 
17  public TimedRegisteredListener(final Listener pluginListener, final EventExecutor eventExecutor, final EventPriority eventPriority, final Plugin registeredPlugin, final boolean listenCancelled) {
18  super(pluginListener, eventExecutor, eventPriority, registeredPlugin, listenCancelled);
19  }
20 
21  @Override
22  public void callEvent(Event event) throws EventException {
23  if (event.isAsynchronous()) {
24  super.callEvent(event);
25  return;
26  }
27  count++;
28  Class<? extends Event> newEventClass = event.getClass();
29  if (this.eventClass == null) {
30  this.eventClass = newEventClass;
31  } else if (!this.eventClass.equals(newEventClass)) {
32  multiple = true;
33  this.eventClass = getCommonSuperclass(newEventClass, this.eventClass).asSubclass(Event.class);
34  }
35  long start = System.nanoTime();
36  super.callEvent(event);
37  totalTime += System.nanoTime() - start;
38  }
39 
40  private static Class<?> getCommonSuperclass(Class<?> class1, Class<?> class2) {
41  while (!class1.isAssignableFrom(class2)) {
42  class1 = class1.getSuperclass();
43  }
44  return class1;
45  }
46 
47  /**
48  * Resets the call count and total time for this listener
49  */
50  public void reset() {
51  count = 0;
52  totalTime = 0;
53  }
54 
55  /**
56  * Gets the total times this listener has been called
57  *
58  * @return Times this listener has been called
59  */
60  public int getCount() {
61  return count;
62  }
63 
64  /**
65  * Gets the total time calls to this listener have taken
66  *
67  * @return Total time for all calls of this listener
68  */
69  public long getTotalTime() {
70  return totalTime;
71  }
72 
73  /**
74  * Gets the class of the events this listener handled. If it handled
75  * multiple classes of event, the closest shared superclass will be
76  * returned, such that for any event this listener has handled,
77  * <code>this.getEventClass().isAssignableFrom(event.getClass())</code>
78  * and no class <code>this.getEventClass().isAssignableFrom(clazz)
79  * && this.getEventClass() != clazz &&
80  * event.getClass().isAssignableFrom(clazz)</code> for all handled events.
81  *
82  * @return the event class handled by this RegisteredListener
83  */
84  public Class<? extends Event> getEventClass() {
85  return eventClass;
86  }
87 
88  /**
89  * Gets whether this listener has handled multiple events, such that for
90  * some two events, <code>eventA.getClass() != eventB.getClass()</code>.
91  *
92  * @return true if this listener has handled multiple events
93  */
94  public boolean hasMultiple() {
95  return multiple;
96  }
97 }
final boolean isAsynchronous()
Definition: Event.java:72