summaryrefslogtreecommitdiff
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
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
-rw-r--r--src/library/scala/concurrent/util/Duration.scala37
-rw-r--r--test/files/jvm/duration-tck.scala9
2 files changed, 42 insertions, 4 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
diff --git a/test/files/jvm/duration-tck.scala b/test/files/jvm/duration-tck.scala
index 0947e84004..c5dbe2e133 100644
--- a/test/files/jvm/duration-tck.scala
+++ b/test/files/jvm/duration-tck.scala
@@ -177,10 +177,17 @@ object Test extends App {
Thread.sleep(1.second.toMillis)
assert(dead.timeLeft < 1.second)
assert(dead2.timeLeft < 1.second)
+
+
+ // test integer mul/div
+ 500.millis * 2 mustBe 1.second
+ (500.millis * 2).unit mustBe MILLISECONDS
+ 1.second / 2 mustBe 500.millis
+ (1.second / 2).unit mustBe MILLISECONDS
// check statically retaining finite-ness
- val finiteDuration: FiniteDuration = 1.second plus 3.seconds minus 1.millisecond min 1.second max 1.second
+ val finiteDuration: FiniteDuration = 1.second * 2 / 3 mul 5 div 4 plus 3.seconds minus 1.millisecond min 1.second max 1.second
}