From 9c3d1071cd44a1a5d49d571d958b7923a89c7fd8 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Fri, 14 Sep 2012 12:44:09 +0100 Subject: [PATCH] added more DRY compareBy function and helper class --- libraries/stdlib/src/kotlin/OrderingJVM.kt | 12 ++++---- libraries/stdlib/test/CompareTest.kt | 30 +++++++------------ .../org/jetbrains/kotlin/doc/KDocConfig.kt | 2 +- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/libraries/stdlib/src/kotlin/OrderingJVM.kt b/libraries/stdlib/src/kotlin/OrderingJVM.kt index a59511e84af..3a2d3ccb98a 100644 --- a/libraries/stdlib/src/kotlin/OrderingJVM.kt +++ b/libraries/stdlib/src/kotlin/OrderingJVM.kt @@ -6,14 +6,14 @@ import java.util.Comparator * Helper method for implementing [[Comparable]] methods using a list of functions * to calculate the values to compare */ -inline fun compareBy(a: T?, b: T?, vararg functions: Function1): Int { +inline fun compareBy(a: T?, b: T?, vararg functions: T.() -> Any?): Int { require(functions.size > 0) if (a === b) return 0 if (a == null) return - 1 if (b == null) return 1 for (fn in functions) { - val v1 = fn(a) - val v2 = fn(b) + val v1 = a.fn() + val v2 = b.fn() val diff = compareValues(v1, v2) if (diff != 0) return diff } @@ -47,12 +47,12 @@ public inline fun compareValues(a: T?, b: T?): Int { /** * Creates a comparator using the sequence of functions used to calculate a value to compare on */ -public inline fun comparator(vararg functions: Function1): Comparator { - return FunctionComparator(functions) +public inline fun comparator(vararg val functions: T.() -> Any?): Comparator { + return FunctionComparator(*functions) } -private class FunctionComparator(val functions: Array>): Comparator { +private class FunctionComparator(vararg val functions: T.() -> Any?): Comparator { public override fun toString(): String { return "FunctionComparator${functions.toList()}" diff --git a/libraries/stdlib/test/CompareTest.kt b/libraries/stdlib/test/CompareTest.kt index 71739f2ad2b..8f3b9028265 100644 --- a/libraries/stdlib/test/CompareTest.kt +++ b/libraries/stdlib/test/CompareTest.kt @@ -8,7 +8,7 @@ class Item(val name: String, val rating: Int): Comparable { fun toString() = "Item($name, $rating)" public override fun compareTo(other: Item): Int { - return compareBy(this, other, {it.rating}, {it.name}) + return compareBy(this, other, { rating }, { name }) } } @@ -22,43 +22,35 @@ class CompareTest { } Test fun compareByNameFirst() { - val diff = compareBy(v1, v2, {(i: Item) -> i.name}, {(i: Item) -> i.rating}) + val diff = compareBy(v1, v2, { name }, { rating }) assertTrue(diff > 0) } Test fun compareByRatingFirst() { - val diff = compareBy(v1, v2, {(i: Item) -> i.rating}, {(i: Item) -> i.name}) - assertTrue(diff < 0) - } - - Test fun compareByRatingFirstUsingItNotation() { - val diff = compareBy(v1, v2, {it.rating}, {it.name}) + val diff = compareBy(v1, v2, { rating }, { name }) assertTrue(diff < 0) } Test fun compareSameObjectsByRatingFirst() { - val diff = compareBy(v1, v1, {(i: Item) -> i.rating}, {(i: Item) -> i.name}) + val diff = compareBy(v1, v1, { rating }, { name }) assertTrue(diff == 0) } Test fun sortUsingComparatorHelperMethod() { - val c = comparator({it.rating}, {it.name}) + val c = comparator({ rating }, { name }) println("Created comparator $c") - todo { - // TODO needs KT-729 before this code works - val diff = c.compare(v1, v2) - assertTrue(diff < 0) - val items = arrayList(v1, v2) - items.sort(c) - println("Sorted list in rating order $items") - } + val diff = c.compare(v1, v2) + assertTrue(diff < 0) + val items = arrayList(v1, v2) + items.sort(c) + println("Sorted list in rating order $items") } Test fun sortUsingCustomComparator() { val c = object : Comparator{ override fun compare(o1: Item?, o2: Item?): Int { - return compareBy(o1, o2, {(it: Item) -> it.name}, {(it: Item) -> it.rating}) + return compareBy(o1, o2, { name }, { rating }) } override fun equals(obj: Any?): Boolean { return this == obj diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocConfig.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocConfig.kt index 0561dbf6e90..a86550e3fbe 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocConfig.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocConfig.kt @@ -115,7 +115,7 @@ class KDocConfig() { private class LongestFirstStringComparator() : Comparator { public override fun compare(s1: String?, s2: String?): Int { - return compareBy(s1, s2, { (s: String) -> s.length() }, { (s: String) -> s }) + return compareBy(s1, s2, { length() }, { this }) } public override fun equals(obj : Any?) : Boolean {