1 | /**
|
---|
2 | * TimeKeeper.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.gui;
|
---|
20 |
|
---|
21 | import java.text.DecimalFormat;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Provides methods an easy way of measuring time. Currently uses
|
---|
25 | * System.nanoTime to get the most precise time measurement available, results
|
---|
26 | * are then converted to milliseconds.
|
---|
27 | *
|
---|
28 | * @author jdm18
|
---|
29 | */
|
---|
30 | public class TimeKeeper {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * The Unit of measure for the returned time values
|
---|
34 | */
|
---|
35 | public static DecimalFormat Formatter = new DecimalFormat("0.00");
|
---|
36 |
|
---|
37 | private long _start = 0;
|
---|
38 |
|
---|
39 | private static String MILLIS = "ms";
|
---|
40 |
|
---|
41 | private static String SECONDS = "sec";
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new TimeKeeper object and initiates the starting time
|
---|
45 | */
|
---|
46 | public TimeKeeper() {
|
---|
47 | restart();
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Returns the time Sandbox has been running, in the time unit set in UNIT.
|
---|
52 | *
|
---|
53 | * @return The amount of time Sandbox has been running.
|
---|
54 | */
|
---|
55 | public static long getCurrentTime() {
|
---|
56 | long nanoTime = System.nanoTime();
|
---|
57 |
|
---|
58 | nanoTime /= 1000000;
|
---|
59 |
|
---|
60 | return nanoTime;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Resets the starting time of this TimeKeeper to the value returned by
|
---|
65 | * getCurrentTime()
|
---|
66 | */
|
---|
67 | public void restart() {
|
---|
68 | _start = getCurrentTime();
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Returns the difference between the current time (getCurrentTime()) and
|
---|
73 | * this TimeKeeper's starting time.
|
---|
74 | *
|
---|
75 | * @return The time that has elapsed since this TimeKeeper's start time.
|
---|
76 | */
|
---|
77 | public long getElapsedMillis() {
|
---|
78 | return getCurrentTime() - _start;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * The same result as getElapsed() but with the unit of measure (UNIT)
|
---|
83 | * appended.
|
---|
84 | *
|
---|
85 | * @return The time that has elapsed since this TimeKeeper's start time,
|
---|
86 | * with the unit of measure displayed.
|
---|
87 | */
|
---|
88 | public String getElapsedStringMillis() {
|
---|
89 | return getElapsedMillis() + MILLIS;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public String getElapsedStringMillis(int pad) {
|
---|
93 | long time = getElapsedMillis();
|
---|
94 | String padding = "";
|
---|
95 | for (int i = 0; i < pad; i++)
|
---|
96 | if (time == 0)
|
---|
97 | padding += " ";
|
---|
98 | else
|
---|
99 | time /= 10;
|
---|
100 |
|
---|
101 | return padding + getElapsedMillis() + MILLIS;
|
---|
102 | }
|
---|
103 |
|
---|
104 | // returns the string in mm:ss format, with seconds shown to 3 decimal
|
---|
105 | // places
|
---|
106 | public String getElapsedStringFull() {
|
---|
107 | int mins = (int) Math.floor(getElapsedSeconds() / 60f);
|
---|
108 | float secs = getElapsedSeconds() - (mins * 60);
|
---|
109 |
|
---|
110 | return mins + ":" + Formatter.format(secs);
|
---|
111 | }
|
---|
112 |
|
---|
113 | public String getElapsedStringSeconds() {
|
---|
114 | return Formatter.format(getElapsedSeconds()) + SECONDS;
|
---|
115 | }
|
---|
116 |
|
---|
117 | public float getElapsedSeconds() {
|
---|
118 | return getElapsedMillis() / 1000f;
|
---|
119 | }
|
---|
120 | }
|
---|