diff options
author | Kristen Kozak <sebright@google.com> | 2018-05-15 15:52:30 -0700 |
---|---|---|
committer | Kristen Kozak <sebright@google.com> | 2018-05-15 17:00:38 -0700 |
commit | 6966d688ea5b14bb8c6fe8e319c5c1bcecf06e50 (patch) | |
tree | e4cb6de7bebd68d54a031e3f0447309a7fb90e8c /api/src/main/java/io/opencensus/common | |
parent | 8d9515345f12a68bf4d8f87cac6e6fedccae2ac4 (diff) | |
download | opencensus-java-6966d688ea5b14bb8c6fe8e319c5c1bcecf06e50.tar.gz |
Throw IllegalArgumentException when Timestamp.create receives invalid arguments.
Throwing IllegalArgumentException for invalid arguments is more consistent with
the rest of the opencensus-java API. This commit also fixes a test that created
an invalid Timestamp.
Diffstat (limited to 'api/src/main/java/io/opencensus/common')
-rw-r--r-- | api/src/main/java/io/opencensus/common/Timestamp.java | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/api/src/main/java/io/opencensus/common/Timestamp.java b/api/src/main/java/io/opencensus/common/Timestamp.java index e3aae3a6..d17b3fd8 100644 --- a/api/src/main/java/io/opencensus/common/Timestamp.java +++ b/api/src/main/java/io/opencensus/common/Timestamp.java @@ -39,7 +39,6 @@ import javax.annotation.concurrent.Immutable; @Immutable @AutoValue public abstract class Timestamp implements Comparable<Timestamp> { - private static final Timestamp EPOCH = new AutoValue_Timestamp(0, 0); Timestamp() {} @@ -51,16 +50,25 @@ public abstract class Timestamp implements Comparable<Timestamp> { * @param nanos Non-negative fractions of a second at nanosecond resolution. Negative second * values with fractions must still have non-negative nanos values that count forward in time. * Must be from 0 to 999,999,999 inclusive. - * @return new {@code Timestamp} with specified fields. For invalid inputs, a {@code Timestamp} of - * zero is returned. + * @return new {@code Timestamp} with specified fields. + * @throws IllegalArgumentException if the arguments are out of range. * @since 0.5 */ public static Timestamp create(long seconds, int nanos) { - if (seconds < -MAX_SECONDS || seconds > MAX_SECONDS) { - return EPOCH; + if (seconds < -MAX_SECONDS) { + throw new IllegalArgumentException( + "'seconds' is less than minimum (" + -MAX_SECONDS + "): " + seconds); } - if (nanos < 0 || nanos > MAX_NANOS) { - return EPOCH; + if (seconds > MAX_SECONDS) { + throw new IllegalArgumentException( + "'seconds' is greater than maximum (" + MAX_SECONDS + "): " + seconds); + } + if (nanos < 0) { + throw new IllegalArgumentException("'nanos' is less than zero: " + nanos); + } + if (nanos > MAX_NANOS) { + throw new IllegalArgumentException( + "'nanos' is greater than maximum (" + MAX_NANOS + "): " + nanos); } return new AutoValue_Timestamp(seconds, nanos); } @@ -69,8 +77,9 @@ public abstract class Timestamp implements Comparable<Timestamp> { * Creates a new timestamp from the given milliseconds. * * @param epochMilli the timestamp represented in milliseconds since epoch. - * @return new {@code Timestamp} with specified fields. For invalid inputs, a {@code Timestamp} of - * zero is returned. + * @return new {@code Timestamp} with specified fields. + * @throws IllegalArgumentException if the number of milliseconds is out of the range that can be + * represented by {@code Timestamp}. * @since 0.5 */ public static Timestamp fromMillis(long epochMilli) { |