1 package net.avcompris.commons3.core;
2
3 import static com.google.common.base.Preconditions.checkArgument;
4 import static com.google.common.base.Preconditions.checkNotNull;
5 import static org.joda.time.DateTimeZone.UTC;
6
7 import javax.annotation.Nullable;
8
9 import org.apache.commons.lang3.NotImplementedException;
10 import org.joda.time.DateTime;
11 import org.joda.time.format.DateTimeFormatter;
12 import org.joda.time.format.ISODateTimeFormat;
13
14 import net.avcompris.commons3.types.DateTimeHolder;
15
16 public final class DateTimeHolderImpl implements DateTimeHolder {
17
18 private static final long serialVersionUID = 1160765887175893130L;
19
20 private static final DateTimeFormatter ISO_FORMATTER = ISODateTimeFormat.dateTime();
21
22 private final DateTime dateTime;
23 private final String text;
24 private final long ms;
25
26 private DateTimeHolderImpl(final DateTime dateTime) {
27
28 this.dateTime = checkNotNull(dateTime, "dateTime");
29
30 text = dateTime.withZone(UTC).toString(ISO_FORMATTER);
31
32 ms = dateTime.getMillis();
33 }
34
35 @Nullable
36 public static DateTimeHolder toDateTimeHolderOrNull(@Nullable final DateTime dateTime) {
37
38 if (dateTime == null) {
39 return null;
40 }
41
42 return toDateTimeHolder(dateTime);
43 }
44
45 @Nullable
46 public static DateTimeHolder toDateTimeHolder(final DateTime dateTime) {
47
48 checkNotNull(dateTime, "dateTime");
49
50 return new DateTimeHolderImpl(dateTime);
51 }
52
53 @Nullable
54 public static DateTimeHolder toDateTimeHolder(final long timestamp) {
55
56 return new DateTimeHolderImpl(new DateTime(timestamp).withZone(UTC));
57 }
58
59 @Override
60 public String getDateTime() {
61 return text;
62 }
63
64 @Override
65 public long getTimestamp() {
66 return ms;
67 }
68
69 @Override
70 public String toString() {
71
72 return "{dateTime: \"" + text + "\", timestamp: " + ms + "}";
73 }
74
75 @Override
76 public int hashCode() {
77
78 return dateTime.hashCode();
79 }
80
81 @Override
82 public boolean equals(@Nullable final Object o) {
83
84 if (o == null || !(o instanceof DateTimeHolder)) {
85 return false;
86 }
87
88 return toString().contentEquals(o.toString());
89 }
90
91 public static DateTime toDateTimeOrNull(@Nullable final DateTimeHolder holder) {
92
93 if (holder == null) {
94 return null;
95 }
96
97 return toDateTime(holder);
98 }
99
100 public static DateTime toDateTime(final DateTimeHolder holder) {
101
102 checkNotNull(holder, "holder");
103
104 checkArgument(holder instanceof DateTimeHolderImpl,
105 "holder should be of type %s, but was: %s",
106 DateTimeHolderImpl.class.getName(), holder.getClass().getName());
107
108 return ((DateTimeHolderImpl) holder).dateTime;
109 }
110
111 @Override
112 public int compareTo(@Nullable final DateTimeHolder d) {
113
114 if (d == null) {
115
116 return dateTime.compareTo(null);
117
118 } else if (d instanceof DateTimeHolderImpl) {
119
120 return dateTime.compareTo(((DateTimeHolderImpl) d).dateTime);
121
122 } else {
123
124 throw new NotImplementedException("d.class: " + d.getClass().getName());
125 }
126 }
127
128 @Override
129 public Object clone() {
130
131 return toDateTimeHolder(dateTime);
132 }
133 }