4479 |
05 May 17 |
nicklas |
1 |
package net.sf.basedb.reggie.converter; |
4479 |
05 May 17 |
nicklas |
2 |
|
4479 |
05 May 17 |
nicklas |
3 |
import java.util.Date; |
4479 |
05 May 17 |
nicklas |
4 |
|
4479 |
05 May 17 |
nicklas |
5 |
/** |
4479 |
05 May 17 |
nicklas |
Value converter implementation that converts a date to the number |
4570 |
08 Sep 17 |
nicklas |
of days since a reference date. NOTE! The dates used in this class |
4570 |
08 Sep 17 |
nicklas |
are expected to be date-only with a time part set to 00:00:00. If |
4570 |
08 Sep 17 |
nicklas |
not, the calculatations may be off by 1 day due to rounding. |
4479 |
05 May 17 |
nicklas |
10 |
|
4479 |
05 May 17 |
nicklas |
@author nicklas |
4479 |
05 May 17 |
nicklas |
@since 4.10 |
4479 |
05 May 17 |
nicklas |
13 |
*/ |
4479 |
05 May 17 |
nicklas |
14 |
public class DaysSinceRefDateConverter |
4479 |
05 May 17 |
nicklas |
15 |
implements ValueConverter<Date, Integer> |
4479 |
05 May 17 |
nicklas |
16 |
{ |
4479 |
05 May 17 |
nicklas |
17 |
|
4570 |
08 Sep 17 |
nicklas |
18 |
private static long MILLIS_IN_AN_HOUR = 3600 * 1000; |
4570 |
08 Sep 17 |
nicklas |
19 |
private static long MILLIS_IN_A_DAY = 24 * MILLIS_IN_AN_HOUR; |
4570 |
08 Sep 17 |
nicklas |
20 |
|
7022 |
06 Feb 23 |
nicklas |
// Need ThreadLocal so a single instance can handle |
7022 |
06 Feb 23 |
nicklas |
// multiple threads at the same time |
7022 |
06 Feb 23 |
nicklas |
23 |
private final ThreadLocal<Long> refTime; |
4479 |
05 May 17 |
nicklas |
24 |
|
4479 |
05 May 17 |
nicklas |
25 |
public DaysSinceRefDateConverter() |
7022 |
06 Feb 23 |
nicklas |
26 |
{ |
7022 |
06 Feb 23 |
nicklas |
27 |
this.refTime = new ThreadLocal<>(); |
7022 |
06 Feb 23 |
nicklas |
28 |
} |
4479 |
05 May 17 |
nicklas |
29 |
|
4479 |
05 May 17 |
nicklas |
30 |
/* |
4479 |
05 May 17 |
nicklas |
From the ValueConverter interface |
4479 |
05 May 17 |
nicklas |
32 |
--------------------------------- |
4479 |
05 May 17 |
nicklas |
33 |
*/ |
4479 |
05 May 17 |
nicklas |
34 |
@Override |
4479 |
05 May 17 |
nicklas |
35 |
public Integer convert(Date value) |
4479 |
05 May 17 |
nicklas |
36 |
{ |
7022 |
06 Feb 23 |
nicklas |
37 |
Long refT = refTime.get(); |
7022 |
06 Feb 23 |
nicklas |
38 |
return value == null || refT == null ? null : (int)((value.getTime() - refT) / MILLIS_IN_A_DAY); |
4479 |
05 May 17 |
nicklas |
39 |
} |
4479 |
05 May 17 |
nicklas |
40 |
// ---------------------------------- |
4479 |
05 May 17 |
nicklas |
41 |
|
4479 |
05 May 17 |
nicklas |
42 |
|
4479 |
05 May 17 |
nicklas |
43 |
public void setRefDate(Date refDate) |
4479 |
05 May 17 |
nicklas |
44 |
{ |
4570 |
08 Sep 17 |
nicklas |
// We remove MILLIS_IN_AN_HOUR to the reference to fix the problem with |
4570 |
08 Sep 17 |
nicklas |
// daylight savings correction in the spring (when one day is only 23 hours) |
4570 |
08 Sep 17 |
nicklas |
// The calculations rely on rounding down to return the correct result but |
4570 |
08 Sep 17 |
nicklas |
// it is important that no time part is present in the date values |
7022 |
06 Feb 23 |
nicklas |
49 |
this.refTime.set(refDate == null ? null : refDate.getTime() - MILLIS_IN_AN_HOUR); |
4479 |
05 May 17 |
nicklas |
50 |
} |
4479 |
05 May 17 |
nicklas |
51 |
|
4479 |
05 May 17 |
nicklas |
52 |
} |