Skip to content

Instantly share code, notes, and snippets.

@grahamar
Last active March 19, 2018 16:45
Show Gist options
  • Save grahamar/322975034e4278d24345e1a80d037e82 to your computer and use it in GitHub Desktop.
Save grahamar/322975034e4278d24345e1a80d037e82 to your computer and use it in GitHub Desktop.
TreeSet Issue
object Item {
implicit val ord = new Ordering[Item] {
def compare(i1: Item, i2: Item): Int = {
if (i1.equals(i2)) {
0
} else {
val order = i1.added.compare(i2.added)
if (order == 0) 1 else order
}
}
}
}
case class Item(group: String, something: String, other: String, added: Long) {
override def equals(other: scala.Any): Boolean = {
other match {
case i2: Item => group == i2.group && something == i2.something && other == i2.other
case _ => false
}
}
override def toString: String = s"Item($group, $something, $other, $added)"
}
import java.time.Instant
import org.scalatest.{ MustMatchers, WordSpecLike }
import org.scalatest.mockito.MockitoSugar
class ItemSpec extends WordSpecLike with MustMatchers with MockitoSugar {
"Item" should {
"be unique" in {
val backlog = items("group1", 1)
println(backlog.mkString("\n"))
backlog.size mustEqual 1
println("===================================================")
val backlog2 = backlog ++ items("group2", 1)
println(backlog2.mkString("\n"))
backlog2.size mustEqual 2
println("===================================================")
val backlog3 = backlog2 ++ items("group3", 1)
println(backlog3.mkString("\n"))
backlog3.size mustEqual 3
println("===================================================")
val backlog4 = backlog3 ++ items("group4", 1)
println(backlog4.mkString("\n"))
backlog4.size mustEqual 4
println("===================================================")
val backlog5 = backlog4 ++ items("group1", 1)
println(backlog5.mkString("\n"))
backlog5.size mustEqual 4
println("===================================================")
}
}
private def items(group: String, num: Int) = TreeSet(Seq.tabulate(num)(i =>
Item(group, s"something$i", "other", Instant.now().toEpochMilli)
): _*)
}
Item(group1, something0, other, 1521477246485)
===================================================
Item(group1, something0, other, 1521477246485)
Item(group2, something0, other, 1521477246491)
===================================================
Item(group1, something0, other, 1521477246485)
Item(group2, something0, other, 1521477246491)
Item(group3, something0, other, 1521477246492)
===================================================
Item(group1, something0, other, 1521477246485)
Item(group2, something0, other, 1521477246491)
Item(group3, something0, other, 1521477246492)
Item(group4, something0, other, 1521477246492)
===================================================
Item(group1, something0, other, 1521477246485)
Item(group2, something0, other, 1521477246491)
Item(group3, something0, other, 1521477246492)
Item(group4, something0, other, 1521477246492)
Item(group1, something0, other, 1521477246492)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment