diff --git a/libraries/stdlib/src/kotlin/Ordering.kt b/libraries/stdlib/src/kotlin/Ordering.kt new file mode 100644 index 00000000000..c380f18f1c0 --- /dev/null +++ b/libraries/stdlib/src/kotlin/Ordering.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin + +/** + * Helper method for implementing [[Comparable]] methods using a list of functions + * to calculate the values to compare + */ +public fun compareBy(a: T?, b: T?, vararg functions: T.() -> Comparable<*>?): Int { + require(functions.size > 0) + if (a identityEquals b) return 0 + if (a == null) return - 1 + if (b == null) return 1 + for (fn in functions) { + val v1 = a.fn() + val v2 = b.fn() + val diff = compareValues(v1, v2) + if (diff != 0) return diff + } + return 0 +} + +/** + * Compares the two values which may be [[Comparable]] otherwise + * they are compared via [[#equals()]] and if they are not the same then + * the [[#hashCode()]] method is used as the difference + */ +public fun > compareValues(a: T?, b: T?): Int { + if (a identityEquals b) return 0 + if (a == null) return - 1 + if (b == null) return 1 + + return (a as Comparable).compareTo(b) +} diff --git a/libraries/stdlib/src/kotlin/OrderingJVM.kt b/libraries/stdlib/src/kotlin/OrderingJVM.kt index f923bad70ca..880e9b3673a 100644 --- a/libraries/stdlib/src/kotlin/OrderingJVM.kt +++ b/libraries/stdlib/src/kotlin/OrderingJVM.kt @@ -2,37 +2,6 @@ package kotlin import java.util.Comparator -/** -* Helper method for implementing [[Comparable]] methods using a list of functions -* to calculate the values to compare -*/ -public fun compareBy(a: T?, b: T?, vararg functions: T.() -> Comparable<*>?): 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 = a.fn() - val v2 = b.fn() - val diff = compareValues(v1, v2) - if (diff != 0) return diff - } - return 0 -} - -/** - * Compares the two values which may be [[Comparable]] otherwise - * they are compared via [[#equals()]] and if they are not the same then - * the [[#hashCode()]] method is used as the difference - */ -public fun > compareValues(a: T?, b: T?): Int { - if (a === b) return 0 - if (a == null) return - 1 - if (b == null) return 1 - - return (a as Comparable).compareTo(b) -} - /** * Creates a comparator using the sequence of functions used to calculate a value to compare on */