added a couple of helper functions to make it easier to compare values, implement Comparable and create Comparators

This commit is contained in:
James Strachan
2012-03-26 10:53:28 +01:00
parent d0e7248ec1
commit d8c3a8f55c
3 changed files with 109 additions and 9 deletions
+68
View File
@@ -0,0 +1,68 @@
package kotlin
import java.lang.Iterable
import java.util.Comparator
import java.util.Comparator
/**
* Helper method for implementing [[Comparable<T>]] methods using a list of functions
* to calculate the values to compare
*/
inline fun <T> compareBy(a: T?, b: T?, vararg functions: Function1<T,Any?>): Int {
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 diff = compareValues(v1, v2)
if (diff != null) return diff
}
return compareValues(a, b)
}
/**
* Compares the two values which may be [[Comparable]] otherwise
* the [[#hashCode()]] method is used as the difference
*/
inline fun <T> compareValues(a: T?, b: T?): Int {
if (a == null) return - 1
if (b == null) return 1
if (a is Comparable<T>) {
return a.compareTo(b)
}
if (a == b) {
return 0
}
if (a is Object && b is Object) {
val diff = a.hashCode() - b.hashCode()
return if (diff == 0) 1 else diff
} else {
// TODO???
return 1
}
}
/**
* Creates a comparator using the sequence of functions used to calcualte a value to compare on
*/
inline fun <T> comparator(vararg functions: Function1<T,Any?>): Comparator<T> {
return FunctionComparator<T>(functions)
}
private class FunctionComparator<T>(val functions: Array<Function1<T,Any?>>): Comparator<T> {
fun toString(): String {
return "FunctionComparator${functions.toList()}"
}
override fun compare(o1: T?, o2: T?): Int {
// TODO compile error
//return compareBy<T>(o1, o2, functions)
throw UnsupportedOperationException("TODO")
}
override fun equals(obj: Any?): Boolean {
return this == obj
}
}