/
package.scala
87 lines (75 loc) · 2.97 KB
/
package.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.concurrent
import scala.language.implicitConversions
package object duration {
/**
* This object can be used as closing token if you prefer dot-less style but do not want
* to enable language.postfixOps:
*
* {{{
* import scala.concurrent.duration._
*
* val duration = 2 seconds span
* }}}
*/
object span
/**
* This object can be used as closing token for declaring a deadline at some future point
* in time:
*
* {{{
* import scala.concurrent.duration._
*
* val deadline = 3 seconds fromNow
* }}}
*/
object fromNow
type TimeUnit = java.util.concurrent.TimeUnit
final val DAYS = java.util.concurrent.TimeUnit.DAYS
final val HOURS = java.util.concurrent.TimeUnit.HOURS
final val MICROSECONDS = java.util.concurrent.TimeUnit.MICROSECONDS
final val MILLISECONDS = java.util.concurrent.TimeUnit.MILLISECONDS
final val MINUTES = java.util.concurrent.TimeUnit.MINUTES
final val NANOSECONDS = java.util.concurrent.TimeUnit.NANOSECONDS
final val SECONDS = java.util.concurrent.TimeUnit.SECONDS
implicit def pairIntToDuration(p: (Int, TimeUnit)): Duration = Duration(p._1.toLong, p._2)
implicit def pairLongToDuration(p: (Long, TimeUnit)): FiniteDuration = Duration(p._1, p._2)
implicit def durationToPair(d: Duration): (Long, TimeUnit) = (d.length, d.unit)
implicit final class DurationInt(private val n: Int) extends AnyVal with DurationConversions {
override protected def durationIn(unit: TimeUnit): FiniteDuration = Duration(n.toLong, unit)
}
implicit final class DurationLong(private val n: Long) extends AnyVal with DurationConversions {
override protected def durationIn(unit: TimeUnit): FiniteDuration = Duration(n, unit)
}
implicit final class DurationDouble(private val d: Double) extends AnyVal with DurationConversions {
override protected def durationIn(unit: TimeUnit): FiniteDuration =
Duration(d, unit) match {
case f: FiniteDuration => f
case _ => throw new IllegalArgumentException("Duration DSL not applicable to " + d)
}
}
/*
* Avoid reflection based invocation by using non-duck type
*/
implicit final class IntMult(private val i: Int) extends AnyVal {
def *(d: Duration): Duration = d * i.toDouble
def *(d: FiniteDuration): FiniteDuration = d * i.toLong
}
implicit final class LongMult(private val i: Long) extends AnyVal {
def *(d: Duration): Duration = d * i.toDouble
def *(d: FiniteDuration): FiniteDuration = d * i.toLong
}
implicit final class DoubleMult(private val f: Double) extends AnyVal {
def *(d: Duration): Duration = d * f.toDouble
}
}