1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.instance.data;
|
11
|
|
|
12
|
|
import java.math.BigDecimal;
|
13
|
|
|
14
|
|
import org.jbind.util.other.StrBuffer;
|
15
|
|
import org.jbind.xml.core.data.CompareResult;
|
16
|
|
import org.jbind.xml.core.data.IDateTime;
|
17
|
|
import org.jbind.xml.core.data.IDuration;
|
18
|
|
|
19
|
|
public class Duration implements IDuration {
|
20
|
|
|
21
|
|
private static final BigDecimal ZERO = new BigDecimal("0");
|
22
|
|
|
23
|
|
private static final DateTime[] DATES = new DateTime[]{ new DateTime(1, 1696, 9, 1, 0, 0, 0, null, true),
|
24
|
|
new DateTime(1, 1697, 2, 1, 0, 0, 0, null, true),
|
25
|
|
new DateTime(1, 1903, 3, 1, 0, 0, 0, null, true),
|
26
|
|
new DateTime(1, 1903, 7, 1, 0, 0, 0, null, true) };
|
27
|
|
|
28
|
|
private int mySign;
|
29
|
|
private int myYears;
|
30
|
|
private int myMonths;
|
31
|
|
private int myDays;
|
32
|
|
private int myHours;
|
33
|
|
private int myMinutes;
|
34
|
|
private int mySeconds;
|
35
|
|
private BigDecimal myFraction;
|
36
|
|
|
37
|
8
|
public Duration(int aSign, int aYears, int aMonths, int aDays, int anHours, int aMinutes, int aSeconds, BigDecimal aFraction) {
|
38
|
8
|
mySign = aSign;
|
39
|
8
|
myYears = aYears;
|
40
|
8
|
myMonths = aMonths;
|
41
|
8
|
myDays = aDays;
|
42
|
8
|
myHours = anHours;
|
43
|
8
|
myMinutes = aMinutes;
|
44
|
8
|
mySeconds = aSeconds;
|
45
|
8
|
myFraction = aFraction;
|
46
|
|
}
|
47
|
|
|
48
|
5
|
public int getDays() {
|
49
|
5
|
return myDays;
|
50
|
|
}
|
51
|
|
|
52
|
8
|
public BigDecimal getFraction() {
|
53
|
8
|
return myFraction;
|
54
|
|
}
|
55
|
|
|
56
|
5
|
public int getHours() {
|
57
|
5
|
return myHours;
|
58
|
|
}
|
59
|
|
|
60
|
5
|
public int getMinutes() {
|
61
|
5
|
return myMinutes;
|
62
|
|
}
|
63
|
|
|
64
|
5
|
public int getMonths() {
|
65
|
5
|
return myMonths;
|
66
|
|
}
|
67
|
|
|
68
|
5
|
public int getSeconds() {
|
69
|
5
|
return mySeconds;
|
70
|
|
}
|
71
|
|
|
72
|
5
|
public int getSign() {
|
73
|
5
|
return mySign;
|
74
|
|
}
|
75
|
|
|
76
|
5
|
public int getYears() {
|
77
|
5
|
return myYears;
|
78
|
|
}
|
79
|
|
|
80
|
4
|
public boolean equals(Object anObject) {
|
81
|
4
|
boolean res = anObject instanceof IDuration;
|
82
|
4
|
if (res) {
|
83
|
4
|
IDuration d = (IDuration)anObject;
|
84
|
4
|
res = (mySign == d.getSign()) && (myYears == d.getYears()) && (myMonths == d.getMonths()) && (myDays == d.getDays()) && (myHours == d.getHours()) && (myMinutes == d.getMinutes()) && (mySeconds == d.getSeconds());
|
85
|
4
|
if (res) {
|
86
|
4
|
BigDecimal f1 = (null != myFraction) ? myFraction : ZERO;
|
87
|
4
|
BigDecimal f2 = (null != d.getFraction()) ? d.getFraction() : ZERO;
|
88
|
4
|
res = 0 == f1.compareTo(f2);
|
89
|
|
}
|
90
|
|
}
|
91
|
4
|
return res;
|
92
|
|
}
|
93
|
|
|
94
|
7
|
public int hashCode() {
|
95
|
7
|
return mySign + myYears + myMonths + myDays + myHours + myMinutes + mySeconds;
|
96
|
|
}
|
97
|
|
|
98
|
0
|
public CompareResult compareTo(IDuration aDuration) {
|
99
|
0
|
IDateTime thisDt = TimeHelper.add(DATES[0], this);
|
100
|
0
|
IDateTime thatDt = TimeHelper.add(DATES[0], aDuration);
|
101
|
0
|
CompareResult res = thisDt.compareTo(thatDt);
|
102
|
0
|
for (int i = 1; i < DATES.length; i++) {
|
103
|
0
|
thisDt = TimeHelper.add(DATES[i], this);
|
104
|
0
|
thatDt = TimeHelper.add(DATES[i], aDuration);
|
105
|
0
|
if (!res.equals(thisDt.compareTo(thatDt))) {
|
106
|
0
|
return CompareResult.UNDETERMINED;
|
107
|
|
}
|
108
|
|
}
|
109
|
0
|
return res;
|
110
|
|
}
|
111
|
|
|
112
|
1
|
public String toString() {
|
113
|
1
|
StrBuffer sb = new StrBuffer();
|
114
|
1
|
if (mySign < 0) {
|
115
|
0
|
sb.append('-');
|
116
|
|
}
|
117
|
1
|
sb.append('P');
|
118
|
1
|
TimeHelper.output(myYears, sb);
|
119
|
1
|
sb.append('Y');
|
120
|
1
|
TimeHelper.output(myMonths, sb);
|
121
|
1
|
sb.append('M');
|
122
|
1
|
TimeHelper.output(myDays, sb);
|
123
|
1
|
sb.append("DT");
|
124
|
1
|
TimeHelper.output(myHours, sb);
|
125
|
1
|
sb.append('H');
|
126
|
1
|
TimeHelper.output(myMinutes, sb);
|
127
|
1
|
sb.append('M');
|
128
|
1
|
TimeHelper.output(mySeconds, sb);
|
129
|
1
|
if ((null != myFraction) && (0 != ZERO.compareTo(myFraction))) {
|
130
|
1
|
sb.append(myFraction.toString().substring(1));
|
131
|
|
}
|
132
|
1
|
sb.append('S');
|
133
|
1
|
return sb.toString();
|
134
|
|
}
|
135
|
|
}
|
136
|
|
|