-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.java
165 lines (146 loc) · 5.57 KB
/
Timer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* The Timer program works just like the GNU utility "sleep"
* – it takes arguments, tries to enterpret them as amounts of
* time, then sleeps for that amount of time. The difference is that
* this program supports milliseconds.
*/
public class Timer {
/**
* The UnknownSuffixException is thrown when a suffix is cannot be
* found from a String.
*/
private static class UnknownSuffixException extends Exception {
/**
* Creates a new UnknownSuffixException with the message
* "Unknown suffix.".
*/
public UnknownSuffixException() {
super("Unknown suffix.");
}
/**
* Creates a new UnknownSuffixException with the message
* "Unknown suffix: " followed by the provided suffix.
*
* @param suffix the suffix that caused the exception.
*/
public UnknownSuffixException(String suffix) {
super("Unknown suffix: " + suffix);
}
}
/**
* Determines the amount of time based on a number and a suffix.
* <p>
* The suffix can currently be any of "", "ms", "s", "m", "h" and
* "d".
*
* @param digits a real number representing an amount of time.
* @param suffix a valid suffix representing a time period (as in
* seconds, minutes, hours, days, ...).
* @return an amount of time in milliseconds.
*/
public static int determineTime(double digits, String suffix)
throws UnknownSuffixException {
double time = digits;
if (suffix == null || suffix.isEmpty()) {
/* Don't alter the digits, this the is default
* suffix. This is to support providing null or the empty
* string as the suffix parameter and this sleeping for
* the provided time interval in milliseconds. */
} else if (suffix.equals("ms")) {
/* Don't alter the digits, this the is default suffix. */
} else if (suffix.equals("s")) {
time *= 1000;
} else if (suffix.equals("m")) {
time *= 1000*60;
} else if (suffix.equals("h")) {
time *= 1000*60*60;
} else if (suffix.equals("d")) {
time *= 1000*60*60*24;
} else {
throw new UnknownSuffixException(suffix);
}
return (int) time;
}
/**
* Extracts a suffix from a String on the format
* <tt><digits><suffix></tt>.
*
* @param input the string from which to extract a suffix.
* @return the <tt><suffix></tt> part of <tt>input</tt> if
* one can be found, otherwise the empty string.
*/
public static String extractSuffix(String input) {
char[] inputArray = input.toCharArray();
if (inputArray == null || inputArray.length == 0) {
return null;
}
/* Find out at what index the prefix starts. */
int i = -1;
while (++i < inputArray.length
&& (Character.isDigit(inputArray[i])
|| inputArray[i] == '.')) {}
return input.substring(i, input.length()).trim();
}
/**
* Extracts the digits from a String on the format
* <tt><digits><suffix></tt>.
*
* @param input the String from which to extract digits.
* @return the <tt><digits></tt> part of <tt>input</tt>, if
* one can be found, as a Double.
* @throws NumberFormatException if <digits> cannot be
* interpreted as a Double.
*/
public static Double extractTime(String input)
throws NumberFormatException {
char[] inputArray = input.toCharArray();
if (inputArray == null || inputArray.length == 0) {
return null;
}
/* Find out at what index the prefix starts. */
int i = -1;
while (++i < inputArray.length
&& (Character.isDigit(inputArray[i])
|| inputArray[i] == '.')) {}
return Double.parseDouble(input.substring(0, i).trim());
}
public static void main(String args[]) {
double totalTime;
if (args.length < 1) {
System.err.println("Too few arguments.");
} else {
totalTime = 0;
for (int i = 0; i < args.length; i++) {
try {
String suffix = extractSuffix(args[i]);
double time = extractTime(args[i]);
totalTime += determineTime(time, suffix);
} catch (UnknownSuffixException use) {
System.err.println(use.getMessage());
} catch (NumberFormatException nfe) {
System.err.println("Could not determine time of \""
+ args[i] + "\"");
}
}
try {
Thread.sleep((int) totalTime);
} catch (InterruptedException ie) {
return;
}
}
}
}