01: /*_############################################################################
02: _##
03: _## SNMP4J - CounterSupport.java
04: _##
05: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
06: _##
07: _## Licensed under the Apache License, Version 2.0 (the "License");
08: _## you may not use this file except in compliance with the License.
09: _## 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: package org.snmp4j.mp;
22:
23: import org.snmp4j.event.*;
24: import java.util.*;
25:
26: /**
27: * The <code>CounterSupport</code> class provides support to fire
28: * {@link CounterEvent} to registered listeners.
29: * @author Frank Fock
30: * @version 1.0
31: */
32: public class CounterSupport {
33:
34: protected static CounterSupport instance = null;
35: private transient Vector counterListeners;
36:
37: protected CounterSupport() {
38: }
39:
40: /**
41: * Gets the counter support singleton.
42: * @return
43: * the <code>CounterSupport</code> instance.
44: */
45: public static CounterSupport getInstance() {
46: if (instance == null) {
47: instance = new CounterSupport();
48: }
49: return instance;
50: }
51:
52: /**
53: * Adds a <code>CounterListener</code>.
54: * @param listener
55: * a <code>CounterListener</code> instance that needs to be informed when
56: * a counter needs to be incremented.
57: */
58: public synchronized void addCounterListener(CounterListener listener) {
59: Vector v = (counterListeners == null) ? new Vector(2)
60: : (Vector) counterListeners.clone();
61: if (!v.contains(listener)) {
62: v.addElement(listener);
63: counterListeners = v;
64: }
65: }
66:
67: /**
68: * Removes a previously added <code>CounterListener</code>.
69: * @param listener
70: * a <code>CounterListener</code> instance.
71: */
72: public synchronized void removeCounterListener(
73: CounterListener listener) {
74: if (counterListeners != null
75: && 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))
95: .incrementCounter(event);
96: }
97: }
98: }
99: }
|