2727
2828@ AutoValue
2929@ Immutable
30- public abstract class Interval implements Comparable < Interval >, Serializable {
30+ public abstract class Interval implements Serializable {
3131 public static final long MONTHS_PER_YEAR = 12 ;
3232 public static final long DAYS_PER_MONTH = 30 ;
3333 public static final long HOURS_PER_DAY = 24 ;
@@ -53,48 +53,92 @@ public abstract class Interval implements Comparable<Interval>, Serializable {
5353 Pattern .compile (
5454 "^P(?!$)(-?\\ d+Y)?(-?\\ d+M)?(-?\\ d+D)?(T(?=-?\\ d)(-?\\ d+H)?(-?\\ d+M)?(-?\\ d+(\\ .\\ d{1,9})?S)?)?$" );
5555
56- public abstract long months ();
56+ /** Returns the months component of the interval. */
57+ public abstract int months ();
5758
58- public abstract long days ();
59+ /** Returns the days component of the interval. */
60+ public abstract int days ();
5961
62+ /** Returns the microseconds component of the interval. */
6063 public abstract long micros ();
6164
65+ /** Returns the nanoFractions component of the interval. */
6266 public abstract short nanoFractions ();
6367
6468 public static Builder builder () {
6569 return new AutoValue_Interval .Builder ();
6670 }
6771
72+ /** Returns the nanoseconds component of the interval. */
6873 public BigInteger nanos () {
6974 return BigInteger .valueOf (micros ())
7075 .multiply (BigInteger .valueOf (NANOS_PER_MICRO ))
7176 .add (BigInteger .valueOf (nanoFractions ()));
7277 }
7378
74- /** Returns the total micros represented by the Interval . */
79+ /** Returns the total microseconds represented by the interval . */
7580 public long getAsMicros () {
7681 return months () * MICROS_PER_MONTH + days () * MICROS_PER_DAY + micros ();
7782 }
7883
79- /** Returns the total nanos represented by the Interval . */
84+ /** Returns the total nanoseconds represented by the interval . */
8085 public BigInteger getAsNanos () {
8186 return BigInteger .valueOf (getAsMicros ())
8287 .multiply (BigInteger .valueOf (NANOS_PER_MICRO ))
8388 .add (BigInteger .valueOf (nanoFractions ()));
8489 }
8590
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 ) {
8893 return builder ().setMonths (months ).setDays (0 ).setMicros (0 ).setNanoFractions ((short ) 0 ).build ();
8994 }
9095
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 ) {
9398 return builder ().setMonths (0 ).setDays (days ).setMicros (0 ).setNanoFractions ((short ) 0 ).build ();
9499 }
95100
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 ) {
98142 return builder ()
99143 .setMonths (months )
100144 .setDays (days )
@@ -103,13 +147,15 @@ public static Interval fromMonthsDaysMicros(long months, long days, long micros)
103147 .build ();
104148 }
105149
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 ();
109153 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 ))))
112155 .shortValue ();
156+
157+ System .out .println ("Micros: " + micros + " Nanos: " + nanoFractions );
158+
113159 return builder ()
114160 .setMonths (months )
115161 .setDays (days )
@@ -149,14 +195,14 @@ public static Interval parseFromString(String interval) {
149195 totalNanos .subtract (totalMicros .multiply (BigInteger .valueOf (NANOS_PER_MICRO )));
150196
151197 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 ())
156202 .build ();
157203 }
158204
159- /** @return the Interval in ISO801 duration format . */
205+ /** Converts Interval to ISO8601 Duration Formatted String . */
160206 public String ToISO8601 () {
161207 StringBuilder result = new StringBuilder ();
162208 result .append ("P" );
@@ -197,7 +243,7 @@ public String ToISO8601() {
197243 BigDecimal seconds = new BigDecimal (nanos ).movePointLeft (9 );
198244
199245 if (seconds .compareTo (new BigDecimal (zero )) != 0 ) {
200- result .append (String .format ("%sS" , seconds ));
246+ result .append (String .format ("%sS" , seconds . stripTrailingZeros () ));
201247 }
202248 }
203249
@@ -208,47 +254,7 @@ public String ToISO8601() {
208254 return result .toString ();
209255 }
210256
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. */
252258 public static Interval zeroInterval () {
253259 return builder ().setMonths (0 ).setDays (0 ).setMicros (0 ).setNanoFractions ((short ) 0 ).build ();
254260 }
@@ -265,14 +271,6 @@ && days() == anotherInterval.days()
265271 && nanos ().equals (anotherInterval .nanos ());
266272 }
267273
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-
276274 @ Override
277275 public int hashCode () {
278276 int result = 17 ;
@@ -284,19 +282,19 @@ public int hashCode() {
284282
285283 @ AutoValue .Builder
286284 public abstract static class Builder {
287- abstract Builder months (long months );
285+ abstract Builder months (int months );
288286
289- abstract Builder days (long days );
287+ abstract Builder days (int days );
290288
291289 abstract Builder micros (long micros );
292290
293291 abstract Builder nanoFractions (short nanoFractions );
294292
295- public Builder setMonths (long months ) {
293+ public Builder setMonths (int months ) {
296294 return months (months );
297295 }
298296
299- public Builder setDays (long days ) {
297+ public Builder setDays (int days ) {
300298 return days (days );
301299 }
302300
0 commit comments