3636public class Interval implements Serializable {
3737 private final int months ;
3838 private final int days ;
39- private final BigInteger nanoseconds ;
40-
41- public static final long MONTHS_PER_YEAR = 12 ;
42- public static final long MINUTES_PER_HOUR = 60 ;
43- public static final long SECONDS_PER_MINUTE = 60 ;
44- public static final long SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE ;
45- public static final long MILLIS_PER_SECOND = 1000 ;
46- public static final long MICROS_PER_MILLISECOND = 1000 ;
47- public static final long MICROS_PER_SECOND = MICROS_PER_MILLISECOND * MILLIS_PER_SECOND ;
48- public static final long MICROS_PER_MINUTE = SECONDS_PER_MINUTE * MICROS_PER_SECOND ;
49- public static final long MICROS_PER_HOUR = SECONDS_PER_HOUR * MICROS_PER_SECOND ;
50- public static final long NANOS_PER_MICROSECOND = 1000 ;
51- public static final BigInteger NANOS_PER_MILLISECOND =
52- BigInteger .valueOf (MICROS_PER_MILLISECOND * NANOS_PER_MICROSECOND );
53- public static final BigInteger NANOS_PER_SECOND =
54- BigInteger .valueOf (MICROS_PER_SECOND * NANOS_PER_MICROSECOND );
55- public static final BigInteger NANOS_PER_MINUTE =
56- BigInteger .valueOf (MICROS_PER_MINUTE * NANOS_PER_MICROSECOND );
57- public static final BigInteger NANOS_PER_HOUR =
58- BigInteger .valueOf (MICROS_PER_HOUR * NANOS_PER_MICROSECOND );
59- public static final Interval ZERO = Interval .builder ().build ();
39+ private final BigInteger nanos ;
40+
41+ private static final long MONTHS_PER_YEAR = 12 ;
42+ private static final long MINUTES_PER_HOUR = 60 ;
43+ private static final long SECONDS_PER_MINUTE = 60 ;
44+ private static final long SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE ;
45+ private static final long MILLIS_PER_SECOND = 1000 ;
46+ private static final long MICROS_PER_MILLI = 1000 ;
47+ private static final long NANOS_PER_MICRO = 1000 ;
48+ private static final long MICROS_PER_SECOND = MICROS_PER_MILLI * MILLIS_PER_SECOND ;
49+ private static final long MICROS_PER_MINUTE = SECONDS_PER_MINUTE * MICROS_PER_SECOND ;
50+ private static final long MICROS_PER_HOUR = SECONDS_PER_HOUR * MICROS_PER_SECOND ;
51+ private static final BigInteger NANOS_PER_MILLI =
52+ BigInteger .valueOf (MICROS_PER_MILLI * NANOS_PER_MICRO );
53+ private static final BigInteger NANOS_PER_SECOND =
54+ BigInteger .valueOf (MICROS_PER_SECOND * NANOS_PER_MICRO );
55+ private static final BigInteger NANOS_PER_MINUTE =
56+ BigInteger .valueOf (MICROS_PER_MINUTE * NANOS_PER_MICRO );
57+ private static final BigInteger NANOS_PER_HOUR =
58+ BigInteger .valueOf (MICROS_PER_HOUR * NANOS_PER_MICRO );
59+ private static final Interval ZERO = Interval .builder ().build ();
6060
6161 /** Regex to parse ISO8601 interval format- `P[n]Y[n]M[n]DT[n]H[n]M[n([.,][fraction])]S` */
6262 private static final Pattern INTERVAL_PATTERN =
6363 Pattern .compile (
6464 "^P(?!$)(-?\\ d+Y)?(-?\\ d+M)?(-?\\ d+D)?(T(?=-?[.,]?\\ d)(-?\\ d+H)?(-?\\ d+M)?(-?((\\ d+([.,]\\ d{1,9})?)|([.,]\\ d{1,9}))S)?)?$" );
6565
66- private Interval (int months , int days , BigInteger nanoseconds ) {
66+ private Interval (int months , int days , BigInteger nanos ) {
6767 this .months = months ;
6868 this .days = days ;
69- this .nanoseconds = nanoseconds ;
69+ this .nanos = nanos ;
7070 }
7171
7272 /** Returns the months component of the interval. */
@@ -80,8 +80,8 @@ public int getDays() {
8080 }
8181
8282 /** Returns the nanoseconds component of the interval. */
83- public BigInteger getNanoseconds () {
84- return nanoseconds ;
83+ public BigInteger getNanos () {
84+ return nanos ;
8585 }
8686
8787 public static Builder builder () {
@@ -100,39 +100,37 @@ public static Interval ofDays(int days) {
100100
101101 /** Creates an interval with specified number of seconds. */
102102 public static Interval ofSeconds (long seconds ) {
103- return builder ().setNanoseconds (BigInteger .valueOf (seconds ).multiply (NANOS_PER_SECOND )).build ();
103+ return builder ().setNanos (BigInteger .valueOf (seconds ).multiply (NANOS_PER_SECOND )).build ();
104104 }
105105
106106 /** Creates an interval with specified number of milliseconds. */
107- public static Interval ofMilliseconds (long milliseconds ) {
108- return builder ()
109- .setNanoseconds (BigInteger .valueOf (milliseconds ).multiply (NANOS_PER_MILLISECOND ))
110- .build ();
107+ public static Interval ofMillis (long millis ) {
108+ return builder ().setNanos (BigInteger .valueOf (millis ).multiply (NANOS_PER_MILLI )).build ();
111109 }
112110
113111 /** Creates an interval with specified number of microseconds. */
114- public static Interval ofMicroseconds (long micros ) {
112+ public static Interval ofMicros (long micros ) {
115113 return builder ()
116- .setNanoseconds (
117- BigInteger .valueOf (micros ).multiply (BigInteger .valueOf (NANOS_PER_MICROSECOND )))
114+ .setNanos (BigInteger .valueOf (micros ).multiply (BigInteger .valueOf (NANOS_PER_MICRO )))
118115 .build ();
119116 }
120117
121118 /** Creates an interval with specified number of nanoseconds. */
122- public static Interval ofNanoseconds (@ NotNull BigInteger nanos ) {
123- return builder ().setNanoseconds (nanos ).build ();
119+ public static Interval ofNanos (@ NotNull BigInteger nanos ) {
120+ return builder ().setNanos (nanos ).build ();
124121 }
125122
126123 /** Creates an interval with specified number of months, days and nanoseconds. */
127- public static Interval fromMonthsDaysNanos (int months , int days , BigInteger nanoseconds ) {
128- return builder ().setMonths (months ).setDays (days ).setNanoseconds ( nanoseconds ).build ();
124+ public static Interval fromMonthsDaysNanos (int months , int days , BigInteger nanos ) {
125+ return builder ().setMonths (months ).setDays (days ).setNanos ( nanos ).build ();
129126 }
130127
131128 private static String getNullOrDefault (Matcher matcher , int groupIdx ) {
132129 String value = matcher .group (groupIdx );
133130 return value == null ? "0" : value ;
134131 }
135132
133+ /* Parses ISO8601 duration format string to Interval. */
136134 public static Interval parseFromString (String interval ) {
137135 Matcher matcher = INTERVAL_PATTERN .matcher (interval );
138136 if (!matcher .matches ()) {
@@ -158,11 +156,11 @@ public static Interval parseFromString(String interval) {
158156 return Interval .builder ()
159157 .setMonths (Math .toIntExact (totalMonths ))
160158 .setDays (Math .toIntExact (days ))
161- .setNanoseconds (totalNanos )
159+ .setNanos (totalNanos )
162160 .build ();
163161 }
164162
165- /** Converts Interval to ISO8601 Duration Formatted String . */
163+ /** Converts Interval to ISO8601 duration format string . */
166164 public String toISO8601 () {
167165 if (this .equals (ZERO )) {
168166 return "P0Y" ;
@@ -187,7 +185,7 @@ public String toISO8601() {
187185 result .append (String .format ("%dD" , this .getDays ()));
188186 }
189187
190- BigInteger nanos = this .getNanoseconds ();
188+ BigInteger nanos = this .getNanos ();
191189 BigInteger zero = BigInteger .valueOf (0 );
192190 if (nanos .compareTo (zero ) != 0 ) {
193191 result .append ("T" );
@@ -238,22 +236,22 @@ public boolean equals(Object rhs) {
238236 Interval anotherInterval = (Interval ) rhs ;
239237 return getMonths () == anotherInterval .getMonths ()
240238 && getDays () == anotherInterval .getDays ()
241- && getNanoseconds ().equals (anotherInterval .getNanoseconds ());
239+ && getNanos ().equals (anotherInterval .getNanos ());
242240 }
243241
244242 @ Override
245243 public int hashCode () {
246244 int result = 17 ;
247245 result = 31 * result + Integer .valueOf (getMonths ()).hashCode ();
248246 result = 31 * result + Integer .valueOf (getDays ()).hashCode ();
249- result = 31 * result + getNanoseconds ().hashCode ();
247+ result = 31 * result + getNanos ().hashCode ();
250248 return result ;
251249 }
252250
253251 public static class Builder {
254252 private int months = 0 ;
255253 private int days = 0 ;
256- private BigInteger nanoseconds = BigInteger .ZERO ;
254+ private BigInteger nanos = BigInteger .ZERO ;
257255
258256 Builder setMonths (int months ) {
259257 this .months = months ;
@@ -265,13 +263,13 @@ Builder setDays(int days) {
265263 return this ;
266264 }
267265
268- Builder setNanoseconds (BigInteger nanoseconds ) {
269- this .nanoseconds = nanoseconds ;
266+ Builder setNanos (BigInteger nanos ) {
267+ this .nanos = nanos ;
270268 return this ;
271269 }
272270
273271 public Interval build () {
274- return new Interval (months , days , nanoseconds );
272+ return new Interval (months , days , nanos );
275273 }
276274 }
277275}
0 commit comments