-
Notifications
You must be signed in to change notification settings - Fork 580
/
Duration.scala
495 lines (415 loc) · 14.6 KB
/
Duration.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package com.twitter.util
import java.io.Serializable
import java.util.concurrent.TimeUnit
object Duration extends TimeLikeOps[Duration] {
def fromNanoseconds(nanoseconds: Long): Duration =
if (nanoseconds == 0L) Zero
else new Duration(nanoseconds)
// This is needed for Java compatibility.
override def fromFractionalSeconds(seconds: Double): Duration =
super.fromFractionalSeconds(seconds)
override def fromSeconds(seconds: Int): Duration = super.fromSeconds(seconds)
override def fromMinutes(minutes: Int): Duration = super.fromMinutes(minutes)
override def fromMilliseconds(millis: Long): Duration = super.fromMilliseconds(millis)
override def fromMicroseconds(micros: Long): Duration = super.fromMicroseconds(micros)
val NanosPerMicrosecond: Long = 1000L
val NanosPerMillisecond: Long = NanosPerMicrosecond * 1000L
val NanosPerSecond: Long = NanosPerMillisecond * 1000L
val NanosPerMinute: Long = NanosPerSecond * 60
val NanosPerHour: Long = NanosPerMinute * 60
val NanosPerDay: Long = NanosPerHour * 24
/**
* Create a duration from a [[java.util.concurrent.TimeUnit]].
* Synonym for `apply`.
*/
def fromTimeUnit(value: Long, unit: TimeUnit): Duration = apply(value, unit)
/**
* Create a duration from a [[java.util.concurrent.TimeUnit]].
*/
def apply(value: Long, unit: TimeUnit): Duration = {
val ns = TimeUnit.NANOSECONDS.convert(value, unit)
fromNanoseconds(ns)
}
// This is needed for Java compatibility.
override val Zero: Duration = new Duration(0)
/**
* Duration `Top` is greater than any other duration, except for
* itself. `Top`'s complement is `Bottom`.
*/
val Top: Duration = new Duration(Long.MaxValue) {
override def hashCode = System.identityHashCode(this)
/** Top is equal only to Top and greater than every finite duration */
override def compare(that: Duration) =
if (that eq Undefined) -1
else if (that eq Top) 0
else 1
override def equals(other: Any) = other match {
case d: Duration => d eq this
case _ => false
}
override def *(x: Long): Duration =
if (x == 0) Undefined
else if (x < 0) Bottom
else Top
override def *(x: Double): Duration =
if (x == 0.0 || java.lang.Double.isNaN(x)) Undefined
else if (x < 0.0) Bottom
else Top
override def /(x: Long): Duration =
if (x == 0) Undefined
else if (x < 0) Bottom
else Top
override def /(x: Double): Duration =
if (x == 0.0 || java.lang.Double.isNaN(x)) Undefined
else if (x < 0.0) Bottom
else Top
override def isFinite = false
override def %(x: Duration) = Undefined
override def abs = this
override def fromNow = Time.Top
override def ago = Time.Bottom
override def afterEpoch = Time.Top
override def +(delta: Duration) = delta match {
case Bottom | Undefined => Undefined
case _ => this
}
override def unary_- = Bottom
override def toString = "Duration.Top"
private def writeReplace(): Object = DurationBox.Top()
}
/**
* Duration `Bottom` is smaller than any other duration, except for
* itself. `Bottom`'s complement is `Top`.
*/
val Bottom: Duration = new Duration(Long.MinValue) {
override def hashCode = System.identityHashCode(this)
/** Bottom is equal to Bottom, but smaller than everything else */
override def compare(that: Duration) = if (this eq that) 0 else -1
override def equals(other: Any) = other match {
case d: Duration => d eq this
case _ => false
}
/** Scaling arithmetic is Bottom preserving. */
override def *(x: Long): Duration =
if (x == 0) Undefined
else if (x < 0) Top
else Bottom
override def *(x: Double): Duration =
if (x == 0.0 || java.lang.Double.isNaN(x)) Undefined
else if (x < 0.0) Top
else Bottom
override def /(x: Long): Duration =
if (x == 0) Undefined
else if (x < 0) Top
else Bottom
override def /(x: Double): Duration =
if (x == 0.0 || java.lang.Double.isNaN(x)) Undefined
else if (x < 0.0) Top
else Bottom
override def %(x: Duration): Duration = Undefined
override def abs = Top
override def fromNow = Time.Bottom
override def ago = Time.Top
override def afterEpoch = Time.Bottom
override def isFinite = false
override def +(delta: Duration) = delta match {
case Top | Undefined => Undefined
case _ => this
}
override def unary_- = Top
override def toString = "Duration.Bottom"
private def writeReplace(): Object = DurationBox.Bottom()
}
val Undefined: Duration = new Duration(0) {
override def hashCode = System.identityHashCode(this)
override def compare(that: Duration) = if (this eq that) 0 else 1
override def equals(other: Any) = other match {
case d: Duration => d eq this
case _ => false
}
override def *(x: Long): Duration = this
override def *(x: Double): Duration = this
override def /(x: Long): Duration = this
override def /(x: Double): Duration = this
override def %(x: Duration): Duration = this
override def abs = this
override def fromNow = Time.Undefined
override def ago = Time.Undefined
override def afterEpoch = Time.Undefined
override def +(delta: Duration) = this
override def unary_- = this
override def isFinite = false
override def toString = "Duration.Undefined"
private def writeReplace(): Object = DurationBox.Undefined()
}
private val timeUnits = Seq(
TimeUnit.DAYS,
TimeUnit.HOURS,
TimeUnit.MINUTES,
TimeUnit.SECONDS,
TimeUnit.MILLISECONDS,
TimeUnit.MICROSECONDS,
TimeUnit.NANOSECONDS
)
private val nameToUnit: Map[String, TimeUnit] =
TimeUnit
.values()
.flatMap { u =>
val pluralK = u.toString.toLowerCase
val singularK = pluralK dropRight 1
Seq(pluralK -> u, singularK -> u)
}
.toMap
private val SingleDurationRegex =
"""\s*([+-]?)\s*(?:([0-9]+)\.([a-z]+)|duration\.(top|bottom|undefined))""".r
private val FullDurationRegex = ("(" + SingleDurationRegex.pattern.pattern + """)+\s*""").r
/**
* Parse a String representation of a duration. This method will
* parse any duration generated by Duration.toString.
*
* The format is either one of the special values, or non-empty
* sequence of durations. Each duration is a sign, an integer, a
* dot, and a unit. The unit may be plural or singular. The parser
* will ignore whitespace around signs and at the beginning and end.
* (That is, it accepts "1.second + 1.minute" and " 1.second ".)
* It's permissible to omit the sign before the first duration.
*
* The special values are "Duration.Top", "Duration.Bottom" and
* "Duration.Undefined".
*
* The parser is case-insensitive.
*
* @throws RuntimeException if the string cannot be parsed.
*/
def parse(s: String): Duration = {
val ss = s.toLowerCase
ss match {
case FullDurationRegex(_*) =>
SingleDurationRegex.findAllIn(ss).matchData.zipWithIndex map {
case (m, i) =>
val List(signStr, numStr, unitStr, special) = m.subgroups
val absDuration = special match {
case "top" => Top
case "bottom" => Bottom
case "undefined" => Undefined
case _ =>
val u = nameToUnit.get(unitStr) match {
case Some(t) => t
case None => throw new NumberFormatException("Invalid unit: " + unitStr)
}
Duration(numStr.toLong, u)
}
signStr match {
case "-" => -absDuration
// It's only OK to omit the sign for the first duration.
case "" if i > 0 =>
throw new NumberFormatException("Expected a sign between durations")
case _ => absDuration
}
// It's OK to use reduce because the regex ensures that there is
// at least one element
} reduce { _ + _ }
case _ => throw new NumberFormatException("Invalid duration: " + s)
}
}
}
private[util] object DurationBox {
case class Finite(nanos: Long) extends Serializable {
private def readResolve(): Object = Duration.fromNanoseconds(nanos)
}
case class Top() extends Serializable {
private def readResolve(): Object = Duration.Top
}
case class Bottom() extends Serializable {
private def readResolve(): Object = Duration.Bottom
}
case class Undefined() extends Serializable {
private def readResolve(): Object = Duration.Undefined
}
}
/**
* A `Duration` represents the span between two points in time. It represents
* this with a signed long, and thus the largest representable duration is:
*
* 106751.days+23.hours+47.minutes+16.seconds
* +854.milliseconds+775.microseconds+807.nanoseconds
*
* Durations may be constructed via its companion object,
* `Duration.fromNanoseconds`, `Duration.fromSeconds`, etc. or by
* using the time conversions:
*
* {{{
* import com.twitter.conversions.DurationOps._
*
* 3.days+4.nanoseconds
* }}}
*
* In addition to the timespans in the range of `Long.MinValue` to
* `Long.MaxValue` nanoseconds, durations have two distinguished
* values: `Duration.Top` and `Duration.Bottom`. These have special
* semantics: `Top` is greater than every other duration, save for
* itself; `Bottom` is smaller than any duration except for
* itself — they act like positive and negative infinity, and
* their arithmetic follows. This is useful for representing durations
* that are truly infinite; for example the absence of a timeout.
*/
sealed class Duration private[util] (protected val nanos: Long) extends {
protected val ops: Duration.type = Duration
} with TimeLike[Duration] with Serializable {
import ops._
def inNanoseconds: Long = nanos
/**
* Returns the length of the duration in the given TimeUnit.
*
* In general, a simpler approach is to use the named methods (eg. inSeconds)
* However, this is useful for more programmatic call sites.
*/
def inUnit(unit: TimeUnit): Long =
unit.convert(inNanoseconds, TimeUnit.NANOSECONDS)
/**
* toString produces a representation that
*
* - loses no information
* - is easy to read
* - can be read back in if com.twitter.conversions.DurationOps._ is imported
*
* An example:
*
* com.twitter.util.Duration(9999999, java.util.concurrent.TimeUnit.MICROSECONDS)
* res0: com.twitter.util.Duration = 9.seconds+999.milliseconds+999.microseconds
*/
override def toString: String = {
if (nanos == 0)
return "0.seconds"
val s = new StringBuilder
var ns = nanos
for (u <- timeUnits) {
val v = u.convert(ns, TimeUnit.NANOSECONDS)
if (v != 0) {
ns -= TimeUnit.NANOSECONDS.convert(v, u)
if (v > 0 && !s.isEmpty)
s.append("+")
s.append(v.toString)
s.append(".")
s.append(u.name.toLowerCase)
}
}
s.toString()
}
override def equals(other: Any): Boolean = {
// in order to ensure that the sentinels are only equal
// to themselves, we need to make sure we only compare nanos
// when both instances are `Duration`s and not a sentinel subclass.
if (other != null && (other.getClass eq getClass)) {
other.asInstanceOf[Duration].nanos == nanos
} else {
false
}
}
override def hashCode: Int =
// inline java.lang.Long.hashCode to avoid the BoxesRunTime.boxToLong
// and then Long.hashCode code.
(nanos ^ (nanos >>> 32)).toInt
/**
* Scales this `Duration` by multiplying by `x`.
*/
def *(x: Long): Duration = {
def overflowedDuration(a: Long, b: Long): Duration =
if ((a < 0) == (b < 0)) Duration.Top else Duration.Bottom
// a < b
def multiplyNanos(a: Long, b: Long): Duration =
if (a < 0L)
if (b < 0L && a < Long.MaxValue / b) overflowedDuration(a, b)
else if (b > 0L && Long.MinValue / b > a) overflowedDuration(a, b)
else Duration.fromNanoseconds(a * b)
else if (a > 0L && a > Long.MaxValue / b) overflowedDuration(a, b)
else Duration.fromNanoseconds(a * b)
if (nanos > x) multiplyNanos(x, nanos) else multiplyNanos(nanos, x)
}
/**
* Scales this `Duration` by multiplying by `x`.
*/
def *(x: Double): Duration = (nanos * x) match {
case product if java.lang.Double.isNaN(product) => Undefined
case Double.PositiveInfinity => Top
case Double.NegativeInfinity => Bottom
case product =>
val productLong = product.toLong
if (productLong == Long.MaxValue) Top
else if (productLong == Long.MinValue) Bottom
else fromNanoseconds(productLong)
}
/**
* Scales this `Duration` by dividing by `x`.
*/
def /(x: Long): Duration =
if (x != 0) fromNanoseconds(nanos / x)
else if (nanos == 0) Undefined
else if (nanos < 0) Bottom
else Top
/**
* Scales this `Duration` by dividing by `x`.
*/
def /(x: Double): Duration =
if (x == 0.0) this / 0
else this * (1.0 / x)
/**
* Scales this `Duration` by modding by `x`.
*/
def %(x: Duration): Duration = x match {
case Undefined => Undefined
case ns if ns.isZero => Undefined
case ns if ns.isFinite => fromNanoseconds(nanos % ns.inNanoseconds)
case Top | Bottom => this
}
/**
* Converts negative durations to positive durations.
*/
def abs: Duration = if (nanos < 0) -this else this
def fromNow: Time = Time.now + this
def ago: Time = Time.now - this
def afterEpoch: Time = Time.epoch + this
// Note that Long.MinValue receives special treatment here because
// of two's complement: -Long.MinValue == Long.MinValue.
def unary_- : Duration =
if (inNanoseconds == Long.MinValue) Top
else ops.fromNanoseconds(-inNanoseconds)
def diff(that: Duration): Duration = this - that
def isFinite: Boolean = true
private def writeReplace(): Object = DurationBox.Finite(inNanoseconds)
/**
* @see operator +
*/
def plus(delta: Duration): Duration = this + delta
/**
* @see operator -
*/
def minus(delta: Duration): Duration = this - delta
/**
* Negates this `Duration`.
*/
def neg: Duration = -this
/**
* @see operator *
*/
def mul(x: Long): Duration = this * x
/**
* @see operator *
*/
def mul(x: Double): Duration = this * x
/**
* @see operator /
*/
def div(x: Long): Duration = this / x
/**
* @see operator /
*/
def div(x: Double): Duration = this / x
/**
* @see operator %
*/
def rem(x: Duration): Duration = this % x
// for Java-compatibility
override def floor(increment: Duration): Duration = super.floor(increment)
override def ceil(increment: Duration): Duration = super.ceil(increment)
}