-
Notifications
You must be signed in to change notification settings - Fork 434
/
BaseDuration.java
137 lines (112 loc) · 4.02 KB
/
BaseDuration.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
/*
* Copyright 2003-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package groovy.time;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.StringGroovyMethods;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* Base class for date and time durations.
*
* @author John Wilson tug@wilson.co.uk
* @see Duration
*/
public abstract class BaseDuration implements Comparable<BaseDuration> {
protected final int years;
protected final int months;
protected final int days;
protected final int hours;
protected final int minutes;
protected final int seconds;
protected final int millis;
protected BaseDuration(final int years, final int months, final int days, final int hours, final int minutes, final int seconds, final int millis) {
this.years = years;
this.months = months;
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.millis = millis;
}
protected BaseDuration(final int days, final int hours, final int minutes, final int seconds, final int millis) {
this(0, 0, days, hours, minutes, seconds, millis);
}
public int getYears() {
return this.years;
}
public int getMonths() {
return this.months;
}
public int getDays() {
return this.days;
}
public int getHours() {
return this.hours;
}
public int getMinutes() {
return this.minutes;
}
public int getSeconds() {
return this.seconds;
}
public int getMillis() {
return this.millis;
}
public Date plus(final Date date) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, this.years);
cal.add(Calendar.MONTH, this.months);
cal.add(Calendar.DAY_OF_YEAR, this.days);
cal.add(Calendar.HOUR_OF_DAY, this.hours);
cal.add(Calendar.MINUTE, this.minutes);
cal.add(Calendar.SECOND, this.seconds);
cal.add(Calendar.MILLISECOND, this.millis);
return cal.getTime();
}
public String toString() {
List buffer = new ArrayList();
if (this.years != 0) buffer.add(this.years + " years");
if (this.months != 0) buffer.add(this.months + " months");
if (this.days != 0) buffer.add(this.days + " days");
if (this.hours != 0) buffer.add(this.hours + " hours");
if (this.minutes != 0) buffer.add(this.minutes + " minutes");
if (this.seconds != 0 || this.millis != 0) {
int norm_millis = millis % 1000;
int norm_seconds = seconds + DefaultGroovyMethods.intdiv(millis - norm_millis, 1000).intValue();
buffer.add((norm_seconds == 0 ? (norm_millis < 0 ? "-0" : "0") : norm_seconds) + "." + StringGroovyMethods.padLeft("" + Math.abs(norm_millis), 3, "0") + " seconds");
}
if (buffer.size() != 0) {
return DefaultGroovyMethods.join(buffer, ", ");
} else {
return "0";
}
}
public abstract long toMilliseconds();
public abstract Date getAgo();
public abstract From getFrom();
public int compareTo(BaseDuration otherDuration) {
return Long.signum(toMilliseconds() - otherDuration.toMilliseconds());
}
public abstract static class From {
public abstract Date getNow();
public Date getToday() {
return getNow();
}
}
}