Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ex03
- import java.time.{LocalDate, Month}
- import java.util.concurrent.{Executors, ScheduledFuture, TimeUnit}
- import rescala._
- class Clock(val startDate: LocalDate) {
- private val scheduler = Executors.newScheduledThreadPool(1)
- private var tickHandle: ScheduledFuture[_] = _
- private val tick: Evt[Unit] = Evt[Unit]()
- private val curDate = Var(startDate)
- def start(): Unit = {
- tickHandle = scheduler.scheduleAtFixedRate(step, 1, 1000, TimeUnit.MILLISECONDS)
- }
- def stop(): Unit = {
- tickHandle.cancel(true)
- scheduler.shutdown()
- }
- def step: Runnable = () => {
- while (true) {
- tick.fire()
- Thread.sleep(1000)
- }
- }
- val dayOfTheYear: Signal[Int] = {
- val f = (x: Int, y: Unit) => {
- if (curDate.now.isBefore(LocalDate.of(2017, 12, 8))) {
- val curDay = x + 1
- if (curDay == 366) {
- if (java.time.Year.isLeap(year.now)) curDay
- else 1
- } else if (curDay > 366) 1
- else curDay
- } else x
- }
- tick.fold(startDate.getDayOfYear)(f)
- }
- val year: Signal[Int] = Signal {
- now.now.getYear
- }
- val month: Signal[Month] = Signal[Month] {
- now.now.getMonth
- }
- val dayOfMonth: Signal[Int] = Signal[Int] {
- now.now.getDayOfMonth
- }
- val isWeekend: Signal[Boolean] = Signal[Boolean] {
- if (now.now.getDayOfWeek.getValue == 6 || now.now.getDayOfWeek.getValue == 7) true
- else false
- }
- val isWeekday: Signal[Boolean] = Signal[Boolean] {
- if (now.now.getDayOfWeek.getValue != 6 && now.now.getDayOfWeek.getValue != 7) true
- else false
- }
- val now: Signal[LocalDate] = {
- val f = (x: LocalDate, y: Unit) => {
- if (x.isBefore(LocalDate.of(2017, 12, 8))) {
- curDate.set(x plusDays 1)
- x plusDays 1
- } else x
- }
- tick.fold(startDate)(f)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement