alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (time.scala)

This example Scala source code file (time.scala) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

duration, enum, equal, int, localdate, monoid, month, order, period, timeinstances, timeinstances0, year, yearmonth

The time.scala Scala example source code

package scalaz
package std.java

import java.time._

// https://github.com/scala-js/scala-js-java-time/issues/7
trait TimeInstances extends TimeInstances0 {

  private[this] def orderFromInt[A](f: (A, A) => Int): Order[A] = new Order[A] {
    def order(x: A, y: A) = Ordering.fromInt(f(x, y))
  }

  implicit val durationInstance: Monoid[Duration] with Order[Duration] =
    new Monoid[Duration] with Order[Duration] {
      override def zero = Duration.ZERO
      override def append(f1: Duration, f2: => Duration) = f1 plus f2
      override def order(a1: Duration, a2: Duration) = Ordering.fromInt(a1 compareTo a2)
    }

  implicit val periodInstance: Monoid[Period] with Equal[Period] =
    new Monoid[Period] with Equal[Period] {
      override def zero = Period.ZERO
      override def append(f1: Period, f2: => Period) = f1 plus f2
      override def equal(a1: Period, a2: Period) = a1 == a2
    }

  implicit val yearMonthInstance: Enum[YearMonth] = new Enum[YearMonth] {
    override def pred(a: YearMonth) = a.minusMonths(1)
    override def succ(a: YearMonth) = a.plusMonths(1)
    override def order(x: YearMonth, y: YearMonth) =
      Ordering.fromInt(x compareTo y)
  }

  implicit val monthDayInstance: Order[MonthDay] = orderFromInt[MonthDay](_ compareTo _)
  implicit val localTimeInstance: Order[LocalTime] = orderFromInt[LocalTime](_ compareTo _)

  implicit val yearInstance: Enum[Year] = new Enum[Year] {
    override def pred(a: Year) = a.minusYears(1)
    override def succ(a: Year) = a.plusYears(1)
    override def order(x: Year, y: Year) =
      Ordering.fromInt(x compareTo y)
  }

  implicit val localDateInstance: Enum[LocalDate] = new Enum[LocalDate] {
    override def pred(a: LocalDate) = a.minusDays(1)
    override def succ(a: LocalDate) = a.plusDays(1)
    override def order(x: LocalDate, y: LocalDate) =
      Ordering.fromInt(x compareTo y)
  }

  implicit val monthInstance: Enum[Month] = new Enum[Month] {
    override val max = Some(Month.DECEMBER)
    override val min = Some(Month.JANUARY)
    override def pred(a: Month): Month = a.minus(1)
    override def succ(a: Month): Month = a.plus(1)
    override def order(x: Month, y: Month) =
      Ordering.fromInt(x compareTo y)
  }

}

object time extends TimeInstances

Other Scala examples (source code examples)

Here is a short list of links related to this Scala time.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.