View Javadoc

1   package com.atlassian.plugin.event.listeners;
2   
3   import com.atlassian.plugin.event.events.PluginEnabledEvent;
4   import com.atlassian.plugin.event.events.PluginDisabledEvent;
5   import com.atlassian.plugin.event.events.PluginModuleEnabledEvent;
6   import com.atlassian.plugin.event.events.PluginModuleDisabledEvent;
7   
8   import java.util.*;
9   
10  public class RecordingListener
11  {
12      private final List<Object> events = new ArrayList<Object>();
13      private final Set<Class> eventClasses = new HashSet<Class>();
14  
15      public RecordingListener(Class... eventClasses)
16      {
17          this.eventClasses.addAll(Arrays.asList(eventClasses));
18      }
19  
20      public void channel(final Object event)
21      {
22          if (event != null && eventClasses.contains(event.getClass()))
23              events.add(event);
24      }
25  
26      /**
27       * @return the events received by this listener in the order they were received
28       */
29      public List<Object> getEvents()
30      {
31          return events;
32      }
33  
34      /**
35       * @return the classes of each event received by this listener in the order they were received
36       */
37      public List<Class> getEventClasses()
38      {
39          List<Class> result = new ArrayList<Class>(events.size());
40          for (Object event : events)
41          {
42              if (event != null) result.add(event.getClass());
43          }
44          return result;
45      }
46  
47      public void reset()
48      {
49          events.clear();
50      }
51  
52      public List<String> getEventPluginOrModuleKeys()
53      {
54          List<String> result = new ArrayList<String>(events.size());
55          for (Object event : events)
56          {
57              if (event instanceof PluginEnabledEvent)
58                  result.add(((PluginEnabledEvent) event).getPlugin().getKey());
59              else if (event instanceof PluginDisabledEvent)
60                  result.add(((PluginDisabledEvent) event).getPlugin().getKey());
61              else if (event instanceof PluginModuleEnabledEvent)
62                  result.add(((PluginModuleEnabledEvent) event).getModule().getCompleteKey());
63              else if (event instanceof PluginModuleDisabledEvent)
64                  result.add(((PluginModuleDisabledEvent) event).getModule().getCompleteKey());
65              else
66                  result.add(null);
67          }
68          return result;
69      }
70  }