View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  
20  package org.eclipse.jetty.monitor.jmx;
21  
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  
28  
29  /* ------------------------------------------------------------ */
30  /**
31   * NotifierGroup
32   * 
33   * This class allows for grouping of the event notifiers 
34   */
35  public class NotifierGroup implements EventNotifier
36  {
37      private Set<EventNotifier> _group;
38  
39      /* ------------------------------------------------------------ */
40      /**
41       * Create a notifier group
42       */
43      public NotifierGroup()
44      {
45          _group = new HashSet<EventNotifier>();
46      }
47      
48      /* ------------------------------------------------------------ */
49      /**
50       * Retrieve all event notifier associated with this group
51       * 
52       * @return collection of event notifiers
53       */    
54      public Collection<EventNotifier> getNotifiers()
55      {
56          return Collections.unmodifiableSet(_group);
57      }
58  
59      /* ------------------------------------------------------------ */
60      /**
61       * Add specified event notifier to event notifier group
62       * 
63       * @param notifier event notifier to be added
64       * @return true if successful
65       */
66      public boolean addNotifier(EventNotifier notifier)
67      {
68          return _group.add(notifier);
69      }
70      
71      /* ------------------------------------------------------------ */
72      /**
73       * Add a collection of event notifiers to event notifier group
74       * 
75       * @param notifiers collection of event notifiers to be added
76       * @return true if successful
77       */
78      public boolean addNotifiers(Collection<EventNotifier> notifiers)
79      {
80          return _group.addAll(notifiers);
81      }
82      
83      /* ------------------------------------------------------------ */
84      /**
85       * Remove event notifier from event notifier group
86       * 
87       * @param notifier event notifier to be removed
88       * @return true if successful
89       */
90      public boolean removeNotifier(EventNotifier notifier)
91      {
92          return _group.remove(notifier);
93      }
94   
95      /* ------------------------------------------------------------ */
96      /**
97       * Remove a collection of event notifiers from event notifier group
98       * 
99       * @param notifiers collection of event notifiers to be removed
100      * @return true if successful
101      */
102     public boolean removeNotifiers(Collection<EventNotifier> notifiers)
103     {
104         return _group.removeAll(notifiers);
105     }
106 
107     /* ------------------------------------------------------------ */
108     /**
109      * Invoke the notify() method of each of the notifiers in group
110      * 
111      * @see org.eclipse.jetty.monitor.jmx.EventNotifier#notify(org.eclipse.jetty.monitor.jmx.EventTrigger, org.eclipse.jetty.monitor.jmx.EventState, long)
112      */
113     public void notify(EventTrigger trigger, EventState<?> state, long timestamp)
114     {
115         for (EventNotifier notifier: _group)
116         {
117             notifier.notify(trigger, state, timestamp);
118         }
119     }
120 }