/
SpanSugar.scala
571 lines (530 loc) · 19.4 KB
/
SpanSugar.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest.time
/**
* Trait providing four implicit conversions that allow you to specify spans of time
* by invoking "units" methods such as <code>millis</code>, <code>seconds</code>, and <code>minutes</code>
* on <code>Int</code>, <code>Long</code>, <code>Float</code>, and <code>Double</code>.
*
* <p>
* This trait enables you to specify a span of time in a clear, boilerplate-free way when you
* need to provide an instance of <a href="Span.html"><code>Span</code></a>. This
* can be used, for example, with the <code>failAfter</code> method of trait
* <a href="../concurrent/Timeouts.html"><code>Timeouts</code></a> or the <code>timeLimit</code> field of trait
* <a href="../concurrent/TimeLimitedTests.html"><code>TimeLimitedTests</code></a>. It can also be used to specify
* timeouts when using traits <a href="../concurrent/Eventually.html"><code>Eventually</code></a>,
* <a href="../concurrent/Futures.html"><code>Futures</code></a>,
* <a href="../concurrent/Waiter.html"><code>Waiter</code></a>. Here are examples of each unit enabled by this trait:
* </p>
*
* <table style="border-collapse: collapse; border: 1px solid black">
* <tr>
* <th style="background-color: #CCCCCC; border-width: 1px; padding: 3px; text-align: center; border: 1px solid black">
* <strong><code>Int</code></strong>
* </th>
* <th style="background-color: #CCCCCC; border-width: 1px; padding: 3px; text-align: center; border: 1px solid black">
* <strong><code>Long</code></strong>
* </th>
* <th style="background-color: #CCCCCC; border-width: 1px; padding: 3px; text-align: center; border: 1px solid black">
* <strong><code>Float</code></strong>
* </th>
* <th style="background-color: #CCCCCC; border-width: 1px; padding: 3px; text-align: center; border: 1px solid black">
* <strong><code>Double</code></strong>
* </th>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1 nanosecond
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1L nanosecond
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0F nanosecond
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0 nanosecond
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100 nanoseconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100L nanoseconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8F nanoseconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8 nanoseconds
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1 microsecond
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1L microsecond
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0F microsecond
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0 microsecond
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100 microseconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100L microseconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8F microseconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8 microseconds
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1 millisecond
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1L millisecond
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0F millisecond
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0 millisecond
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100 milliseconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100L milliseconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8F milliseconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8 milliseconds
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100 millis
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100L millis
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8F millis
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8 millis
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1 second
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1L second
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0F second
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0 second
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100 seconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100L seconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8F seconds
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8 seconds
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1 minute
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1L minute
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0F minute
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0 minute
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100 minutes
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100L minutes
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8F minutes
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8 minutes
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1 hour
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1L hour
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0F hour
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0 hour
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100 hours
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100L hours
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8F hours
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8 hours
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1 day
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1L day
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0F day
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 1.0 day
* </td>
* </tr>
* <tr>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100 days
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 100L days
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8F days
* </td>
* <td style="border-width: 1px; padding: 3px; border: 1px solid black; text-align: right">
* 99.8 days
* </td>
* </tr>
* </table>
*
* <p>
* This trait is not the default way to specify <code>Span</code>s for two reasons. First, it adds
* four implicits, which would give the compiler more work to do and may conflict with other implicits the
* user has in scope. Instead, <code>Span</code> provides a clear, concise default way to specify time
* spans that requires no implicits. Here's an example:
* </p>
*
* <pre class="stHighlighted">
* <span class="stType">Span</span>(<span class="stLiteral">1</span>, <span class="stType">Second</span>)
* </pre>
*
* <p>
* If you already have implicit conversions in scope that provide a similar syntax sugar for expression
* time spans, you can use that by providing an implicit conversion from the result of those expressions
* to <code>Span</code>. Note that because of implicit conversions in the <code>Span</code> companion object,
* you can use a <code>scala.concurrent.duration.Duration</code> (including in its "sugary" form) where
* a <code>Span</code> is needed, and vice versa.
* </p>
*/
trait SpanSugar {
implicit val postfixOps: languageFeature.postfixOps = language.postfixOps
/**
* Class containing methods that return a <code>Span</code> time value calculated from the
* <code>Long</code> value passed to the <code>GrainOfTime</code> constructor.
*
* @param value the value to be converted
*/
class GrainOfTime(value: Long) {
/**
* A units method for one nanosecond.
*
* @return A <code>Span</code> representing the value passed to the constructor in nanoseconds
*/
def nanosecond: Span = Span(value, Nanosecond)
/**
* A units method for nanoseconds.
*
* @return A <code>Span</code> representing the value passed to the constructor in nanoseconds
*/
def nanoseconds: Span = Span(value, Nanoseconds)
/**
* A units method for one microsecond.
*
* @return A <code>Span</code> representing the value passed to the constructor in microseconds
*/
def microsecond: Span = Span(value, Microsecond)
/**
* A units method for microseconds.
*
* @return A <code>Span</code> representing the value passed to the constructor in microseconds
*/
def microseconds: Span = Span(value, Microseconds)
/**
* A units method for one millisecond.
*
* @return A <code>Span</code> representing the value passed to the constructor in milliseconds
*/
def millisecond: Span = Span(value, Millisecond)
/**
* A units method for milliseconds.
*
* @return A <code>Span</code> representing the value passed to the constructor in milliseconds
*/
def milliseconds: Span = Span(value, Milliseconds)
/**
* A shorter units method for milliseconds.
*
* @return A <code>Span</code> representing the value passed to the constructor in milliseconds
*/
def millis: Span = Span(value, Millis)
/**
* A units method for one second.
*
* @return A <code>Span</code> representing the value passed to the constructor in seconds
*/
def second: Span = Span(value, Second)
/**
* A units method for seconds.
*
* @return A <code>Span</code> representing the value passed to the constructor in seconds
*/
def seconds: Span = Span(value, Seconds)
/**
* A units method for one minute.
*
* @return A <code>Span</code> representing the value passed to the constructor in minutes
*/
def minute: Span = Span(value, Minute)
/**
* A units method for minutes.
*
* @return A <code>Span</code> representing the value passed to the constructor in minutes
*/
def minutes: Span = Span(value, Minutes)
/**
* A units method for one hour.
*
* @return A <code>Span</code> representing the value passed to the constructor in hours
*/
def hour: Span = Span(value, Hour)
/**
* A units method for hours.
*
* @return A <code>Span</code> representing the value passed to the constructor in hours
*/
def hours: Span = Span(value, Hours)
/**
* A units method for one day.
*
* @return A <code>Span</code> representing the value passed to the constructor in days
*/
def day: Span = Span(value, Day)
/**
* A units method for days.
*
* @return A <code>Span</code> representing the value passed to the constructor multiplied in days
*/
def days: Span = Span(value, Days)
}
/**
* Class containing methods that return a <code>Span</code> time value calculated from the
* <code>Double</code> value passed to the <code>FloatingGrainOfTime</code> constructor.
*
* @param value the value to be converted
*/
class FloatingGrainOfTime(value: Double) {
/**
* A units method for one nanosecond.
*
* @return A <code>Span</code> representing the value passed to the constructor in nanoseconds
*/
def nanosecond: Span = Span(value, Nanosecond)
/**
* A units method for nanoseconds.
*
* @return A <code>Span</code> representing the value passed to the constructor in nanoseconds
*/
def nanoseconds: Span = Span(value, Nanoseconds)
/**
* A units method for one microsecond.
*
* @return A <code>Span</code> representing the value passed to the constructor in microseconds
*/
def microsecond: Span = Span(value, Microsecond)
/**
* A units method for microseconds.
*
* @return A <code>Span</code> representing the value passed to the constructor in microseconds
*/
def microseconds: Span = Span(value, Microseconds)
/**
* A units method for one millisecond.
*
* @return A <code>Span</code> representing the value passed to the constructor in milliseconds
*/
def millisecond: Span = Span(value, Millisecond)
/**
* A units method for milliseconds.
*
* @return A <code>Span</code> representing the value passed to the constructor in milliseconds
*/
def milliseconds: Span = Span(value, Milliseconds)
/**
* A shorter units method for milliseconds.
*
* @return A <code>Span</code> representing the value passed to the constructor in milliseconds
*/
def millis: Span = Span(value, Millis)
/**
* A units method for one second.
*
* @return A <code>Span</code> representing the value passed to the constructor in seconds
*/
def second: Span = Span(value, Second)
/**
* A units method for seconds.
*
* @return A <code>Span</code> representing the value passed to the constructor in seconds
*/
def seconds: Span = Span(value, Seconds)
/**
* A units method for one minute.
*
* @return A <code>Span</code> representing the value passed to the constructor in minutes
*/
def minute: Span = Span(value, Minute)
/**
* A units method for minutes.
*
* @return A <code>Span</code> representing the value passed to the constructor in minutes
*/
def minutes: Span = Span(value, Minutes)
/**
* A units method for one hour.
*
* @return A <code>Span</code> representing the value passed to the constructor in hours
*/
def hour: Span = Span(value, Hour)
/**
* A units method for hours.
*
* @return A <code>Span</code> representing the value passed to the constructor in hours
*/
def hours: Span = Span(value, Hours)
/**
* A units method for one day.
*
* @return A <code>Span</code> representing the value passed to the constructor in days
*/
def day: Span = Span(value, Day)
/**
* A units method for days.
*
* @return A <code>Span</code> representing the value passed to the constructor multiplied in days
*/
def days: Span = Span(value, Days)
}
import scala.language.implicitConversions
/**
* Implicit conversion that adds time units methods to <code>Int</code>s.
*
* @param i: the <code>Int</code> to which to add time units methods
* @return a <code>GrainOfTime</code> wrapping the passed <code>Int</code>
*/
implicit def convertIntToGrainOfTime(i: Int): GrainOfTime = new GrainOfTime(i)
/**
* Implicit conversion that adds time units methods to <code>Long</code>s.
*
* @param i: the <code>Long</code> to which to add time units methods
* @return a <code>GrainOfTime</code> wrapping the passed <code>Long</code>
*/
implicit def convertLongToGrainOfTime(i: Long): GrainOfTime = new GrainOfTime(i)
/**
* Implicit conversion that adds time units methods to <code>Float</code>s.
*
* @param f: the <code>Float</code> to which to add time units methods
* @return a <code>FloatingGrainOfTime</code> wrapping the passed <code>Float</code>
*/
implicit def convertFloatToGrainOfTime(f: Float): FloatingGrainOfTime = new FloatingGrainOfTime(f)
/**
* Implicit conversion that adds time units methods to <code>Double</code>s.
*
* @param d: the <code>Double</code> to which to add time units methods
* @return a <code>FloatingGrainOfTime</code> wrapping the passed <code>Double</code>
*/
implicit def convertDoubleToGrainOfTime(d: Double): FloatingGrainOfTime = new FloatingGrainOfTime(d)
}
/**
* Companion object that facilitates the importing of <code>SpanSugar</code> members as
* an alternative to mixing it in. One use case is to import <code>SpanSugar</code> members so you can use
* them in the Scala interpreter:
*
* <pre class="stREPL">
* $scala -classpath scalatest.jar
* Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
* Type in expressions to have them evaluated.
* Type :help for more information.
*
* scala> import org.scalatest._
* import org.scalatest._
*
* scala> import concurrent.Eventually._
* import org.scalatest.concurrent.Eventually._
*
* scala> import time.SpanSugar._
* import org.scalatest.time.SpanSugar._
*
* scala> eventually(timeout(100 millis)) { 1 + 1 should equal (3) }
* </pre>
*/
object SpanSugar extends SpanSugar