KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > mp > CounterSupport


1 /*_############################################################################
2   _##
3   _## SNMP4J - CounterSupport.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22
23 package org.snmp4j.mp;
24
25 import org.snmp4j.event.*;
26 import java.util.*;
27
28 /**
29  * The <code>CounterSupport</code> class provides support to fire
30  * {@link CounterEvent} to registered listeners.
31  * @author Frank Fock
32  * @version 1.0
33  */

34 public class CounterSupport {
35
36   protected static CounterSupport instance = null;
37   private transient Vector counterListeners;
38
39   protected CounterSupport() {
40   }
41
42   /**
43    * Gets the counter support singleton.
44    * @return
45    * the <code>CounterSupport</code> instance.
46    */

47   public static CounterSupport getInstance() {
48     if (instance == null) {
49       instance = new CounterSupport();
50     }
51     return instance;
52   }
53
54   /**
55    * Adds a <code>CounterListener</code>.
56    * @param listener
57    * a <code>CounterListener</code> instance that needs to be informed when
58    * a counter needs to be incremented.
59    */

60   public synchronized void addCounterListener(CounterListener listener) {
61     Vector v = (counterListeners == null) ?
62         new Vector(2) : (Vector) counterListeners.clone();
63     if (!v.contains(listener)) {
64       v.addElement(listener);
65       counterListeners = v;
66     }
67   }
68
69   /**
70    * Removes a previously added <code>CounterListener</code>.
71    * @param listener
72    * a <code>CounterListener</code> instance.
73    */

74   public synchronized void removeCounterListener(CounterListener listener) {
75     if (counterListeners != null && counterListeners.contains(listener)) {
76       Vector v = (Vector) counterListeners.clone();
77       v.removeElement(listener);
78       counterListeners = v;
79     }
80   }
81
82   /**
83    * Inform all registered listeners that the supplied counter needs to be
84    * incremented.
85    * @param event
86    * a <code>CounterEvent</code> containing information about the counter to
87    * be incremented.
88    */

89   public void fireIncrementCounter(CounterEvent event) {
90     if (counterListeners != null) {
91       Vector listeners = counterListeners;
92       int count = listeners.size();
93       for (int i = 0; i < count; i++) {
94         ((CounterListener) listeners.elementAt(i)).incrementCounter(event);
95       }
96     }
97   }
98 }
99
Popular Tags