Revision f1a25720
Added by Katja Luther over 6 years ago
- ID f1a25720755daa96e8838a617a660dfced7b9f31
- Parent 23e50194
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/TimePeriod.java | ||
---|---|---|
10 | 10 |
package eu.etaxonomy.cdm.model.common; |
11 | 11 |
|
12 | 12 |
import java.io.Serializable; |
13 |
import java.time.Instant; |
|
14 |
import java.time.LocalDate; |
|
15 |
import java.time.LocalDateTime; |
|
16 |
import java.time.Year; |
|
17 |
import java.time.YearMonth; |
|
18 |
import java.time.ZoneId; |
|
19 |
import java.time.temporal.ChronoField; |
|
20 |
import java.time.temporal.Temporal; |
|
13 | 21 |
import java.util.Calendar; |
14 | 22 |
import java.util.Date; |
15 | 23 |
|
... | ... | |
28 | 36 |
import org.hibernate.search.annotations.Analyze; |
29 | 37 |
import org.hibernate.search.annotations.Field; |
30 | 38 |
import org.hibernate.search.annotations.FieldBridge; |
31 |
import org.joda.time.DateTime; |
|
32 |
import org.joda.time.DateTimeFieldType; |
|
33 |
import org.joda.time.LocalDate; |
|
34 |
import org.joda.time.Partial; |
|
35 | 39 |
import org.joda.time.ReadableInstant; |
36 |
import org.joda.time.format.DateTimeFormatter; |
|
37 | 40 |
|
38 | 41 |
import com.fasterxml.jackson.annotation.JsonIgnore; |
39 | 42 |
|
... | ... | |
59 | 62 |
public class TimePeriod implements Cloneable, Serializable { |
60 | 63 |
private static final long serialVersionUID = 3405969418194981401L; |
61 | 64 |
private static final Logger logger = Logger.getLogger(TimePeriod.class); |
62 |
public static final DateTimeFieldType MONTH_TYPE = DateTimeFieldType.monthOfYear(); |
|
63 |
public static final DateTimeFieldType YEAR_TYPE = DateTimeFieldType.year(); |
|
64 |
public static final DateTimeFieldType DAY_TYPE = DateTimeFieldType.dayOfMonth(); |
|
65 |
// public static final DateTimeFieldType MONTH_TYPE = DateTimeFieldType.monthOfYear();
|
|
66 |
// public static final DateTimeFieldType YEAR_TYPE = DateTimeFieldType.year();
|
|
67 |
// public static final DateTimeFieldType DAY_TYPE = DateTimeFieldType.dayOfMonth();
|
|
65 | 68 |
|
66 | 69 |
@XmlElement(name = "Start") |
67 | 70 |
@XmlJavaTypeAdapter(value = PartialAdapter.class) |
... | ... | |
69 | 72 |
@Field(analyze = Analyze.NO) |
70 | 73 |
@FieldBridge(impl = PartialBridge.class) |
71 | 74 |
@JsonIgnore // currently used for swagger model scanner |
72 |
private Partial start;
|
|
75 |
private Temporal start;
|
|
73 | 76 |
|
74 | 77 |
@XmlElement(name = "End") |
75 | 78 |
@XmlJavaTypeAdapter(value = PartialAdapter.class) |
... | ... | |
77 | 80 |
@Field(analyze = Analyze.NO) |
78 | 81 |
@FieldBridge(impl = PartialBridge.class) |
79 | 82 |
@JsonIgnore // currently used for swagger model scanner |
80 |
private Partial end;
|
|
83 |
private Temporal end;
|
|
81 | 84 |
|
82 | 85 |
|
83 | 86 |
@XmlElement(name = "FreeText") |
... | ... | |
98 | 101 |
* Factory method |
99 | 102 |
* @return |
100 | 103 |
*/ |
101 |
public static TimePeriod NewInstance(Partial startDate){
|
|
104 |
public static TimePeriod NewInstance(Temporal startDate){
|
|
102 | 105 |
return new TimePeriod(startDate); |
103 | 106 |
} |
104 | 107 |
|
... | ... | |
107 | 110 |
* Factory method |
108 | 111 |
* @return |
109 | 112 |
*/ |
110 |
public static TimePeriod NewInstance(Partial startDate, Partial endDate){
|
|
113 |
public static TimePeriod NewInstance(LocalDate startDate, LocalDate endDate){
|
|
111 | 114 |
return new TimePeriod(startDate, endDate); |
112 | 115 |
} |
113 | 116 |
|
... | ... | |
126 | 129 |
* @return |
127 | 130 |
*/ |
128 | 131 |
public static TimePeriod NewInstance(Integer startYear, Integer endYear){ |
129 |
Partial startDate = null;
|
|
130 |
Partial endDate = null;
|
|
132 |
Temporal startDate = null;
|
|
133 |
Temporal endDate = null;
|
|
131 | 134 |
if (startYear != null){ |
132 |
startDate = new Partial().with(YEAR_TYPE, startYear);
|
|
135 |
startDate = Year.of(startYear);
|
|
133 | 136 |
} |
134 | 137 |
if (endYear != null){ |
135 |
endDate = new Partial().with(YEAR_TYPE, endYear);
|
|
138 |
endDate = Year.of(endYear);
|
|
136 | 139 |
} |
137 | 140 |
return new TimePeriod(startDate, endDate); |
138 | 141 |
} |
... | ... | |
152 | 155 |
* The <code>ReadableInstant</code> is stored as the starting instant. |
153 | 156 |
* @return |
154 | 157 |
*/ |
155 |
public static TimePeriod NewInstance(ReadableInstant readableInstant){
|
|
158 |
public static TimePeriod NewInstance(Instant readableInstant){ |
|
156 | 159 |
return NewInstance(readableInstant, null); |
157 | 160 |
} |
158 | 161 |
|
... | ... | |
161 | 164 |
* @return |
162 | 165 |
*/ |
163 | 166 |
public static TimePeriod NewInstance(Calendar startCalendar, Calendar endCalendar){ |
164 |
Partial startDate = null;
|
|
165 |
Partial endDate = null;
|
|
167 |
LocalDate startDate = null;
|
|
168 |
LocalDate endDate = null;
|
|
166 | 169 |
if (startCalendar != null){ |
167 | 170 |
startDate = calendarToPartial(startCalendar); |
168 | 171 |
} |
... | ... | |
196 | 199 |
* Factory method to create a TimePeriod from a starting and an ending <code>ReadableInstant</code>(e.g. <code>DateTime</code>) |
197 | 200 |
* @return |
198 | 201 |
*/ |
199 |
public static TimePeriod NewInstance(ReadableInstant startInstant, ReadableInstant endInstant){
|
|
200 |
Partial startDate = null;
|
|
201 |
Partial endDate = null;
|
|
202 |
public static TimePeriod NewInstance(Instant startInstant, Instant endInstant){
|
|
203 |
LocalDate startDate = null;
|
|
204 |
LocalDate endDate = null;
|
|
202 | 205 |
if (startInstant != null){ |
203 | 206 |
startDate = readableInstantToPartial(startInstant); |
204 | 207 |
} |
... | ... | |
215 | 218 |
* @param calendar |
216 | 219 |
* @return |
217 | 220 |
*/ |
218 |
public static Partial calendarToPartial(Calendar calendar){
|
|
219 |
LocalDate ld = new LocalDate(calendar);
|
|
220 |
Partial partial = new Partial(ld); |
|
221 |
return partial;
|
|
221 |
public static LocalDate calendarToPartial(Calendar calendar){
|
|
222 |
LocalDate ld = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) +1, calendar.get(Calendar.DAY_OF_MONTH));
|
|
223 |
|
|
224 |
return ld;
|
|
222 | 225 |
} |
223 | 226 |
|
224 | 227 |
/** |
... | ... | |
226 | 229 |
* @param calendar |
227 | 230 |
* @return |
228 | 231 |
*/ |
229 |
public static Partial readableInstantToPartial(ReadableInstant readableInstant){ |
|
230 |
DateTime dt = readableInstant.toInstant().toDateTime(); |
|
231 |
LocalDate ld = dt.toLocalDate(); |
|
232 |
Partial partial = new Partial(ld); |
|
233 |
return partial; |
|
232 |
public static LocalDate readableInstantToPartial(Instant readableInstant){ |
|
233 |
// Date date = Date.from(readableInstant); |
|
234 |
LocalDateTime ld = LocalDateTime.ofInstant(readableInstant, ZoneId.systemDefault()); |
|
235 |
return ld.toLocalDate(); |
|
234 | 236 |
} |
235 | 237 |
|
236 | 238 |
|
237 |
public static Integer getPartialValue(Partial partial, DateTimeFieldType type){
|
|
239 |
public static Integer getPartialValue(Temporal partial, ChronoField type){
|
|
238 | 240 |
if (partial == null || ! partial.isSupported(type)){ |
239 | 241 |
return null; |
240 | 242 |
}else{ |
... | ... | |
252 | 254 |
protected TimePeriod() { |
253 | 255 |
super(); |
254 | 256 |
} |
255 |
public TimePeriod(Partial startDate) {
|
|
257 |
public TimePeriod(Temporal startDate) {
|
|
256 | 258 |
start=startDate; |
257 | 259 |
} |
258 |
public TimePeriod(Partial startDate, Partial endDate) {
|
|
260 |
public TimePeriod(Temporal startDate, Temporal endDate) {
|
|
259 | 261 |
start=startDate; |
260 | 262 |
end=endDate; |
261 | 263 |
} |
... | ... | |
264 | 266 |
|
265 | 267 |
|
266 | 268 |
@JsonIgnore // currently used for swagger model scanner |
267 |
public Partial getStart() {
|
|
269 |
public Temporal getStart() {
|
|
268 | 270 |
return start; |
269 | 271 |
} |
270 | 272 |
|
271 |
public void setStart(Partial start) {
|
|
273 |
public void setStart(Temporal start) {
|
|
272 | 274 |
this.start = start; |
273 | 275 |
} |
274 | 276 |
|
275 | 277 |
|
276 | 278 |
@JsonIgnore // currently used for swagger model scanner |
277 |
public Partial getEnd() {
|
|
279 |
public Temporal getEnd() {
|
|
278 | 280 |
return end; |
279 | 281 |
} |
280 | 282 |
|
281 |
public void setEnd(Partial end) {
|
|
283 |
public void setEnd(Temporal end) {
|
|
282 | 284 |
this.end = end; |
283 | 285 |
} |
284 | 286 |
|
... | ... | |
354 | 356 |
|
355 | 357 |
@Transient |
356 | 358 |
public Integer getStartYear(){ |
357 |
return getPartialValue(start, YEAR_TYPE); |
|
359 |
if (start instanceof Temporal){ |
|
360 |
return getPartialValue(start, ChronoField.YEAR); |
|
361 |
} |
|
362 |
return null; |
|
358 | 363 |
} |
359 | 364 |
|
360 | 365 |
@Transient |
361 | 366 |
public Integer getStartMonth(){ |
362 |
return getPartialValue(start, MONTH_TYPE); |
|
367 |
if (start instanceof Temporal){ |
|
368 |
return getPartialValue(start, ChronoField.MONTH_OF_YEAR); |
|
369 |
} |
|
370 |
return null; |
|
363 | 371 |
} |
364 | 372 |
|
365 | 373 |
@Transient |
366 | 374 |
public Integer getStartDay(){ |
367 |
return getPartialValue(start, DAY_TYPE); |
|
375 |
if (start instanceof Temporal){ |
|
376 |
return getPartialValue(start, ChronoField.DAY_OF_MONTH); |
|
377 |
} |
|
378 |
return null; |
|
368 | 379 |
} |
369 | 380 |
|
370 | 381 |
@Transient |
371 | 382 |
public Integer getEndYear(){ |
372 |
return getPartialValue(end, YEAR_TYPE); |
|
383 |
if (end instanceof Temporal){ |
|
384 |
return getPartialValue(end, ChronoField.YEAR); |
|
385 |
} |
|
386 |
return null; |
|
373 | 387 |
} |
374 | 388 |
|
375 | 389 |
@Transient |
376 | 390 |
public Integer getEndMonth(){ |
377 |
return getPartialValue(end, MONTH_TYPE); |
|
391 |
if (end instanceof LocalDate){ |
|
392 |
return getPartialValue(end, ChronoField.MONTH_OF_YEAR); |
|
393 |
} |
|
394 |
return null; |
|
378 | 395 |
} |
379 | 396 |
|
380 | 397 |
@Transient |
381 | 398 |
public Integer getEndDay(){ |
382 |
return getPartialValue(end, DAY_TYPE); |
|
399 |
if (end instanceof LocalDate){ |
|
400 |
return getPartialValue(end, ChronoField.DAY_OF_MONTH); |
|
401 |
} |
|
402 |
return null; |
|
383 | 403 |
} |
384 | 404 |
|
405 |
|
|
385 | 406 |
public TimePeriod setStartYear(Integer year){ |
386 |
return setStartField(year, YEAR_TYPE);
|
|
407 |
return setStartField(year, ChronoField.YEAR);
|
|
387 | 408 |
} |
388 | 409 |
|
389 | 410 |
public TimePeriod setStartMonth(Integer month) throws IndexOutOfBoundsException{ |
390 |
return setStartField(month, MONTH_TYPE);
|
|
411 |
return setStartField(month, ChronoField.MONTH_OF_YEAR);
|
|
391 | 412 |
} |
392 | 413 |
|
393 | 414 |
public TimePeriod setStartDay(Integer day) throws IndexOutOfBoundsException{ |
394 |
return setStartField(day, DAY_TYPE);
|
|
415 |
return setStartField(day, ChronoField.DAY_OF_MONTH);
|
|
395 | 416 |
} |
396 | 417 |
|
397 | 418 |
public TimePeriod setEndYear(Integer year){ |
398 |
return setEndField(year, YEAR_TYPE);
|
|
419 |
return setEndField(year, ChronoField.YEAR);
|
|
399 | 420 |
} |
400 | 421 |
|
401 | 422 |
public TimePeriod setEndMonth(Integer month) throws IndexOutOfBoundsException{ |
402 |
return setEndField(month, MONTH_TYPE);
|
|
423 |
return setEndField(month, ChronoField.MONTH_OF_YEAR);
|
|
403 | 424 |
} |
404 | 425 |
|
405 | 426 |
public TimePeriod setEndDay(Integer day) throws IndexOutOfBoundsException{ |
406 |
return setEndField(day, DAY_TYPE);
|
|
427 |
return setEndField(day, ChronoField.DAY_OF_MONTH);
|
|
407 | 428 |
} |
408 | 429 |
|
409 |
public static Partial setPartialField(Partial partial, Integer value, DateTimeFieldType type)
|
|
430 |
public static Temporal setPartialField(Temporal partial, Integer value, ChronoField type)
|
|
410 | 431 |
throws IndexOutOfBoundsException{ |
411 |
if (partial == null){ |
|
412 |
partial = new Partial();
|
|
413 |
} |
|
432 |
// if (partial == null){
|
|
433 |
// partial = LocalDate.now();
|
|
434 |
// }
|
|
414 | 435 |
if (value == null){ |
415 |
return partial.without(type); |
|
436 |
if (partial instanceof LocalDate){ |
|
437 |
return (partial); |
|
438 |
} |
|
416 | 439 |
}else{ |
417 | 440 |
checkFieldValues(value, type, partial); |
418 |
return partial.with(type, value); |
|
441 |
if (partial instanceof LocalDate){ |
|
442 |
return ((LocalDate)partial).with(type, value); |
|
443 |
} else if (partial instanceof Year){ |
|
444 |
if(type.equals(ChronoField.MONTH_OF_YEAR)){ |
|
445 |
return ((Year)partial).atMonth(value); |
|
446 |
}else if (type.equals(ChronoField.YEAR)){ |
|
447 |
return ((Year)partial).with(type, value); |
|
448 |
} else if (type.equals(ChronoField.DAY_OF_MONTH)){ |
|
449 |
return ((Year)partial).atDay(value); |
|
450 |
} |
|
451 |
} else if (partial instanceof YearMonth){ |
|
452 |
if(type.equals(ChronoField.MONTH_OF_YEAR)){ |
|
453 |
return ((YearMonth)partial).withMonth(value); |
|
454 |
}else if (type.equals(ChronoField.YEAR)){ |
|
455 |
return ((YearMonth)partial).with(type, value); |
|
456 |
} else if (type.equals(ChronoField.DAY_OF_MONTH)){ |
|
457 |
return ((YearMonth)partial).atDay(value); |
|
458 |
} |
|
459 |
} else if (partial == null){ |
|
460 |
if(type.equals(ChronoField.MONTH_OF_YEAR) || type.equals(ChronoField.DAY_OF_MONTH) ){ |
|
461 |
return null; |
|
462 |
}else if (type.equals(ChronoField.YEAR)){ |
|
463 |
return Year.of(value); |
|
464 |
} |
|
465 |
} |
|
466 |
|
|
467 |
|
|
419 | 468 |
} |
469 |
return partial; |
|
420 | 470 |
} |
421 | 471 |
|
422 | 472 |
@Transient |
423 |
private TimePeriod setStartField(Integer value, DateTimeFieldType type)
|
|
473 |
private TimePeriod setStartField(Integer value, ChronoField type)
|
|
424 | 474 |
throws IndexOutOfBoundsException{ |
425 | 475 |
start = setPartialField(start, value, type); |
426 | 476 |
return this; |
427 | 477 |
} |
428 | 478 |
|
429 | 479 |
@Transient |
430 |
private TimePeriod setEndField(Integer value, DateTimeFieldType type)
|
|
480 |
private TimePeriod setEndField(Integer value, ChronoField type)
|
|
431 | 481 |
throws IndexOutOfBoundsException{ |
432 | 482 |
end = setPartialField(end, value, type); |
433 | 483 |
return this; |
... | ... | |
442 | 492 |
* @param type |
443 | 493 |
* @throws IndexOutOfBoundsException |
444 | 494 |
*/ |
445 |
private static void checkFieldValues(Integer value, DateTimeFieldType type, Partial partial)
|
|
495 |
private static void checkFieldValues(Integer value, ChronoField type, Temporal partial)
|
|
446 | 496 |
throws IndexOutOfBoundsException{ |
447 | 497 |
int max = 9999999; |
448 |
if (type.equals(MONTH_TYPE)){
|
|
498 |
if (type.equals(ChronoField.MONTH_OF_YEAR)){
|
|
449 | 499 |
max = 12; |
450 | 500 |
} |
451 |
if (type.equals(DAY_TYPE)){
|
|
501 |
if (type.equals(ChronoField.DAY_OF_MONTH)){
|
|
452 | 502 |
max = 31; |
453 | 503 |
Integer month = null; |
454 |
if (partial.isSupported(MONTH_TYPE)){
|
|
455 |
month = partial.get(MONTH_TYPE);
|
|
504 |
if (partial.isSupported(ChronoField.MONTH_OF_YEAR)){
|
|
505 |
month = partial.get(ChronoField.MONTH_OF_YEAR);
|
|
456 | 506 |
} |
457 | 507 |
if (month != null){ |
458 | 508 |
if (month == 2){ |
... | ... | |
469 | 519 |
|
470 | 520 |
private void initStart(){ |
471 | 521 |
if (start == null){ |
472 |
start = new Partial();
|
|
522 |
start = LocalDate.now();
|
|
473 | 523 |
} |
474 | 524 |
} |
475 | 525 |
|
476 | 526 |
private void initEnd(){ |
477 | 527 |
if (end == null){ |
478 |
end = new Partial();
|
|
528 |
end = LocalDate.now();
|
|
479 | 529 |
} |
480 | 530 |
} |
481 | 531 |
|
... | ... | |
491 | 541 |
@Override |
492 | 542 |
public String toString(){ |
493 | 543 |
String result = null; |
494 |
DateTimeFormatter formatter = TimePeriodPartialFormatter.NewInstance();
|
|
544 |
// TimePeriodPartialFormatter formatter = TimePeriodPartialFormatter.NewInstance();
|
|
495 | 545 |
if ( StringUtils.isNotBlank(this.getFreeText())){ |
496 | 546 |
result = this.getFreeText(); |
497 | 547 |
}else{ |
... | ... | |
508 | 558 |
|
509 | 559 |
public String getTimePeriod(){ |
510 | 560 |
String result = null; |
511 |
DateTimeFormatter formatter = TimePeriodPartialFormatter.NewInstance();
|
|
512 |
String strStart = start != null ? start.toString(formatter): null;
|
|
513 |
String strEnd = end != null ? end.toString(formatter): null;
|
|
561 |
TimePeriodPartialFormatter formatter = TimePeriodPartialFormatter.NewInstance();
|
|
562 |
String strStart = start != null ? formatter.print(start): null;
|
|
563 |
String strEnd = end != null ? formatter.print(end): null;
|
|
514 | 564 |
result = CdmUtils.concat("-", strStart, strEnd); |
515 | 565 |
|
516 | 566 |
return result; |
Also available in: Unified diff
first implementation for change from joda time to java8 time