001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.management;
027:
028: import java.lang.management.MemoryUsage;
029: import java.util.Iterator;
030: import java.util.Map;
031: import java.util.HashMap;
032:
033: /**
034: * An abstract sensor.
035: *
036: * <p>
037: * A <tt>AbstractSensor</tt> object consists of two attributes:
038: * <ul>
039: * <li><tt>on</tt> is a boolean flag indicating if a sensor is
040: * triggered. This flag will be set or cleared by the
041: * component that owns the sensor.</li>
042: * <li><tt>count</tt> is the total number of times that a sensor
043: * has been triggered.</li>
044: * </ul>
045: *
046: * @author Mandy Chung
047: * @version 1.11, 05/05/07
048: * @since 1.5
049: */
050:
051: public abstract class Sensor {
052: private Object lock;
053: private String name;
054: private long count;
055: private boolean on;
056:
057: /**
058: * Constructs a <tt>Sensor</tt> object.
059: *
060: * @param name The name of this sensor.
061: */
062: public Sensor(String name) {
063: this .name = name;
064: this .count = 0;
065: this .on = false;
066: this .lock = new Object();
067: }
068:
069: /**
070: * Returns the name of this sensor.
071: *
072: * @return the name of this sensor.
073: */
074: public String getName() {
075: return name;
076: }
077:
078: /**
079: * Returns the total number of times that this sensor has been triggered.
080: *
081: * @return the total number of times that this sensor has been triggered.
082: */
083: public long getCount() {
084: synchronized (lock) {
085: return count;
086: }
087: }
088:
089: /**
090: * Tests if this sensor is currently on.
091: *
092: * @return <tt>true</tt> if the sensor is currently on;
093: * <tt>false</tt> otherwise.
094: *
095: */
096: public boolean isOn() {
097: synchronized (lock) {
098: return on;
099: }
100: }
101:
102: /**
103: * Triggers this sensor. This method first sets this sensor on
104: * and increments its sensor count.
105: */
106: public void trigger() {
107: synchronized (lock) {
108: on = true;
109: count++;
110: }
111: triggerAction();
112: }
113:
114: /**
115: * Triggers this sensor. This method sets this sensor on
116: * and increments the count with the input <tt>increment</tt>.
117: */
118: public void trigger(int increment) {
119: synchronized (lock) {
120: on = true;
121: count += increment;
122: // Do something here...
123: }
124: triggerAction();
125: }
126:
127: /**
128: * Triggers this sensor piggybacking a memory usage object.
129: * This method sets this sensor on
130: * and increments the count with the input <tt>increment</tt>.
131: */
132: public void trigger(int increment, MemoryUsage usage) {
133: synchronized (lock) {
134: on = true;
135: count += increment;
136: // Do something here...
137: }
138: triggerAction(usage);
139: }
140:
141: /**
142: * Clears this sensor.
143: */
144: public void clear() {
145: synchronized (lock) {
146: on = false;
147: }
148: clearAction();
149: }
150:
151: /**
152: * Clears this sensor
153: * and increments the count with the input <tt>increment</tt>.
154: */
155: public void clear(int increment) {
156: synchronized (lock) {
157: on = false;
158: count += increment;
159: }
160: clearAction();
161: }
162:
163: public String toString() {
164: return "Sensor - " + getName() + (isOn() ? " on " : " off ")
165: + " count = " + getCount();
166: }
167:
168: abstract void triggerAction();
169:
170: abstract void triggerAction(MemoryUsage u);
171:
172: abstract void clearAction();
173: }
|