Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.07 KB | None | 0 0
  1. package ex03
  2.  
  3. import java.time.{LocalDate, Month}
  4. import java.util.concurrent.{Executors, ScheduledFuture, TimeUnit}
  5.  
  6. import rescala._
  7.  
  8. class Clock(val startDate: LocalDate) {
  9.  
  10.     private val scheduler = Executors.newScheduledThreadPool(1)
  11.     private var tickHandle: ScheduledFuture[_] = _
  12.  
  13.     private val tick: Evt[Unit] = Evt[Unit]()
  14.     private val curDate = Var(startDate)
  15.  
  16.     def start(): Unit = {
  17.         tickHandle = scheduler.scheduleAtFixedRate(step, 1, 1000, TimeUnit.MILLISECONDS)
  18.     }
  19.  
  20.     def stop(): Unit = {
  21.         tickHandle.cancel(true)
  22.         scheduler.shutdown()
  23.     }
  24.  
  25.     def step: Runnable = () => {
  26.         while (true) {
  27.             tick.fire()
  28.             Thread.sleep(1000)
  29.         }
  30.     }
  31.  
  32.     val dayOfTheYear: Signal[Int] = {
  33.         val f = (x: Int, y: Unit) => {
  34.             if (curDate.now.isBefore(LocalDate.of(2017, 12, 8))) {
  35.                 val curDay = x + 1
  36.                 if (curDay == 366) {
  37.                     if (java.time.Year.isLeap(year.now)) curDay
  38.                     else 1
  39.                 } else if (curDay > 366) 1
  40.                 else curDay
  41.             } else x
  42.         }
  43.         tick.fold(startDate.getDayOfYear)(f)
  44.     }
  45.  
  46.     val year: Signal[Int] = Signal {
  47.         now.now.getYear
  48.     }
  49.  
  50.     val month: Signal[Month] = Signal[Month] {
  51.         now.now.getMonth
  52.     }
  53.  
  54.     val dayOfMonth: Signal[Int] = Signal[Int] {
  55.         now.now.getDayOfMonth
  56.     }
  57.  
  58.     val isWeekend: Signal[Boolean] = Signal[Boolean] {
  59.         if (now.now.getDayOfWeek.getValue == 6 || now.now.getDayOfWeek.getValue == 7) true
  60.         else false
  61.     }
  62.  
  63.     val isWeekday: Signal[Boolean] = Signal[Boolean] {
  64.         if (now.now.getDayOfWeek.getValue != 6 && now.now.getDayOfWeek.getValue != 7) true
  65.         else false
  66.     }
  67.  
  68.     val now: Signal[LocalDate] = {
  69.         val f = (x: LocalDate, y: Unit) => {
  70.             if (x.isBefore(LocalDate.of(2017, 12, 8))) {
  71.                 curDate.set(x plusDays 1)
  72.                 x plusDays 1
  73.             } else x
  74.         }
  75.         tick.fold(startDate)(f)
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement