2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
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>
|
37880
|
37 |
* An {@code AbstractSensor} object consists of two attributes:
|
2
|
38 |
* <ul>
|
30655
|
39 |
* <li>{@code on} is a boolean flag indicating if a sensor is
|
2
|
40 |
* triggered. This flag will be set or cleared by the
|
|
41 |
* component that owns the sensor.</li>
|
30655
|
42 |
* <li>{@code count} is the total number of times that a sensor
|
2
|
43 |
* has been triggered.</li>
|
|
44 |
* </ul>
|
|
45 |
*
|
|
46 |
* @author Mandy Chung
|
|
47 |
* @since 1.5
|
|
48 |
*/
|
|
49 |
|
|
50 |
public abstract class Sensor {
|
49558
|
51 |
private final Object lock = new Object();
|
|
52 |
private final String name;
|
|
53 |
private long count; // VM-initialized to 0
|
|
54 |
private boolean on; // VM-initialized to false
|
2
|
55 |
|
|
56 |
/**
|
30655
|
57 |
* Constructs a {@code Sensor} object.
|
2
|
58 |
*
|
|
59 |
* @param name The name of this sensor.
|
|
60 |
*/
|
|
61 |
public Sensor(String name) {
|
|
62 |
this.name = name;
|
|
63 |
}
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Returns the name of this sensor.
|
|
67 |
*
|
|
68 |
* @return the name of this sensor.
|
|
69 |
*/
|
|
70 |
public String getName() {
|
|
71 |
return name;
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Returns the total number of times that this sensor has been triggered.
|
|
76 |
*
|
|
77 |
* @return the total number of times that this sensor has been triggered.
|
|
78 |
*/
|
|
79 |
public long getCount() {
|
|
80 |
synchronized (lock) {
|
|
81 |
return count;
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Tests if this sensor is currently on.
|
|
87 |
*
|
30655
|
88 |
* @return {@code true} if the sensor is currently on;
|
|
89 |
* {@code false} otherwise.
|
2
|
90 |
*
|
|
91 |
*/
|
|
92 |
public boolean isOn() {
|
|
93 |
synchronized (lock) {
|
|
94 |
return on;
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Triggers this sensor. This method first sets this sensor on
|
|
100 |
* and increments its sensor count.
|
|
101 |
*/
|
|
102 |
public void trigger() {
|
|
103 |
synchronized (lock) {
|
|
104 |
on = true;
|
|
105 |
count++;
|
|
106 |
}
|
|
107 |
triggerAction();
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Triggers this sensor. This method sets this sensor on
|
30655
|
112 |
* and increments the count with the input {@code increment}.
|
2
|
113 |
*/
|
|
114 |
public void trigger(int increment) {
|
|
115 |
synchronized (lock) {
|
|
116 |
on = true;
|
|
117 |
count += increment;
|
|
118 |
// Do something here...
|
|
119 |
}
|
|
120 |
triggerAction();
|
|
121 |
}
|
|
122 |
|
|
123 |
/**
|
|
124 |
* Triggers this sensor piggybacking a memory usage object.
|
|
125 |
* This method sets this sensor on
|
30655
|
126 |
* and increments the count with the input {@code increment}.
|
2
|
127 |
*/
|
|
128 |
public void trigger(int increment, MemoryUsage usage) {
|
|
129 |
synchronized (lock) {
|
|
130 |
on = true;
|
|
131 |
count += increment;
|
|
132 |
// Do something here...
|
|
133 |
}
|
|
134 |
triggerAction(usage);
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Clears this sensor.
|
|
139 |
*/
|
|
140 |
public void clear() {
|
|
141 |
synchronized (lock) {
|
|
142 |
on = false;
|
|
143 |
}
|
|
144 |
clearAction();
|
|
145 |
}
|
|
146 |
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Clears this sensor
|
30655
|
150 |
* and increments the count with the input {@code increment}.
|
2
|
151 |
*/
|
|
152 |
public void clear(int increment) {
|
|
153 |
synchronized (lock) {
|
|
154 |
on = false;
|
|
155 |
count += increment;
|
|
156 |
}
|
|
157 |
clearAction();
|
|
158 |
}
|
|
159 |
|
|
160 |
public String toString() {
|
|
161 |
return "Sensor - " + getName() +
|
|
162 |
(isOn() ? " on " : " off ") +
|
|
163 |
" count = " + getCount();
|
|
164 |
}
|
|
165 |
|
|
166 |
abstract void triggerAction();
|
|
167 |
abstract void triggerAction(MemoryUsage u);
|
|
168 |
abstract void clearAction();
|
|
169 |
}
|