summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorRoland <rk@rkuhn.info>2012-09-18 15:00:48 +0200
committerRoland <rk@rkuhn.info>2012-09-18 15:00:48 +0200
commitc555ff56db737bc65097add317946ee6d6166cd8 (patch)
tree844d6e5db4d95e91f2626966e12975c958a8f440 /src/library
parent66603a2c003852d39faec20a9763fb0e25049cf4 (diff)
downloadscala-c555ff56db737bc65097add317946ee6d6166cd8.tar.gz
scala-c555ff56db737bc65097add317946ee6d6166cd8.tar.bz2
scala-c555ff56db737bc65097add317946ee6d6166cd8.zip
enable integer multiplication/divison on FiniteDuration, see SI-6389
- without this "2.seconds * 2" will not return FiniteDuration but Duration - added test case verifying this behavior - matches normal arithmetics: integers throw on div-by-zero while doubles yield infinity; extended here to include overflow protection on integer multiplication
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/concurrent/util/Duration.scala37
1 files changed, 34 insertions, 3 deletions
diff --git a/src/library/scala/concurrent/util/Duration.scala b/src/library/scala/concurrent/util/Duration.scala
index 3f8b98831e..4567bf8ee6 100644
--- a/src/library/scala/concurrent/util/Duration.scala
+++ b/src/library/scala/concurrent/util/Duration.scala
@@ -704,16 +704,47 @@ final class FiniteDuration(val length: Long, val unit: TimeUnit) extends Duratio
else if ((length < 0) ^ (other > Zero)) 0d
else minusZero
- // overridden methods taking FiniteDurations, so that you can calculate while statically staying finite
+ // overloaded methods taking FiniteDurations, so that you can calculate while statically staying finite
def +(other: FiniteDuration) = add(other.length, other.unit)
def -(other: FiniteDuration) = add(-other.length, other.unit)
def plus(other: FiniteDuration) = this + other
def minus(other: FiniteDuration) = this - other
- override def div(factor: Double) = this / factor
- override def mul(factor: Double) = this * factor
def min(other: FiniteDuration) = if (this < other) this else other
def max(other: FiniteDuration) = if (this > other) this else other
+ // overloaded methods taking Long so that you can calculate while statically staying finite
+
+ /**
+ * Return the quotient of this duration and the given integer factor.
+ *
+ * @throws ArithmeticException if the factor is 0
+ */
+ def /(factor: Long) = fromNanos(toNanos / factor)
+
+ /**
+ * Return the product of this duration and the given integer factor.
+ *
+ * @throws IllegalArgumentException if the result would overflow the range of FiniteDuration
+ */
+ def *(factor: Long) = {
+ if (length > Long.MaxValue / factor) throw new IllegalArgumentException("multiplication overflow")
+ new FiniteDuration(length * factor, unit)
+ }
+
+ /**
+ * Return the quotient of this duration and the given integer factor.
+ *
+ * @throws ArithmeticException if the factor is 0
+ */
+ def div(factor: Long) = this / factor
+
+ /**
+ * Return the product of this duration and the given integer factor.
+ *
+ * @throws IllegalArgumentException if the result would overflow the range of FiniteDuration
+ */
+ def mul(factor: Long) = this * factor
+
def unary_- = Duration(-length, unit)
final def isFinite() = true