tried more concise comparison code

This commit is contained in:
James Strachan
2012-09-14 12:05:06 +01:00
parent 1e9b1bdea6
commit 51122a288d
+15 -1
View File
@@ -4,14 +4,23 @@ import java.util.Comparator
import kotlin.test.*
import org.junit.Test
class Item(val name: String, val rating: Int) {
class Item(val name: String, val rating: Int): Comparable<Item> {
fun toString() = "Item($name, $rating)"
public override fun compareTo(other: Item): Int {
return compareBy(this, other, {it.rating}, {it.name})
}
}
class CompareTest {
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
Test fun compareByCompareTo() {
val diff = v1.compareTo(v2)
assertTrue(diff < 0)
}
Test fun compareByNameFirst() {
val diff = compareBy(v1, v2, {(i: Item) -> i.name}, {(i: Item) -> i.rating})
assertTrue(diff > 0)
@@ -22,6 +31,11 @@ class CompareTest {
assertTrue(diff < 0)
}
Test fun compareByRatingFirstUsingItNotation() {
val diff = compareBy(v1, v2, {it.rating}, {it.name})
assertTrue(diff < 0)
}
Test fun compareSameObjectsByRatingFirst() {
val diff = compareBy(v1, v1, {(i: Item) -> i.rating}, {(i: Item) -> i.name})
assertTrue(diff == 0)