• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf.util;
32 
33 import static com.google.common.base.Preconditions.checkArgument;
34 import static com.google.common.math.IntMath.checkedAdd;
35 import static com.google.common.math.IntMath.checkedSubtract;
36 import static com.google.common.math.LongMath.checkedAdd;
37 import static com.google.common.math.LongMath.checkedMultiply;
38 import static com.google.common.math.LongMath.checkedSubtract;
39 import static com.google.protobuf.util.Timestamps.MICROS_PER_SECOND;
40 import static com.google.protobuf.util.Timestamps.MILLIS_PER_SECOND;
41 import static com.google.protobuf.util.Timestamps.NANOS_PER_MICROSECOND;
42 import static com.google.protobuf.util.Timestamps.NANOS_PER_MILLISECOND;
43 import static com.google.protobuf.util.Timestamps.NANOS_PER_SECOND;
44 
45 import com.google.errorprone.annotations.CanIgnoreReturnValue;
46 import com.google.protobuf.Duration;
47 import java.text.ParseException;
48 import java.util.Comparator;
49 
50 /**
51  * Utilities to help create/manipulate {@code protobuf/duration.proto}. All operations throw an
52  * {@link IllegalArgumentException} if the input(s) are not {@linkplain #isValid(Duration) valid}.
53  */
54 public final class Durations {
55   static final long DURATION_SECONDS_MIN = -315576000000L;
56   static final long DURATION_SECONDS_MAX = 315576000000L;
57 
58   private static final long SECONDS_PER_MINUTE = 60L;
59   private static final long SECONDS_PER_HOUR = SECONDS_PER_MINUTE * 60;
60   private static final long SECONDS_PER_DAY = SECONDS_PER_HOUR * 24;
61 
62   /** A constant holding the minimum valid {@link Duration}, approximately {@code -10,000} years. */
63   public static final Duration MIN_VALUE =
64       Duration.newBuilder().setSeconds(DURATION_SECONDS_MIN).setNanos(-999999999).build();
65 
66   /** A constant holding the maximum valid {@link Duration}, approximately {@code +10,000} years. */
67   public static final Duration MAX_VALUE =
68       Duration.newBuilder().setSeconds(DURATION_SECONDS_MAX).setNanos(999999999).build();
69 
70   /** A constant holding the duration of zero. */
71   public static final Duration ZERO = Duration.newBuilder().setSeconds(0L).setNanos(0).build();
72 
Durations()73   private Durations() {}
74 
75   private static final Comparator<Duration> COMPARATOR =
76       new Comparator<Duration>() {
77         @Override
78         public int compare(Duration d1, Duration d2) {
79           checkValid(d1);
80           checkValid(d2);
81           int secDiff = Long.compare(d1.getSeconds(), d2.getSeconds());
82           return (secDiff != 0) ? secDiff : Integer.compare(d1.getNanos(), d2.getNanos());
83         }
84       };
85 
86   /**
87    * Returns a {@link Comparator} for {@link Duration}s which sorts in increasing chronological
88    * order. Nulls and invalid {@link Duration}s are not allowed (see {@link #isValid}).
89    */
comparator()90   public static Comparator<Duration> comparator() {
91     return COMPARATOR;
92   }
93 
94   /**
95    * Compares two durations. The value returned is identical to what would be returned by: {@code
96    * Durations.comparator().compare(x, y)}.
97    *
98    * @return the value {@code 0} if {@code x == y}; a value less than {@code 0} if {@code x < y};
99    *     and a value greater than {@code 0} if {@code x > y}
100    */
compare(Duration x, Duration y)101   public static int compare(Duration x, Duration y) {
102     return COMPARATOR.compare(x, y);
103   }
104 
105   /**
106    * Returns true if the given {@link Duration} is valid. The {@code seconds} value must be in the
107    * range [-315,576,000,000, +315,576,000,000]. The {@code nanos} value must be in the range
108    * [-999,999,999, +999,999,999].
109    *
110    * <p><b>Note:</b> Durations less than one second are represented with a 0 {@code seconds} field
111    * and a positive or negative {@code nanos} field. For durations of one second or more, a non-zero
112    * value for the {@code nanos} field must be of the same sign as the {@code seconds} field.
113    */
isValid(Duration duration)114   public static boolean isValid(Duration duration) {
115     return isValid(duration.getSeconds(), duration.getNanos());
116   }
117 
118   /**
119    * Returns true if the given number of seconds and nanos is a valid {@link Duration}. The {@code
120    * seconds} value must be in the range [-315,576,000,000, +315,576,000,000]. The {@code nanos}
121    * value must be in the range [-999,999,999, +999,999,999].
122    *
123    * <p><b>Note:</b> Durations less than one second are represented with a 0 {@code seconds} field
124    * and a positive or negative {@code nanos} field. For durations of one second or more, a non-zero
125    * value for the {@code nanos} field must be of the same sign as the {@code seconds} field.
126    */
127   @SuppressWarnings("GoodTime") // this is a legacy conversion API
isValid(long seconds, int nanos)128   public static boolean isValid(long seconds, int nanos) {
129     if (seconds < DURATION_SECONDS_MIN || seconds > DURATION_SECONDS_MAX) {
130       return false;
131     }
132     if (nanos < -999999999L || nanos >= NANOS_PER_SECOND) {
133       return false;
134     }
135     if (seconds < 0 || nanos < 0) {
136       if (seconds > 0 || nanos > 0) {
137         return false;
138       }
139     }
140     return true;
141   }
142 
143   /** Returns whether the given {@link Duration} is negative or not. */
isNegative(Duration duration)144   public static boolean isNegative(Duration duration) {
145     checkValid(duration);
146     return (duration.getSeconds() == 0) ? duration.getNanos() < 0 : duration.getSeconds() < 0;
147   }
148 
149   /**
150    * Ensures that the given {@link Duration} is not negative.
151    *
152    * @throws IllegalArgumentException if {@code duration} is negative or invalid
153    * @throws NullPointerException if {@code duration} is {@code null}
154    */
155   @CanIgnoreReturnValue
checkNotNegative(Duration duration)156   public static Duration checkNotNegative(Duration duration) {
157     checkValid(duration);
158     checkArgument(!isNegative(duration), "duration (%s) must not be negative", toString(duration));
159     return duration;
160   }
161 
162   /**
163    * Ensures that the given {@link Duration} is positive.
164    *
165    * @throws IllegalArgumentException if {@code duration} is negative, {@code ZERO}, or invalid
166    * @throws NullPointerException if {@code duration} is {@code null}
167    */
168   @CanIgnoreReturnValue
checkPositive(Duration duration)169   public static Duration checkPositive(Duration duration) {
170     checkValid(duration);
171     checkArgument(
172         !isNegative(duration) && !duration.equals(ZERO),
173         "duration (%s) must be positive",
174         toString(duration));
175     return duration;
176   }
177 
178   /** Throws an {@link IllegalArgumentException} if the given {@link Duration} is not valid. */
179   @CanIgnoreReturnValue
checkValid(Duration duration)180   public static Duration checkValid(Duration duration) {
181     long seconds = duration.getSeconds();
182     int nanos = duration.getNanos();
183     if (!isValid(seconds, nanos)) {
184       throw new IllegalArgumentException(
185           String.format(
186               "Duration is not valid. See proto definition for valid values. "
187                   + "Seconds (%s) must be in range [-315,576,000,000, +315,576,000,000]. "
188                   + "Nanos (%s) must be in range [-999,999,999, +999,999,999]. "
189                   + "Nanos must have the same sign as seconds",
190               seconds, nanos));
191     }
192     return duration;
193   }
194 
195   /**
196    * Builds the given builder and throws an {@link IllegalArgumentException} if it is not valid. See
197    * {@link #checkValid(Duration)}.
198    *
199    * @return A valid, built {@link Duration}.
200    */
checkValid(Duration.Builder durationBuilder)201   public static Duration checkValid(Duration.Builder durationBuilder) {
202     return checkValid(durationBuilder.build());
203   }
204 
205   /**
206    * Convert Duration to string format. The string format will contains 3, 6, or 9 fractional digits
207    * depending on the precision required to represent the exact Duration value. For example: "1s",
208    * "1.010s", "1.000000100s", "-3.100s" The range that can be represented by Duration is from
209    * -315,576,000,000 to +315,576,000,000 inclusive (in seconds).
210    *
211    * @return The string representation of the given duration.
212    * @throws IllegalArgumentException if the given duration is not in the valid range.
213    */
toString(Duration duration)214   public static String toString(Duration duration) {
215     checkValid(duration);
216 
217     long seconds = duration.getSeconds();
218     int nanos = duration.getNanos();
219 
220     StringBuilder result = new StringBuilder();
221     if (seconds < 0 || nanos < 0) {
222       result.append("-");
223       seconds = -seconds;
224       nanos = -nanos;
225     }
226     result.append(seconds);
227     if (nanos != 0) {
228       result.append(".");
229       result.append(Timestamps.formatNanos(nanos));
230     }
231     result.append("s");
232     return result.toString();
233   }
234 
235   /**
236    * Parse from a string to produce a duration.
237    *
238    * @return A Duration parsed from the string.
239    * @throws ParseException if parsing fails.
240    */
parse(String value)241   public static Duration parse(String value) throws ParseException {
242     // Must ended with "s".
243     if (value.isEmpty() || value.charAt(value.length() - 1) != 's') {
244       throw new ParseException("Invalid duration string: " + value, 0);
245     }
246     boolean negative = false;
247     if (value.charAt(0) == '-') {
248       negative = true;
249       value = value.substring(1);
250     }
251     String secondValue = value.substring(0, value.length() - 1);
252     String nanoValue = "";
253     int pointPosition = secondValue.indexOf('.');
254     if (pointPosition != -1) {
255       nanoValue = secondValue.substring(pointPosition + 1);
256       secondValue = secondValue.substring(0, pointPosition);
257     }
258     long seconds = Long.parseLong(secondValue);
259     int nanos = nanoValue.isEmpty() ? 0 : Timestamps.parseNanos(nanoValue);
260     if (seconds < 0) {
261       throw new ParseException("Invalid duration string: " + value, 0);
262     }
263     if (negative) {
264       seconds = -seconds;
265       nanos = -nanos;
266     }
267     try {
268       return normalizedDuration(seconds, nanos);
269     } catch (IllegalArgumentException e) {
270       throw new ParseException("Duration value is out of range.", 0);
271     }
272   }
273 
274   // Static factories
275 
276   /** Create a Duration from the number of days. */
277   @SuppressWarnings("GoodTime") // this is a legacy conversion API
fromDays(long days)278   public static Duration fromDays(long days) {
279     return Duration.newBuilder()
280         .setSeconds(checkedMultiply(days, SECONDS_PER_DAY))
281         .setNanos(0)
282         .build();
283   }
284 
285   /** Create a Duration from the number of hours. */
286   @SuppressWarnings("GoodTime") // this is a legacy conversion API
fromHours(long hours)287   public static Duration fromHours(long hours) {
288     return Duration.newBuilder()
289         .setSeconds(checkedMultiply(hours, SECONDS_PER_HOUR))
290         .setNanos(0)
291         .build();
292   }
293 
294   /** Create a Duration from the number of minutes. */
295   @SuppressWarnings("GoodTime") // this is a legacy conversion API
fromMinutes(long minutes)296   public static Duration fromMinutes(long minutes) {
297     return Duration.newBuilder()
298         .setSeconds(checkedMultiply(minutes, SECONDS_PER_MINUTE))
299         .setNanos(0)
300         .build();
301   }
302 
303   /** Create a Duration from the number of seconds. */
304   @SuppressWarnings("GoodTime") // this is a legacy conversion API
fromSeconds(long seconds)305   public static Duration fromSeconds(long seconds) {
306     return normalizedDuration(seconds, 0);
307   }
308 
309   /** Create a Duration from the number of milliseconds. */
310   @SuppressWarnings("GoodTime") // this is a legacy conversion API
fromMillis(long milliseconds)311   public static Duration fromMillis(long milliseconds) {
312     return normalizedDuration(
313         milliseconds / MILLIS_PER_SECOND,
314         (int) (milliseconds % MILLIS_PER_SECOND * NANOS_PER_MILLISECOND));
315   }
316 
317   /** Create a Duration from the number of microseconds. */
318   @SuppressWarnings("GoodTime") // this is a legacy conversion API
fromMicros(long microseconds)319   public static Duration fromMicros(long microseconds) {
320     return normalizedDuration(
321         microseconds / MICROS_PER_SECOND,
322         (int) (microseconds % MICROS_PER_SECOND * NANOS_PER_MICROSECOND));
323   }
324 
325   /** Create a Duration from the number of nanoseconds. */
326   @SuppressWarnings("GoodTime") // this is a legacy conversion API
fromNanos(long nanoseconds)327   public static Duration fromNanos(long nanoseconds) {
328     return normalizedDuration(
329         nanoseconds / NANOS_PER_SECOND, (int) (nanoseconds % NANOS_PER_SECOND));
330   }
331 
332   // Conversion APIs
333 
334   /**
335    * Convert a Duration to the number of days. The result will be rounded towards 0 to the nearest
336    * day.
337    */
338   @SuppressWarnings("GoodTime") // this is a legacy conversion API
toDays(Duration duration)339   public static long toDays(Duration duration) {
340     return checkValid(duration).getSeconds() / SECONDS_PER_DAY;
341   }
342 
343   /**
344    * Convert a Duration to the number of hours. The result will be rounded towards 0 to the nearest
345    * hour.
346    */
347   @SuppressWarnings("GoodTime") // this is a legacy conversion API
toHours(Duration duration)348   public static long toHours(Duration duration) {
349     return checkValid(duration).getSeconds() / SECONDS_PER_HOUR;
350   }
351 
352   /**
353    * Convert a Duration to the number of minutes. The result will be rounded towards 0 to the
354    * nearest minute.
355    */
356   @SuppressWarnings("GoodTime") // this is a legacy conversion API
toMinutes(Duration duration)357   public static long toMinutes(Duration duration) {
358     return checkValid(duration).getSeconds() / SECONDS_PER_MINUTE;
359   }
360 
361   /**
362    * Convert a Duration to the number of seconds. The result will be rounded towards 0 to the
363    * nearest second. E.g., if the duration represents -1 nanosecond, it will be rounded to 0.
364    */
365   @SuppressWarnings("GoodTime") // this is a legacy conversion API
toSeconds(Duration duration)366   public static long toSeconds(Duration duration) {
367     return checkValid(duration).getSeconds();
368   }
369 
370   /**
371    * Returns the number of seconds of the given duration as a {@code double}. This method should be
372    * used to accommodate APIs that <b>only</b> accept durations as {@code double} values.
373    *
374    * <p>This conversion may lose precision.
375    *
376    * <p>If you need the number of seconds in this duration as a {@code long} (not a {@code double}),
377    * simply use {@code duration.getSeconds()} or {@link #toSeconds} (which includes validation).
378    */
379   @SuppressWarnings({
380     "DurationSecondsToDouble", // that's the whole point of this method
381     "GoodTime" // this is a legacy conversion API
382   })
toSecondsAsDouble(Duration duration)383   public static double toSecondsAsDouble(Duration duration) {
384     checkValid(duration);
385     return duration.getSeconds() + duration.getNanos() / 1e9;
386   }
387 
388   /**
389    * Convert a Duration to the number of milliseconds. The result will be rounded towards 0 to the
390    * nearest millisecond. E.g., if the duration represents -1 nanosecond, it will be rounded to 0.
391    */
392   @SuppressWarnings("GoodTime") // this is a legacy conversion API
toMillis(Duration duration)393   public static long toMillis(Duration duration) {
394     checkValid(duration);
395     return checkedAdd(
396         checkedMultiply(duration.getSeconds(), MILLIS_PER_SECOND),
397         duration.getNanos() / NANOS_PER_MILLISECOND);
398   }
399 
400   /**
401    * Convert a Duration to the number of microseconds. The result will be rounded towards 0 to the
402    * nearest microseconds. E.g., if the duration represents -1 nanosecond, it will be rounded to 0.
403    */
404   @SuppressWarnings("GoodTime") // this is a legacy conversion API
toMicros(Duration duration)405   public static long toMicros(Duration duration) {
406     checkValid(duration);
407     return checkedAdd(
408         checkedMultiply(duration.getSeconds(), MICROS_PER_SECOND),
409         duration.getNanos() / NANOS_PER_MICROSECOND);
410   }
411 
412   /** Convert a Duration to the number of nanoseconds. */
413   @SuppressWarnings("GoodTime") // this is a legacy conversion API
toNanos(Duration duration)414   public static long toNanos(Duration duration) {
415     checkValid(duration);
416     return checkedAdd(
417         checkedMultiply(duration.getSeconds(), NANOS_PER_SECOND), duration.getNanos());
418   }
419 
420   // Math operations
421 
422   /** Add two durations. */
add(Duration d1, Duration d2)423   public static Duration add(Duration d1, Duration d2) {
424     checkValid(d1);
425     checkValid(d2);
426     return normalizedDuration(
427         checkedAdd(d1.getSeconds(), d2.getSeconds()), checkedAdd(d1.getNanos(), d2.getNanos()));
428   }
429 
430   /** Subtract a duration from another. */
subtract(Duration d1, Duration d2)431   public static Duration subtract(Duration d1, Duration d2) {
432     checkValid(d1);
433     checkValid(d2);
434     return normalizedDuration(
435         checkedSubtract(d1.getSeconds(), d2.getSeconds()),
436         checkedSubtract(d1.getNanos(), d2.getNanos()));
437   }
438 
normalizedDuration(long seconds, int nanos)439   static Duration normalizedDuration(long seconds, int nanos) {
440     if (nanos <= -NANOS_PER_SECOND || nanos >= NANOS_PER_SECOND) {
441       seconds = checkedAdd(seconds, nanos / NANOS_PER_SECOND);
442       nanos %= NANOS_PER_SECOND;
443     }
444     if (seconds > 0 && nanos < 0) {
445       nanos += NANOS_PER_SECOND; // no overflow since nanos is negative (and we're adding)
446       seconds--; // no overflow since seconds is positive (and we're decrementing)
447     }
448     if (seconds < 0 && nanos > 0) {
449       nanos -= NANOS_PER_SECOND; // no overflow since nanos is positive (and we're subtracting)
450       seconds++; // no overflow since seconds is negative (and we're incrementing)
451     }
452     Duration duration = Duration.newBuilder().setSeconds(seconds).setNanos(nanos).build();
453     return checkValid(duration);
454   }
455 }
456