blob: 37c1a6e3a94403e73af139de8bc2c9f238b735b3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.statistics;
import java.util.TimerTask;
/**
* Base class for the interface to the statistics framework.
*
* @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
*/
public abstract class Handle {
private TimerTask task;
private final String name;
private boolean cancelled;
private final Statistics manager;
private final Callback parametrizedCallback;
private boolean firstTime;
Handle(String name, Statistics manager, Callback parametrizedCallback) {
this.name = name;
this.manager = manager;
this.parametrizedCallback = parametrizedCallback;
firstTime = true;
}
String getName() {
return name;
}
TimerTask makeTask() {
final Handle self = this;
synchronized (self) {
if (task != null) {
task.cancel();
}
task = new TimerTask() {
public void run() {
self.run();
}
};
return task;
}
}
/**
* Run the callback object.
*
* This will happen at the start of each invocation of a Handle's
* run() method. The callback is presumed to be exception safe.
* If no callback is set, this is a no-op. The callback will need
* to handle any necessary synchronization itself.
*/
public final void runCallback() {
if (parametrizedCallback == null) {
return;
}
parametrizedCallback.run(this, firstTime);
firstTime = false;
}
/**
* Run the callback object first, then invoke runHandle().
*/
public final void run() {
runCallback();
runHandle();
}
/**
* Invoke an action to be performed periodically for a statistics Handle.
*
* <p>Synchronization has to be handled by the method itself.
*/
public abstract void runHandle();
/**
* Cancel this Handle and remove it from internal state in Statistics.
*
* @return value of java.util.TimerTask.cancel()
*/
public final boolean cancel() {
boolean ok = (task == null ? false : task.cancel());
cancelled = true;
manager.purge();
return ok;
}
/**
* Returns whether this object has been cancelled or not.
*
* @return true if cancelled
*/
public final boolean isCancelled() {
return cancelled;
}
@Override
public abstract boolean equals(Object o);
@Override
public abstract int hashCode();
}
|