Guest User

Untitled

a guest
Jun 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. package com.cubicle6.time
  2.  
  3. import com.cubicle6.time.Instant._
  4.  
  5. /**
  6. * Duration class which stores durations as milliseconds and
  7. * facilitates operations that produce time instants. Simple
  8. * addition and subtraction between Duration instances is also
  9. * provided.
  10. */
  11. case class Duration(val milliseconds: Double) {
  12.  
  13. // Mathematic operations
  14. def + (d: Duration): Duration =
  15. new Duration(this.milliseconds + d.milliseconds)
  16.  
  17. def - (d: Duration): Duration =
  18. new Duration(this.milliseconds - d.milliseconds)
  19.  
  20. def + (i: Instant): Instant =
  21. i + this
  22.  
  23. def - (i: Instant): Instant =
  24. i - this
  25.  
  26. // Time relation operations
  27. def and(d: Duration): Duration =
  28. this + d
  29.  
  30. def from(i: Instant): Instant =
  31. new Instant(i.milliseconds + milliseconds)
  32.  
  33. def ago(): Instant =
  34. before(now)
  35.  
  36. def before(i: Instant): Instant =
  37. new Instant(i.milliseconds - milliseconds)
  38.  
  39. // Unit conversions
  40. def toSeconds: Double =
  41. this.milliseconds / 1000
  42.  
  43. def toMinutes: Double =
  44. this.milliseconds / (1000 * 60)
  45.  
  46. def toHours: Double =
  47. this.milliseconds / (1000 * 60 * 60)
  48.  
  49. def toDays: Double =
  50. this.milliseconds / (1000 * 60 * 60 * 24)
  51.  
  52. // Boring operations
  53. override def toString(): String =
  54. "Duration: " + this.milliseconds + " ms"
  55.  
  56. override def hashCode: Int = super.hashCode + milliseconds.hashCode
  57. }
  58.  
  59. /**
  60. * Duration Value class used for implementing the time DSL
  61. */
  62. class DurationValue(val value: Double) {
  63.  
  64. // Single units
  65. def millisecond: Duration =
  66. new Duration(value)
  67.  
  68. def second: Duration =
  69. new Duration(value * 1000)
  70.  
  71. def minute: Duration =
  72. new Duration(value * 1000 * 60)
  73.  
  74. def hour: Duration =
  75. new Duration(value * 1000 * 60 * 60)
  76.  
  77. def day: Duration =
  78. new Duration(value * 1000 * 60 * 60 * 24)
  79.  
  80. // Plural units
  81. def milliseconds(): Duration =
  82. new Duration(value)
  83.  
  84. def seconds(): Duration =
  85. new Duration(value * 1000)
  86.  
  87. def minutes(): Duration =
  88. new Duration(value * 1000 * 60)
  89.  
  90. def hours(): Duration =
  91. new Duration(value * 1000 * 60 * 60)
  92.  
  93. def days(): Duration =
  94. new Duration(value * 1000 * 60 * 60 * 24)
  95.  
  96. }
  97.  
  98. /**
  99. * Duration Singleton Object. Contains implicits.
  100. */
  101. object Duration {
  102.  
  103. implicit def double2durationValue(double: Double): DurationValue =
  104. new DurationValue(double)
  105.  
  106. implicit def float2durationValue(float: Float): DurationValue =
  107. new DurationValue(float.doubleValue)
  108.  
  109. implicit def integer2durationValue(integer: Int): DurationValue =
  110. new DurationValue(integer.doubleValue())
  111.  
  112. implicit def long2durationValue(long: Long): DurationValue =
  113. new DurationValue(long.doubleValue())
  114.  
  115. implicit def duration2long(duration: Duration): Long =
  116. duration.milliseconds.longValue
  117.  
  118. }
Add Comment
Please, Sign In to add comment