J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.management; |
| 27 | |
| 28 | import java.lang.management.MemoryUsage; |
| 29 | import java.util.Iterator; |
| 30 | import java.util.Map; |
| 31 | import java.util.HashMap; |
| 32 | |
| 33 | /** |
| 34 | * An abstract sensor. |
| 35 | * |
| 36 | * <p> |
| 37 | * A <tt>AbstractSensor</tt> object consists of two attributes: |
| 38 | * <ul> |
| 39 | * <li><tt>on</tt> is a boolean flag indicating if a sensor is |
| 40 | * triggered. This flag will be set or cleared by the |
| 41 | * component that owns the sensor.</li> |
| 42 | * <li><tt>count</tt> is the total number of times that a sensor |
| 43 | * has been triggered.</li> |
| 44 | * </ul> |
| 45 | * |
| 46 | * @author Mandy Chung |
| 47 | * @since 1.5 |
| 48 | */ |
| 49 | |
| 50 | public abstract class Sensor { |
| 51 | private Object lock; |
| 52 | private String name; |
| 53 | private long count; |
| 54 | private boolean on; |
| 55 | |
| 56 | /** |
| 57 | * Constructs a <tt>Sensor</tt> object. |
| 58 | * |
| 59 | * @param name The name of this sensor. |
| 60 | */ |
| 61 | public Sensor(String name) { |
| 62 | this.name = name; |
| 63 | this.count = 0; |
| 64 | this.on = false; |
| 65 | this.lock = new Object(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the name of this sensor. |
| 70 | * |
| 71 | * @return the name of this sensor. |
| 72 | */ |
| 73 | public String getName() { |
| 74 | return name; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the total number of times that this sensor has been triggered. |
| 79 | * |
| 80 | * @return the total number of times that this sensor has been triggered. |
| 81 | */ |
| 82 | public long getCount() { |
| 83 | synchronized (lock) { |
| 84 | return count; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Tests if this sensor is currently on. |
| 90 | * |
| 91 | * @return <tt>true</tt> if the sensor is currently on; |
| 92 | * <tt>false</tt> otherwise. |
| 93 | * |
| 94 | */ |
| 95 | public boolean isOn() { |
| 96 | synchronized (lock) { |
| 97 | return on; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Triggers this sensor. This method first sets this sensor on |
| 103 | * and increments its sensor count. |
| 104 | */ |
| 105 | public void trigger() { |
| 106 | synchronized (lock) { |
| 107 | on = true; |
| 108 | count++; |
| 109 | } |
| 110 | triggerAction(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Triggers this sensor. This method sets this sensor on |
| 115 | * and increments the count with the input <tt>increment</tt>. |
| 116 | */ |
| 117 | public void trigger(int increment) { |
| 118 | synchronized (lock) { |
| 119 | on = true; |
| 120 | count += increment; |
| 121 | // Do something here... |
| 122 | } |
| 123 | triggerAction(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Triggers this sensor piggybacking a memory usage object. |
| 128 | * This method sets this sensor on |
| 129 | * and increments the count with the input <tt>increment</tt>. |
| 130 | */ |
| 131 | public void trigger(int increment, MemoryUsage usage) { |
| 132 | synchronized (lock) { |
| 133 | on = true; |
| 134 | count += increment; |
| 135 | // Do something here... |
| 136 | } |
| 137 | triggerAction(usage); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Clears this sensor. |
| 142 | */ |
| 143 | public void clear() { |
| 144 | synchronized (lock) { |
| 145 | on = false; |
| 146 | } |
| 147 | clearAction(); |
| 148 | } |
| 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() + |
| 165 | (isOn() ? " on " : " off ") + |
| 166 | " count = " + getCount(); |
| 167 | } |
| 168 | |
| 169 | abstract void triggerAction(); |
| 170 | abstract void triggerAction(MemoryUsage u); |
| 171 | abstract void clearAction(); |
| 172 | } |