1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.timer;
22
23 import java.text.DecimalFormat;
24
25 /***
26 * A simple interface for measuring time intervals.
27 *
28 * Created on 19.11.2004
29 *
30 * Contributing Author(s):
31 *
32 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
33 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
34 *
35 * @author vlads
36 * @version $Revision: 1.5 $ ($Author: vlads $) $Date: 2004/12/03 09:14:03 $
37 */
38 public class SimpleTimer {
39 private double start_time;
40 private double end_time;
41 private double pause_time;
42
43 private Timer timer;
44
45 public SimpleTimer() {
46 init(true);
47 }
48
49 public SimpleTimer(boolean useNative) {
50 init(useNative);
51 }
52
53 public void init(boolean useNative) {
54 if (useNative && NativeTimer.isAvalabel()) {
55 timer = new NativeTimer();
56 } else {
57 timer = new SystemTimer();
58 }
59 start_time = timer.getTimeCounter();
60 end_time = 0;
61 pause_time = 0;
62 }
63
64 public void start() {
65 start_time = timer.getTimeCounter();
66 }
67
68 public void end() {
69 end_time = timer.getTimeCounter();
70 un_pause();
71 }
72
73 public void pause() {
74 if (pause_time == 0) {
75 pause_time = timer.getTimeCounter();
76 }
77 }
78
79 public void un_pause() {
80 if (pause_time != 0) {
81 double un_pause_time = timer.getTimeCounter();
82 start_time = start_time + (un_pause_time - pause_time);
83 pause_time = 0;
84 }
85 }
86
87 public double duration() {
88 return end_time - start_time;
89 }
90
91
92 public String text() {
93 return text(duration());
94 }
95
96 public String toString() {
97 return text();
98 }
99
100 public String text2(long cnt) {
101 return text(duration()/ cnt);
102 }
103
104 public static String text(double duration) {
105 final DecimalFormat format = new DecimalFormat();
106 format.setMinimumFractionDigits(4);
107 format.setMaximumFractionDigits(4);
108 return format.format(duration);
109 }
110
111 public static String textPrc(double duration, double durationTotal) {
112 if (durationTotal == 0.0) {
113 return "n/a";
114 }
115 final DecimalFormat format = new DecimalFormat("##0");
116 format.setMinimumIntegerDigits(1);
117 format.setMaximumFractionDigits(0);
118
119 int prc = (new Double(Math.floor(100 * duration/durationTotal))).intValue();
120 return format.format(prc);
121 }
122 }