diff options
author | Roland <rk@rkuhn.info> | 2012-09-12 11:58:23 +0200 |
---|---|---|
committer | Roland <rk@rkuhn.info> | 2012-09-12 11:58:23 +0200 |
commit | 300803606ebca352955e945cf468a0c2bfc83b9c (patch) | |
tree | 18bdb6df4b15a7d4ab538a0a5e3be4277e475e81 | |
parent | 85fae9e9771f4671d2ab070c9739436e627460e7 (diff) | |
download | scala-300803606ebca352955e945cf468a0c2bfc83b9c.tar.gz scala-300803606ebca352955e945cf468a0c2bfc83b9c.tar.bz2 scala-300803606ebca352955e945cf468a0c2bfc83b9c.zip |
restrict Deadline to finite durations (would have to throw otherwise)
-rw-r--r-- | src/library/scala/concurrent/util/Duration.scala | 19 | ||||
-rw-r--r-- | test/files/neg/deadline-inf-illegal.check | 15 | ||||
-rw-r--r-- | test/files/neg/deadline-inf-illegal.scala | 8 |
3 files changed, 33 insertions, 9 deletions
diff --git a/src/library/scala/concurrent/util/Duration.scala b/src/library/scala/concurrent/util/Duration.scala index d466d2978f..1a4bc1323d 100644 --- a/src/library/scala/concurrent/util/Duration.scala +++ b/src/library/scala/concurrent/util/Duration.scala @@ -28,26 +28,26 @@ import language.implicitConversions * does not take into account changes to the system clock (such as leap * seconds). */ -case class Deadline private (time: Duration) extends Ordered[Deadline] { +case class Deadline private (time: FiniteDuration) extends Ordered[Deadline] { /** * Return a deadline advanced (i.e. moved into the future) by the given duration. */ - def +(other: Duration): Deadline = copy(time = time + other) + def +(other: FiniteDuration): Deadline = copy(time = time + other) /** * Return a deadline moved backwards (i.e. towards the past) by the given duration. */ - def -(other: Duration): Deadline = copy(time = time - other) + def -(other: FiniteDuration): Deadline = copy(time = time - other) /** * Calculate time difference between this and the other deadline, where the result is directed (i.e. may be negative). */ - def -(other: Deadline): Duration = time - other.time + def -(other: Deadline): FiniteDuration = time - other.time /** * Calculate time difference between this duration and now; the result is negative if the deadline has passed. * * '''''Note that on some systems this operation is costly because it entails a system call.''''' * Check `System.nanoTime` for your platform. */ - def timeLeft: Duration = this - Deadline.now + def timeLeft: FiniteDuration = this - Deadline.now /** * Determine whether the deadline still lies in the future at the point where this method is called. * @@ -552,10 +552,6 @@ sealed abstract class Duration extends Serializable with Ordered[Duration] { * Return the larger of this and that duration as determined by the natural ordering. */ def max(other: Duration): Duration = if (this > other) this else other - /** - * Construct a [[Deadline]] from this duration by adding it to the current instant `Duration.now`. - */ - def fromNow: Deadline = Deadline.now + this // Java API @@ -655,6 +651,11 @@ final class FiniteDuration(val length: Long, val unit: TimeUnit) extends Duratio def toDays = unit.toDays(length) def toUnit(u: TimeUnit) = toNanos.toDouble / NANOSECONDS.convert(1, u) + /** + * Construct a [[Deadline]] from this duration by adding it to the current instant `Deadline.now`. + */ + def fromNow: Deadline = Deadline.now + this + private[this] def unitString = timeUnitName(unit) + ( if (length == 1) "" else "s" ) override def toString = "" + length + " " + unitString diff --git a/test/files/neg/deadline-inf-illegal.check b/test/files/neg/deadline-inf-illegal.check new file mode 100644 index 0000000000..2b9b25e48e --- /dev/null +++ b/test/files/neg/deadline-inf-illegal.check @@ -0,0 +1,15 @@ +deadline-inf-illegal.scala:5: error: value fromNow is not a member of scala.concurrent.util.Duration + d.fromNow + ^ +deadline-inf-illegal.scala:6: error: type mismatch; + found : scala.concurrent.util.Duration + required: scala.concurrent.util.FiniteDuration + Deadline.now + d + ^ +deadline-inf-illegal.scala:7: error: overloaded method value - with alternatives: + (other: scala.concurrent.util.Deadline)scala.concurrent.util.FiniteDuration <and> + (other: scala.concurrent.util.FiniteDuration)scala.concurrent.util.Deadline + cannot be applied to (scala.concurrent.util.Duration) + Deadline.now - d + ^ +three errors found diff --git a/test/files/neg/deadline-inf-illegal.scala b/test/files/neg/deadline-inf-illegal.scala new file mode 100644 index 0000000000..161089bfee --- /dev/null +++ b/test/files/neg/deadline-inf-illegal.scala @@ -0,0 +1,8 @@ +import concurrent.util.{ Deadline, Duration } + +class T { + val d: Duration = Duration.Zero + d.fromNow + Deadline.now + d + Deadline.now - d +} |