Skip to content

Instantly share code, notes, and snippets.

@retroryan
Created September 3, 2013 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retroryan/6424599 to your computer and use it in GitHub Desktop.
Save retroryan/6424599 to your computer and use it in GitHub Desktop.
sealed abstract class QueryType[T] {
val data: T
}
case class IntQueryType(data: Int) extends QueryType[Int] with Ordered[IntQueryType] {
def compare(that: IntQueryType): Int = this.data - that.data
}
case class StringQueryType(data: String) extends QueryType[String] with Ordered[StringQueryType] {
def compare(that:StringQueryType):Int = this.data.compareTo(that.data)
}
case class DateQueryType(data: Date) extends QueryType[Date] with Ordered[DateQueryType] {
def compare(that:DateQueryType):Int = this.data.compareTo(that.data)
}
object QueryTests {
def testQueryType = {
val map = buildTestMap
val ni1 = map("i1")
val ni2 = map("i2")
(ni1,ni2) match {
case (mi:IntQueryType,mi2:IntQueryType) => println(s"${mi < mi2}")
case (mi:StringQueryType,mi2:StringQueryType) => println(s"${mi < mi2}")
}
}
def buildTestMap = {
//Fails - QueryType is expecting the type parameter
var map = HashMap.empty[String,QueryType]
val i1 = IntQueryType(1)
val i2 = IntQueryType(10)
val i3 = IntQueryType(18)
val i4 = IntQueryType(15)
val s1 = StringQueryType("s1")
val s2 = StringQueryType("s10")
val s3 = StringQueryType("s18")
val s4 = StringQueryType("s15")
val d1 = DateQueryType(new Date(5000))
val d2 = DateQueryType(new Date(7000))
val d3 = DateQueryType(new Date(8000))
val d4 = DateQueryType(new Date(9000))
map += ("i1" -> i1)
map += ("i2" -> i2)
map += ("i3" -> i3)
map += ("i4" -> i4)
map += ("s1" -> s1)
map += ("s2" -> s2)
map += ("s3" -> s3)
map += ("s4" -> s4)
map += ("d1" -> d1)
map += ("d2" -> d2)
map += ("d3" -> d3)
map += ("d4" -> d4)
map
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment