Advertisement
Guest User

Untitled

a guest
Jan 15th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.82 KB | None | 0 0
  1. import collection.mutable
  2. import java.util.Date
  3. import scala.concurrent.duration.Duration
  4.  
  5. trait AirportScheduler {
  6.   def book(d: Date): Boolean
  7.   def free(d: Date): Boolean
  8.   def isValid(d: Date): Boolean
  9. }
  10.  
  11. class AirportSchedulerImpl extends AirportScheduler {
  12.  
  13.   private[this] val bookedTimes = mutable.TreeSet.empty[Date]
  14.   // Hours worth of milliseconds
  15.   private[this] val Duration = 60 * 60 * 1000
  16.  
  17.   def book(d: Date): Boolean = {
  18.     if (isValid(d)) bookedTimes.add(d)
  19.     else false
  20.   }
  21.  
  22.   def isValid(d: Date): Boolean = {
  23.     val dt = d.getTime()
  24.  
  25.     bookedTimes.filter { r =>
  26.       val rt = r.getTime()
  27.  
  28.       // find any nodes that our reserved start time may fall between
  29.       dt >= rt && dt <= rt + Duration
  30.     }.isEmpty
  31.   }
  32.  
  33.   def free(d: Date): Boolean = {
  34.     bookedTimes.remove(d)
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement