-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuration.java
More file actions
228 lines (203 loc) · 6.88 KB
/
Duration.java
File metadata and controls
228 lines (203 loc) · 6.88 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.util.Scanner;
/**
* A Duration object represents a length of time (with millisecond accuracy).
*
*
*
* @author Stephan Jamieson
* @version 28/05/2010
*/
public class Duration implements Comparable<Duration> {
public final static Duration ZERO = new Duration(0);
public final static Duration ONE_MILLISECOND = new Duration(1);
public final static Duration ONE_SECOND = new Duration(1000);
public final static Duration ONE_MINUTE = new Duration(60000);
public final static Duration ONE_HOUR = new Duration(3600000);
public final static Duration ONE_DAY = new Duration(86400000);
public final static Duration ONE_WEEK = new Duration(604800000);
private long milliseconds;
/**
* Create a Duration object that represents the given quantity of milliseconds.
*/
public Duration(long quantity) {
this.milliseconds = quantity;
}
/**
* Create a Duration object that represents the given quantity of the given time unit.
*
* For example, the expression <em>new Duration("minutes", 4)</em> creates a Duration object
* that represents 4 minutes.
*
* Permissible time units are: "millisecond", "second", "minute", "hour", "day".
*
*/
public Duration(String timeUnit, long quantity) {
this.milliseconds = durationOf(timeUnit).multiplyBy(quantity).intValue();
}
/**
* Create a Duration object that represents the same length of time as that given.
*/
public Duration(Duration duration) {
this.milliseconds = duration.intValue();
}
/**
* Obtain an integer value for this duration in milliseconds.
*/
public long intValue() {
return this.milliseconds;
}
/**
* Obtain an integer value that represents that part of this Duration which may be expressed
* as a multiple of the given time unit.
*
* For example, given a duration object d that represents 4 minutes and 30 seconds,
* d.intValue("minute") produces the value 4.
*
* Permissible time units are: "millisecond", "second", "minute", "hour", "day".
*/
public long intValue(String timeUnit) {
return this.divideBy(durationOf(timeUnit));
}
/**
* Obtain a double value for this duration in milliseconds;
*/
public double doubleValue() {
return this.milliseconds;
}
/**
* Obtain a double value that represents this Duration expressed as a multiple of the given
* time unit.
*
* For example, given a duration object d that represents 4 minutes and 30 seconds,
* d.doubleValue("minute") produces the value 4.5.
*
*/
public double doubleValue(String timeUnit) {
Duration unit = durationOf(timeUnit);
return this.divideBy(unit)+this.remainder(unit).doubleValue()/unit.doubleValue();
}
/**
* Obtain the sum of this Duration and the given Duration.
*/
public Duration add(Duration other) {
return new Duration(this.intValue()+other.intValue());
}
/**
* Obtain the result of subtracting the given Duration from this Duration.
*/
public Duration subtract(Duration other) {
return new Duration(this.intValue()-other.intValue());
}
/**
* Obtain the result of multiplying this Duration by the given value.
*/
public Duration multiplyBy(long value) {
return new Duration(this.intValue()*value);
}
/**
* Obtain the result of multiplying this Duration by the given value.
* The result is rounded to the nearest millisecond.
*/
public Duration multiplyBy(double value) {
return new Duration(Math.round(this.intValue()*value));
}
/**
* Obtain the result of dividing this Duration by the given value.
* The result is rounded to the nearest millisecond.
*/
public Duration divideBy(long value) {
long result = this.intValue()/value;
long remainder = this.intValue()%value;
if (remainder*2>=value) {
result = result+1;
}
return new Duration(result);
}
/**
* Obtain the result of dividing this duration by the given value.
* The result is rounded to the nearest millisecond.
*
*/
public Duration divideBy(double value) {
return new Duration(Math.round(this.intValue()/value));
}
/**
* Perform an integer division of this Duration by the given Duration.
*/
public long divideBy(Duration other) {
return this.intValue()/other.intValue();
}
/**
* Obtain the remainder of an integer division of this Duration by the given Duration.
*/
public Duration remainder(Duration modulus) {
return new Duration(this.intValue()%modulus.intValue());
}
/**
* Obtain an absolute (unsigned) instance of this Duration.
*/
public Duration abs() {
return new Duration(Math.abs(this.intValue()));
}
/**
* Returns true if this Duration has a negative value, false otherwise.
*/
public boolean isNegative() {
return this.intValue()<0;
}
/**
* Return a negative, zero, or positive value, depending on whether this duration is smaller,
* equal to, or greater than the given duration.
*/
public int compareTo(Duration other) {
return (int)(this.intValue()-other.intValue());
}
/**
* Obtain a hash code value for this object.
*/
public int hashCode() {
return (int)this.intValue();
}
/**
* Determine whether object o is equivalent to this object.
*
* Object o is equivalent if it is a Duration of the same value as this Duration.
*
*/
public boolean equals(Object o) {
if (!(o instanceof Duration)) {
return false;
}
else {
Duration other = (Duration)o;
return this.intValue()==other.intValue();
}
}
private static Duration durationOf(String timeUnit) {
timeUnit = timeUnit.toLowerCase().trim();
if (timeUnit.charAt(timeUnit.length()-1)=='s') {
timeUnit = timeUnit.substring(0, timeUnit.length()-1);
}
if (timeUnit.equals("millisecond")) {
return ONE_MILLISECOND;
}
else if (timeUnit.equals("second")) {
return ONE_SECOND;
}
else if (timeUnit.equals("minute")) {
return ONE_MINUTE;
}
else if (timeUnit.equals("hour")) {
return ONE_HOUR;
}
else if (timeUnit.equals("day")) {
return ONE_DAY;
}
else if (timeUnit.equals("week")) {
return ONE_WEEK;
}
else {
throw new IllegalArgumentException("Duration: Time unit '"+timeUnit+"' not recognised");
}
}
}