Last active
February 10, 2016 08:16
-
-
Save mathieuancelin/7aa8210e7a549551209a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package object enums { | |
trait Day extends Comparable[Day] { | |
def name(): String | |
def ordinal(): Int | |
override def compareTo(o: Day): Int = Integer.compare(this.ordinal(), o.ordinal()) | |
} | |
private[enums] class DayImpl(n: String, i: Int) extends Day { | |
def name(): String = n | |
def ordinal(): Int = i | |
} | |
private val enumValues = Seq( | |
new DayImpl("MON", 0), | |
new DayImpl("TUE", 1), | |
new DayImpl("WED", 2), | |
new DayImpl("THU", 3), | |
new DayImpl("FRI", 4), | |
new DayImpl("SAT", 5), | |
new DayImpl("SUN", 6) | |
) | |
object WorkDay { | |
val MON: Day = enumValues(0) | |
val TUE: Day = enumValues(1) | |
val WED: Day = enumValues(2) | |
val THU: Day = enumValues(3) | |
val FRI: Day = enumValues(4) | |
val SAT: Day = enumValues(5) | |
val SUN: Day = enumValues(6) | |
def find(name: String): Option[Day] = enumValues.find(_.name() == name) | |
def valueOf(name: String): Day = enumValues.find(_.name() == name).get | |
def values(): Seq[Day] = enumValues | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment