-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeComparator_252.java
More file actions
275 lines (251 loc) · 9.79 KB
/
DateTimeComparator_252.java
File metadata and controls
275 lines (251 loc) · 9.79 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright 2001-2009 Stephen Colebourne
*
* 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 org.joda.time;
import java.io.Serializable;
import java.util.Comparator;
import org.joda.time.convert.ConverterManager;
import org.joda.time.convert.InstantConverter;
/**
* DateTimeComparator provides comparators to compare one date with another.
* <p>
* Dates may be specified using any object recognised by the
* {@link org.joda.time.convert.ConverterManager ConverterManager} class.
* <p>
* The default objects recognised by the comparator are:
* <ul>
* <li>ReadableInstant
* <li>String
* <li>Calendar
* <li>Date
* <li>Long (milliseconds)
* <li>null (now)
* </ul>
*
* <p>
* DateTimeComparator is thread-safe and immutable.
*
* @author Guy Allard
* @author Stephen Colebourne
* @author Brian S O'Neill
* @since 1.0
*/
public class DateTimeComparator implements Comparator<Object>, Serializable {
/** Serialization lock */
private static final long serialVersionUID = -6097339773320178364L;
/** Singleton instance */
private static final DateTimeComparator ALL_INSTANCE = new DateTimeComparator(null, null);
/** Singleton instance */
private static final DateTimeComparator DATE_INSTANCE = new DateTimeComparator(DateTimeFieldType.dayOfYear(), null);
/** Singleton instance */
private static final DateTimeComparator TIME_INSTANCE = new DateTimeComparator(null, DateTimeFieldType.dayOfYear());
/** The lower limit of fields to compare, null if no limit */
private final DateTimeFieldType iLowerLimit;
/** The upper limit of fields to compare, null if no limit */
private final DateTimeFieldType iUpperLimit;
//-----------------------------------------------------------------------
/**
* Returns a DateTimeComparator the compares the entire date time value.
*
* @return a comparator over all fields
*/
public static DateTimeComparator getInstance() {
return ALL_INSTANCE;
}
/**
* Returns a DateTimeComparator with a lower limit only. Fields of a
* magnitude less than the lower limit are excluded from comparisons.
* <p>
* The time-zone is considered when using this comparator.
* The input millis are truncated using the time-zone of that input value.
* Thus, two inputs with different time-zones will typically not be equal
*
* @param lowerLimit inclusive lower limit for fields to be compared, null means no limit
* @return a comparator over all fields above the lower limit
*/
public static DateTimeComparator getInstance(DateTimeFieldType lowerLimit) {
return getInstance(lowerLimit, null);
}
/**
* Returns a DateTimeComparator with a lower and upper limit. Fields of a
* magnitude less than the lower limit are excluded from comparisons.
* Fields of a magnitude greater than or equal to the upper limit are also
* excluded from comparisons. Either limit may be specified as null, which
* indicates an unbounded limit.
* <p>
* The time-zone is considered when using this comparator unless both limits are null.
* The input millis are rounded/truncated using the time-zone of that input value.
* Thus, two inputs with different time-zones will typically not be equal
*
* @param lowerLimit inclusive lower limit for fields to be compared, null means no limit
* @param upperLimit exclusive upper limit for fields to be compared, null means no limit
* @return a comparator over all fields between the limits
*/
public static DateTimeComparator getInstance(DateTimeFieldType lowerLimit, DateTimeFieldType upperLimit) {
if (lowerLimit == null && upperLimit == null) {
return ALL_INSTANCE;
}
if (lowerLimit == DateTimeFieldType.dayOfYear() && upperLimit == null) {
return DATE_INSTANCE;
}
if (lowerLimit == null && upperLimit == DateTimeFieldType.dayOfYear()) {
return TIME_INSTANCE;
}
return new DateTimeComparator(lowerLimit, upperLimit);
}
/**
* Returns a comparator that only considers date fields.
* Time of day is ignored.
* <p>
* The time-zone is considered when using this comparator.
* The input millis are rounded down to the start of the day
* in the time-zone of that input value. Thus, two inputs with
* different time-zones will typically not be equal
*
* @return a comparator over all date fields
*/
public static DateTimeComparator getDateOnlyInstance() {
return DATE_INSTANCE;
}
/**
* Returns a comparator that only considers time fields.
* Date is ignored.
* <p>
* The time-zone is considered when using this comparator.
* The input millis are truncated to be within the day
* in the time-zone of that input value. Thus, two inputs with
* different time-zones will typically not be equal
*
* @return a comparator over all time fields
*/
public static DateTimeComparator getTimeOnlyInstance() {
return TIME_INSTANCE;
}
/**
* Restricted constructor.
*
* @param lowerLimit the lower field limit, null means no limit
* @param upperLimit the upper field limit, null means no limit
*/
protected DateTimeComparator(DateTimeFieldType lowerLimit, DateTimeFieldType upperLimit) {
super();
iLowerLimit = lowerLimit;
iUpperLimit = upperLimit;
}
//-----------------------------------------------------------------------
/**
* Gets the field type that represents the lower limit of comparison.
*
* @return the field type, null if no upper limit
*/
public DateTimeFieldType getLowerLimit() {
return iLowerLimit;
}
/**
* Gets the field type that represents the upper limit of comparison.
*
* @return the field type, null if no upper limit
*/
public DateTimeFieldType getUpperLimit() {
return iUpperLimit;
}
/**
* Compare two objects against only the range of date time fields as
* specified in the constructor.
*
* @param lhsObj the first object,
* logically on the left of a < comparison, null means now
* @param rhsObj the second object,
* logically on the right of a < comparison, null means now
* @return zero if order does not matter,
* negative value if lhsObj < rhsObj, positive value otherwise.
* @throws IllegalArgumentException if either argument is not supported
*/
public int compare(Object lhsObj, Object rhsObj) {
InstantConverter conv = ConverterManager.getInstance().getInstantConverter(lhsObj);
Chronology lhsChrono = conv.getChronology(lhsObj, (Chronology) null);
long lhsMillis = conv.getInstantMillis(lhsObj, lhsChrono);
conv = ConverterManager.getInstance().getInstantConverter(rhsObj);
Chronology rhsChrono = conv.getChronology(rhsObj, (Chronology) null);
long rhsMillis = conv.getInstantMillis(rhsObj, rhsChrono);
if (iLowerLimit != null) {
lhsMillis = iLowerLimit.getField(lhsChrono).roundFloor(lhsMillis);
rhsMillis = iLowerLimit.getField(rhsChrono).roundFloor(rhsMillis);
}
if (iUpperLimit != null) {
lhsMillis = iUpperLimit.getField(lhsChrono).remainder(lhsMillis);
rhsMillis = iUpperLimit.getField(rhsChrono).remainder(rhsMillis);
}
if (lhsMillis < rhsMillis) {
return -1;
} else if (lhsMillis > rhsMillis) {
return 1;
} else {
return 0;
}
}
//-----------------------------------------------------------------------
/**
* Support serialization singletons.
*
* @return the resolved singleton instance
*/
private Object readResolve() {
return getInstance(iLowerLimit, iUpperLimit);
}
/**
* Compares this comparator to another.
*
* @param object the object to compare to
* @return true if equal
*/
public boolean equals(Object object) {
if (object instanceof DateTimeComparator) {
DateTimeComparator other = (DateTimeComparator) object;
return (iLowerLimit == other.getLowerLimit() ||
(iLowerLimit != null && iLowerLimit.equals(other.getLowerLimit()))) &&
(iUpperLimit == other.getUpperLimit() ||
(iUpperLimit != null && iUpperLimit.equals(other.getUpperLimit())));
}
return false;
}
/**
* Gets a suitable hashcode.
*
* @return the hashcode
*/
public int hashCode() {
return (iLowerLimit == null ? 0 : iLowerLimit.hashCode()) -
(123 * (iUpperLimit == null ? 0 : iUpperLimit.hashCode()));
}
/**
* Gets a debugging string.
*
* @return a debugging string
*/
public String toString() {
if (iLowerLimit == iUpperLimit) {
return "DateTimeComparator["
+ (iLowerLimit == null ? "" : iLowerLimit.getName())
+ "]";
} else {
return "DateTimeComparator["
+ (iLowerLimit == null ? "" : iLowerLimit.getName())
+ "-"
+ (iUpperLimit == null ? "" : iUpperLimit.getName())
+ "]";
}
}
}