27
27
28
28
@ AutoValue
29
29
@ Immutable
30
- public abstract class Interval implements Comparable < Interval >, Serializable {
30
+ public abstract class Interval implements Serializable {
31
31
public static final long MONTHS_PER_YEAR = 12 ;
32
32
public static final long DAYS_PER_MONTH = 30 ;
33
33
public static final long HOURS_PER_DAY = 24 ;
@@ -53,48 +53,92 @@ public abstract class Interval implements Comparable<Interval>, Serializable {
53
53
Pattern .compile (
54
54
"^P(?!$)(-?\\ d+Y)?(-?\\ d+M)?(-?\\ d+D)?(T(?=-?\\ d)(-?\\ d+H)?(-?\\ d+M)?(-?\\ d+(\\ .\\ d{1,9})?S)?)?$" );
55
55
56
- public abstract long months ();
56
+ /** Returns the months component of the interval. */
57
+ public abstract int months ();
57
58
58
- public abstract long days ();
59
+ /** Returns the days component of the interval. */
60
+ public abstract int days ();
59
61
62
+ /** Returns the microseconds component of the interval. */
60
63
public abstract long micros ();
61
64
65
+ /** Returns the nanoFractions component of the interval. */
62
66
public abstract short nanoFractions ();
63
67
64
68
public static Builder builder () {
65
69
return new AutoValue_Interval .Builder ();
66
70
}
67
71
72
+ /** Returns the nanoseconds component of the interval. */
68
73
public BigInteger nanos () {
69
74
return BigInteger .valueOf (micros ())
70
75
.multiply (BigInteger .valueOf (NANOS_PER_MICRO ))
71
76
.add (BigInteger .valueOf (nanoFractions ()));
72
77
}
73
78
74
- /** Returns the total micros represented by the Interval . */
79
+ /** Returns the total microseconds represented by the interval . */
75
80
public long getAsMicros () {
76
81
return months () * MICROS_PER_MONTH + days () * MICROS_PER_DAY + micros ();
77
82
}
78
83
79
- /** Returns the total nanos represented by the Interval . */
84
+ /** Returns the total nanoseconds represented by the interval . */
80
85
public BigInteger getAsNanos () {
81
86
return BigInteger .valueOf (getAsMicros ())
82
87
.multiply (BigInteger .valueOf (NANOS_PER_MICRO ))
83
88
.add (BigInteger .valueOf (nanoFractions ()));
84
89
}
85
90
86
- /** Creates an Interval consisting of the given number of months. */
87
- public static Interval ofMonths (long months ) {
91
+ /** Creates an interval with specified number of months. */
92
+ public static Interval ofMonths (int months ) {
88
93
return builder ().setMonths (months ).setDays (0 ).setMicros (0 ).setNanoFractions ((short ) 0 ).build ();
89
94
}
90
95
91
- /** Creates an Interval consisting of the given number of days. */
92
- public static Interval ofDays (long days ) {
96
+ /** Creates an interval with specified number of days. */
97
+ public static Interval ofDays (int days ) {
93
98
return builder ().setMonths (0 ).setDays (days ).setMicros (0 ).setNanoFractions ((short ) 0 ).build ();
94
99
}
95
100
96
- /** Creates an Interval with specified months, days and micros. */
97
- public static Interval fromMonthsDaysMicros (long months , long days , long micros ) {
101
+ /** Creates an interval with specified number of seconds. */
102
+ public static Interval ofSeconds (long seconds ) {
103
+ return builder ()
104
+ .setMonths (0 )
105
+ .setDays (0 )
106
+ .setMicros (seconds * MICROS_PER_SECOND )
107
+ .setNanoFractions ((short ) 0 )
108
+ .build ();
109
+ }
110
+
111
+ /** Creates an interval with specified number of milliseconds. */
112
+ public static Interval ofMilliseconds (long milliseconds ) {
113
+ return builder ()
114
+ .setMonths (0 )
115
+ .setDays (0 )
116
+ .setMicros (milliseconds * MICROS_PER_MILLI )
117
+ .setNanoFractions ((short ) 0 )
118
+ .build ();
119
+ }
120
+
121
+ /** Creates an interval with specified number of microseconds. */
122
+ public static Interval ofMicros (long micros ) {
123
+ return builder ().months (0 ).days (0 ).micros (micros ).nanoFractions ((short ) 0 ).build ();
124
+ }
125
+
126
+ /** Creates an interval with specified number of nanoseconds. */
127
+ public static Interval ofNanos (@ NotNull BigInteger nanos ) {
128
+ BigInteger micros = nanos .divide (BigInteger .valueOf (NANOS_PER_MICRO ));
129
+ BigInteger nanoFractions = nanos .subtract (micros .multiply (BigInteger .valueOf (NANOS_PER_MICRO )));
130
+ long microsValue = micros .longValueExact ();
131
+ short nanoFractionsValue = nanoFractions .shortValueExact ();
132
+ return builder ()
133
+ .setMonths (0 )
134
+ .setDays (0 )
135
+ .setMicros (microsValue )
136
+ .setNanoFractions (nanoFractionsValue )
137
+ .build ();
138
+ }
139
+
140
+ /** Creates an interval with specified number of months, days and microseconds. */
141
+ public static Interval fromMonthsDaysMicros (int months , int days , long micros ) {
98
142
return builder ()
99
143
.setMonths (months )
100
144
.setDays (days )
@@ -103,13 +147,15 @@ public static Interval fromMonthsDaysMicros(long months, long days, long micros)
103
147
.build ();
104
148
}
105
149
106
- /** Creates an Interval with specified months, days and nanos . */
107
- public static Interval fromMonthsDaysNanos (long months , long days , BigInteger nanos ) {
108
- long micros = nanos .divide (BigInteger .valueOf (NANOS_PER_MICRO )). longValue ();
150
+ /** Creates an interval with specified number of months, days and nanoseconds . */
151
+ public static Interval fromMonthsDaysNanos (int months , int days , BigInteger nanos ) {
152
+ long micros = ( nanos .divide (BigInteger .valueOf (NANOS_PER_MICRO ))). longValueExact ();
109
153
short nanoFractions =
110
- nanos
111
- .subtract (BigInteger .valueOf (micros ).multiply (BigInteger .valueOf (NANOS_PER_MICRO )))
154
+ (nanos .subtract (BigInteger .valueOf (micros ).multiply (BigInteger .valueOf (NANOS_PER_MICRO ))))
112
155
.shortValue ();
156
+
157
+ System .out .println ("Micros: " + micros + " Nanos: " + nanoFractions );
158
+
113
159
return builder ()
114
160
.setMonths (months )
115
161
.setDays (days )
@@ -149,14 +195,14 @@ public static Interval parseFromString(String interval) {
149
195
totalNanos .subtract (totalMicros .multiply (BigInteger .valueOf (NANOS_PER_MICRO )));
150
196
151
197
return Interval .builder ()
152
- .setMonths (totalMonths )
153
- .setDays (days )
154
- .setMicros (totalMicros .longValue ())
155
- .setNanoFractions (nanoFractions .shortValue ())
198
+ .setMonths (Math . toIntExact ( totalMonths ) )
199
+ .setDays (Math . toIntExact ( days ) )
200
+ .setMicros (totalMicros .longValueExact ())
201
+ .setNanoFractions (nanoFractions .shortValueExact ())
156
202
.build ();
157
203
}
158
204
159
- /** @return the Interval in ISO801 duration format . */
205
+ /** Converts Interval to ISO8601 Duration Formatted String . */
160
206
public String ToISO8601 () {
161
207
StringBuilder result = new StringBuilder ();
162
208
result .append ("P" );
@@ -197,7 +243,7 @@ public String ToISO8601() {
197
243
BigDecimal seconds = new BigDecimal (nanos ).movePointLeft (9 );
198
244
199
245
if (seconds .compareTo (new BigDecimal (zero )) != 0 ) {
200
- result .append (String .format ("%sS" , seconds ));
246
+ result .append (String .format ("%sS" , seconds . stripTrailingZeros () ));
201
247
}
202
248
}
203
249
@@ -208,47 +254,7 @@ public String ToISO8601() {
208
254
return result .toString ();
209
255
}
210
256
211
- /** Creates an Interval consisting of the given number of seconds. */
212
- public static Interval ofSeconds (long seconds ) {
213
- return builder ()
214
- .setMonths (0 )
215
- .setDays (0 )
216
- .setMicros (seconds * MICROS_PER_SECOND )
217
- .setNanoFractions ((short ) 0 )
218
- .build ();
219
- }
220
-
221
- /** Creates an Interval consisting of the given number of milliseconds. */
222
- public static Interval ofMilliseconds (long milliseconds ) {
223
- return builder ()
224
- .setMonths (0 )
225
- .setDays (0 )
226
- .setMicros (milliseconds * MICROS_PER_MILLI )
227
- .setNanoFractions ((short ) 0 )
228
- .build ();
229
- }
230
-
231
- /** Creates an Interval consisting of the given number of microseconds. */
232
- public static Interval ofMicros (long micros ) {
233
- return builder ().months (0 ).days (0 ).micros (micros ).nanoFractions ((short ) 0 ).build ();
234
- }
235
-
236
- /** Creates an Interval consisting of the given number of nanoseconds. */
237
- public static Interval ofNanos (@ NotNull BigInteger nanos ) {
238
- BigInteger micros = nanos .divide (BigInteger .valueOf (NANOS_PER_MICRO ));
239
- BigInteger nanoFractions = nanos .subtract (micros .multiply (BigInteger .valueOf (NANOS_PER_MICRO )));
240
-
241
- long microsValue = micros .longValue ();
242
- long nanoFractionsValue = nanoFractions .longValue ();
243
-
244
- return builder ()
245
- .setMonths (0 )
246
- .setDays (0 )
247
- .setMicros (microsValue )
248
- .setNanoFractions ((short ) nanoFractionsValue )
249
- .build ();
250
- }
251
-
257
+ /** Creates an interval which representing 0-duration. */
252
258
public static Interval zeroInterval () {
253
259
return builder ().setMonths (0 ).setDays (0 ).setMicros (0 ).setNanoFractions ((short ) 0 ).build ();
254
260
}
@@ -265,14 +271,6 @@ && days() == anotherInterval.days()
265
271
&& nanos ().equals (anotherInterval .nanos ());
266
272
}
267
273
268
- @ Override
269
- public int compareTo (@ NotNull Interval anotherInterval ) {
270
- if (equals (anotherInterval )) {
271
- return 0 ;
272
- }
273
- return getAsNanos ().compareTo (anotherInterval .getAsNanos ());
274
- }
275
-
276
274
@ Override
277
275
public int hashCode () {
278
276
int result = 17 ;
@@ -284,19 +282,19 @@ public int hashCode() {
284
282
285
283
@ AutoValue .Builder
286
284
public abstract static class Builder {
287
- abstract Builder months (long months );
285
+ abstract Builder months (int months );
288
286
289
- abstract Builder days (long days );
287
+ abstract Builder days (int days );
290
288
291
289
abstract Builder micros (long micros );
292
290
293
291
abstract Builder nanoFractions (short nanoFractions );
294
292
295
- public Builder setMonths (long months ) {
293
+ public Builder setMonths (int months ) {
296
294
return months (months );
297
295
}
298
296
299
- public Builder setDays (long days ) {
297
+ public Builder setDays (int days ) {
300
298
return days (days );
301
299
}
302
300
0 commit comments