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 org.jbind.util.other.StrBuffer;
|
13
|
|
import org.jbind.xml.core.data.CompareResult;
|
14
|
|
import org.jbind.xml.core.data.IDateBase;
|
15
|
|
import org.jbind.xml.core.data.IDateTime;
|
16
|
|
import org.jbind.xml.core.data.IGDate;
|
17
|
|
import org.jbind.xml.core.data.ITimeZone;
|
18
|
|
|
19
|
|
public class GDate implements IGDate, IDateBase {
|
20
|
|
|
21
|
|
// Default values for gXXX types
|
22
|
|
protected static final int DEFAULT_SIGN = 1;
|
23
|
|
protected static final int DEFAULT_YEAR = 1111;
|
24
|
|
// Must be a month with 31 days because otherwise the checkDate method
|
25
|
|
// would allow gDay not to take the value 31.
|
26
|
|
protected static final int DEFAULT_MONTH = 10;
|
27
|
|
protected static final int DEFAULT_DAY = 11;
|
28
|
|
|
29
|
|
private int mySign;
|
30
|
|
private int myYear;
|
31
|
|
private int myMonth;
|
32
|
|
private int myDay;
|
33
|
|
private ITimeZone myTimeZone;
|
34
|
|
|
35
|
14
|
public GDate(int aSign, int aYear, int aMonth, int aDay, ITimeZone aTimeZone) {
|
36
|
14
|
mySign = aSign;
|
37
|
14
|
myYear = aYear;
|
38
|
14
|
myMonth = aMonth;
|
39
|
14
|
myDay = aDay;
|
40
|
14
|
myTimeZone = aTimeZone;
|
41
|
|
}
|
42
|
|
|
43
|
2
|
public int getSign() {
|
44
|
2
|
return mySign;
|
45
|
|
}
|
46
|
39
|
public int getYear() {
|
47
|
39
|
return myYear;
|
48
|
|
}
|
49
|
54
|
public int getMonth() {
|
50
|
54
|
return myMonth;
|
51
|
|
}
|
52
|
39
|
public int getDay() {
|
53
|
39
|
return myDay;
|
54
|
|
}
|
55
|
70
|
public ITimeZone getTimeZone() {
|
56
|
70
|
return myTimeZone;
|
57
|
|
}
|
58
|
|
|
59
|
0
|
protected CompareResult doCompare(GDate aDate) {
|
60
|
0
|
if ((null != getTimeZone()) || (null != aDate.getTimeZone())) {
|
61
|
0
|
IDateTime dt1 = DateTime.create(getSign(), getYear(), getMonth(), getDay(), 0, 0, 0, null, getTimeZone());
|
62
|
0
|
IDateTime dt2 = DateTime.create(aDate.getSign(), aDate.getYear(), aDate.getMonth(), aDate.getDay(), 0, 0, 0, null, aDate.getTimeZone());
|
63
|
0
|
return dt1.compareTo(dt2);
|
64
|
|
} else {
|
65
|
0
|
CompareResult res1, res2;
|
66
|
0
|
if (getSign() < 0) {
|
67
|
0
|
if (aDate.getSign() < 0) {
|
68
|
0
|
res1 = CompareResult.GREATER;
|
69
|
0
|
res2 = CompareResult.LESS;
|
70
|
|
} else {
|
71
|
0
|
res1 = CompareResult.LESS;
|
72
|
0
|
res2 = CompareResult.LESS;
|
73
|
|
}
|
74
|
|
} else {
|
75
|
0
|
if (aDate.getSign() < 0) {
|
76
|
0
|
res1 = CompareResult.GREATER;
|
77
|
0
|
res2 = CompareResult.GREATER;
|
78
|
|
} else {
|
79
|
0
|
res1 = CompareResult.LESS;
|
80
|
0
|
res2 = CompareResult.GREATER;
|
81
|
|
}
|
82
|
|
}
|
83
|
0
|
if (getYear() < aDate.getYear()) {
|
84
|
0
|
return res1;
|
85
|
0
|
} else if (getYear() > aDate.getYear()) {
|
86
|
0
|
return res2;
|
87
|
|
}
|
88
|
0
|
if (getMonth() < aDate.getMonth()) {
|
89
|
0
|
return res1;
|
90
|
0
|
} else if (getMonth() > aDate.getMonth()) {
|
91
|
0
|
return res2;
|
92
|
|
}
|
93
|
0
|
if (getDay() < aDate.getDay()) {
|
94
|
0
|
return res1;
|
95
|
0
|
} else if (getDay() > aDate.getDay()) {
|
96
|
0
|
return res2;
|
97
|
|
}
|
98
|
0
|
return CompareResult.EQUAL;
|
99
|
|
}
|
100
|
|
}
|
101
|
|
|
102
|
4
|
public boolean equals(Object anObject) {
|
103
|
4
|
boolean res = anObject instanceof GDate;
|
104
|
4
|
if (res) {
|
105
|
4
|
GDate d = (GDate)anObject;
|
106
|
4
|
res = (getYear() == d.getYear()) && (getMonth() == d.getMonth()) && (getDay() == d.getDay());
|
107
|
4
|
if (res) {
|
108
|
4
|
if ((null != myTimeZone) && (null != d.getTimeZone())) {
|
109
|
2
|
res = myTimeZone.equals(d.getTimeZone());
|
110
|
2
|
} else if ((null != myTimeZone) || (null != d.getTimeZone())) {
|
111
|
0
|
res = false;
|
112
|
|
}
|
113
|
|
}
|
114
|
|
}
|
115
|
4
|
return res;
|
116
|
|
}
|
117
|
|
|
118
|
10
|
public int hashCode() {
|
119
|
10
|
return mySign + myYear + myMonth + myDay;
|
120
|
|
}
|
121
|
|
|
122
|
0
|
protected void outputSign(StrBuffer aStrBuffer) {
|
123
|
0
|
if (getSign() < 0) {
|
124
|
0
|
aStrBuffer.append('-');
|
125
|
|
}
|
126
|
|
}
|
127
|
|
|
128
|
0
|
protected void outputYear(StrBuffer aStrBuffer) {
|
129
|
0
|
if (getYear() > 9999) {
|
130
|
0
|
StrBuffer b = new StrBuffer();
|
131
|
0
|
for (int x = getYear(); x != 0; x /= 10) {
|
132
|
0
|
b.append((char)('0' + x % 10));
|
133
|
|
}
|
134
|
0
|
for (int i = b.length(); --i >= 0; ) {
|
135
|
0
|
aStrBuffer.append(b.charAt(i));
|
136
|
|
}
|
137
|
|
} else {
|
138
|
0
|
TimeHelper.output(getYear(), 4, aStrBuffer);
|
139
|
|
}
|
140
|
|
}
|
141
|
|
|
142
|
0
|
protected void outputMonth(StrBuffer aStrBuffer) {
|
143
|
0
|
TimeHelper.output(getMonth(), 2, aStrBuffer);
|
144
|
|
}
|
145
|
|
|
146
|
0
|
protected void outputDay(StrBuffer aStrBuffer) {
|
147
|
0
|
TimeHelper.output(getDay(), 2, aStrBuffer);
|
148
|
|
}
|
149
|
|
|
150
|
0
|
protected void outputTimeZone(StrBuffer aStrBuffer) {
|
151
|
0
|
if (null != myTimeZone) {
|
152
|
0
|
aStrBuffer.append(myTimeZone);
|
153
|
|
}
|
154
|
|
}
|
155
|
|
|
156
|
|
}
|
157
|
|
|