source: trunk/org/expeditee/gui/TimeKeeper.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

File size: 2.3 KB
Line 
1package org.expeditee.gui;
2
3import java.text.DecimalFormat;
4
5/**
6 * Provides methods an easy way of measuring time. Currently uses
7 * System.nanoTime to get the most precise time measurement available, results
8 * are then converted to milliseconds.
9 *
10 * @author jdm18
11 */
12public class TimeKeeper {
13
14 /**
15 * The Unit of measure for the returned time values
16 */
17 public static DecimalFormat Formatter = new DecimalFormat("0.00");
18
19 private long _start = 0;
20
21 private static String MILLIS = "ms";
22
23 private static String SECONDS = "sec";
24
25 /**
26 * Constructs a new TimeKeeper object and initiates the starting time
27 */
28 public TimeKeeper() {
29 restart();
30 }
31
32 /**
33 * Returns the time Sandbox has been running, in the time unit set in UNIT.
34 *
35 * @return The amount of time Sandbox has been running.
36 */
37 public static long getCurrentTime() {
38 long nanoTime = System.nanoTime();
39
40 nanoTime /= 1000000;
41
42 return nanoTime;
43 }
44
45 /**
46 * Resets the starting time of this TimeKeeper to the value returned by
47 * getCurrentTime()
48 */
49 public void restart() {
50 _start = getCurrentTime();
51 }
52
53 /**
54 * Returns the difference between the current time (getCurrentTime()) and
55 * this TimeKeeper's starting time.
56 *
57 * @return The time that has elapsed since this TimeKeeper's start time.
58 */
59 public long getElapsedMillis() {
60 return getCurrentTime() - _start;
61 }
62
63 /**
64 * The same result as getElapsed() but with the unit of measure (UNIT)
65 * appended.
66 *
67 * @return The time that has elapsed since this TimeKeeper's start time,
68 * with the unit of measure displayed.
69 */
70 public String getElapsedStringMillis() {
71 return getElapsedMillis() + MILLIS;
72 }
73
74 public String getElapsedStringMillis(int pad) {
75 long time = getElapsedMillis();
76 String padding = "";
77 for (int i = 0; i < pad; i++)
78 if (time == 0)
79 padding += " ";
80 else
81 time /= 10;
82
83 return padding + getElapsedMillis() + MILLIS;
84 }
85
86 // returns the string in mm:ss format, with seconds shown to 3 decimal
87 // places
88 public String getElapsedStringFull() {
89 int mins = (int) Math.floor(getElapsedSeconds() / 60f);
90 float secs = getElapsedSeconds() - (mins * 60);
91
92 return mins + ":" + Formatter.format(secs);
93 }
94
95 public String getElapsedStringSeconds() {
96 return Formatter.format(getElapsedSeconds()) + SECONDS;
97 }
98
99 public float getElapsedSeconds() {
100 return getElapsedMillis() / 1000f;
101 }
102}
Note: See TracBrowser for help on using the repository browser.