1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml2.env;
18
19 import java.util.Timer;
20 import java.util.TimerTask;
21
22 import org.apache.commons.scxml2.model.ModelException;
23
24
25
26
27
28
29
30
31
32
33
34 public class StopWatch extends AbstractStateMachine {
35
36
37 public static final String EVENT_START = "watch.start",
38 EVENT_STOP = "watch.stop", EVENT_SPLIT = "watch.split",
39 EVENT_UNSPLIT = "watch.unsplit", EVENT_RESET = "watch.reset";
40
41
42 private int hr, min, sec, fract;
43
44 private int dhr, dmin, dsec, dfract;
45
46 private boolean split;
47
48 private Timer timer;
49
50 private static final String DELIM = ":", DOT = ".", EMPTY = "", ZERO = "0";
51
52 public StopWatch() throws ModelException {
53 super(StopWatch.class.getClassLoader().
54 getResource("org/apache/commons/scxml2/env/stopwatch.xml"));
55 }
56
57
58
59 public void reset() {
60 hr = min = sec = fract = dhr = dmin = dsec = dfract = 0;
61 split = false;
62 }
63
64 public void running() {
65 split = false;
66 if (timer == null) {
67 timer = new Timer(true);
68 timer.scheduleAtFixedRate(new TimerTask() {
69 @Override
70 public void run() {
71 increment();
72 }
73 }, 100, 100);
74 }
75 }
76
77 public void paused() {
78 split = true;
79 }
80
81 public void stopped() {
82 timer.cancel();
83 timer = null;
84 }
85
86 public String getDisplay() {
87 String padhr = dhr > 9 ? EMPTY : ZERO;
88 String padmin = dmin > 9 ? EMPTY : ZERO;
89 String padsec = dsec > 9 ? EMPTY : ZERO;
90 return new StringBuffer().append(padhr).append(dhr).append(DELIM).
91 append(padmin).append(dmin).append(DELIM).append(padsec).
92 append(dsec).append(DOT).append(dfract).toString();
93 }
94
95
96 public String getCurrentState() {
97 return getEngine().getStatus().getStates().iterator().next().getId();
98 }
99
100 private void increment() {
101 if (fract < 9) {
102 fract++;
103 } else {
104 fract = 0;
105 if (sec < 59) {
106 sec++;
107 } else {
108 sec = 0;
109 if (min < 59) {
110 min++;
111 } else {
112 min = 0;
113 if (hr < 99) {
114 hr++;
115 } else {
116 hr = 0;
117 }
118 }
119 }
120 }
121 if (!split) {
122 dhr = hr;
123 dmin = min;
124 dsec = sec;
125 dfract = fract;
126 }
127 }
128
129 }
130