01: package com.jamonapi;
02:
03: import java.util.*;
04: import java.text.*;
05:
06: /** Class that tracks when a Monitor was first and last called. **/
07:
08: public class LastAccessMonitor extends AccumulateMonitor {
09: static private final String FIRSTACCESS = "First access";
10: static private final String LASTACCESS = "Last access";
11: private boolean isFirstAccess = true;
12: private long firstAccessTime;
13:
14: public LastAccessMonitor() {
15: super ();
16: }
17:
18: public LastAccessMonitor(AccumulateMonitorInterface childMonitor) {
19: super (childMonitor);
20: }
21:
22: /** Updates the accrued value which indicates when this method was last invoked. This is used to track when the
23: * monitor was last called
24: **/
25: synchronized protected void increaseThis(long increaseValue) {
26: accrued = System.currentTimeMillis();
27: }
28:
29: /** Erase/wipe out first accessed and last accessed variables **/
30: synchronized protected void resetThis(long increaseValue) {
31: firstAccessTime = System.currentTimeMillis();
32: accrued = System.currentTimeMillis();
33: }
34:
35: /** If this is the first time the method is called update the first accessed variable with the current time.
36: **/
37: protected void startThis() {
38: if (isFirstAccess) {
39: isFirstAccess = false;
40: firstAccessTime = System.currentTimeMillis();
41: }
42:
43: }
44:
45: private String getDateString(long time) {
46: if (time == 0)
47: return "";
48: else
49: return DateFormat.getDateTimeInstance(DateFormat.SHORT,
50: DateFormat.DEFAULT).format(new Date(time));
51: }
52:
53: synchronized protected String toStringThis() {
54: return getDisplayString(FIRSTACCESS,
55: getDateString(firstAccessTime), NONE)
56: + getDisplayString(LASTACCESS, getDateString(accrued),
57: NONE);
58:
59: }
60:
61: synchronized protected void getDataThis(ArrayList rowData) {
62: rowData.add(getDateString(firstAccessTime));
63: rowData.add(getDateString(accrued));
64: }
65:
66: protected void getHeaderThis(ArrayList header) {
67: header.add(FIRSTACCESS);
68: header.add(LASTACCESS);
69: }
70:
71: /** Test code for this class **/
72: public static void main(String[] args) throws Exception {
73: AccumulateMonitor m = new LastAccessMonitor();
74:
75: m.start();
76:
77: System.out.println("current time=" + m);
78: Thread.sleep(2000);
79: m.increase(1);
80: System.out.println("above time plus about 2 seconds=" + m);
81:
82: }
83: }
|