Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.
*/
@file:kotlin.jvm.JvmName("ComparisonsKt")
package kotlin
import java.util.Comparator
import kotlin.platform.platformName
/**
* Compares two values using the specified sequence of functions to calculate the result of the comparison.
* The functions are called sequentially, receive the given values [a] and [b] and return [Comparable]
* objects. As soon as the [Comparable] instances returned by a function for [a] and [b] values do not
* compare as equal, the result of that comparison is returned.
*/
@Deprecated("Use selector functions accepting nullable T as a receiver.")
public fun compareValuesBy(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 = fn(a)
val v2 = fn(b)
val diff = compareValues(v1, v2)
if (diff != 0) return diff
}
return 0
}
/**
* Compares two values using the specified functions [selectors] to calculate the result of the comparison.
* The functions are called sequentially, receive the given values [a] and [b] and return [Comparable]
* objects. As soon as the [Comparable] instances returned by a function for [a] and [b] values do not
* compare as equal, the result of that comparison is returned.
*/
// TODO: Remove platformName after M13
@platformName("compareValuesByNullable")
public fun compareValuesBy(a: T, b: T, vararg selectors: (T) -> Comparable<*>?): Int {
require(selectors.size() > 0)
for (fn in selectors) {
val v1 = fn(a)
val v2 = fn(b)
val diff = compareValues(v1, v2)
if (diff != 0) return diff
}
return 0
}
/**
* Compares two values using the specified [selector] function to calculate the result of the comparison.
* The function is applied to the given values [a] and [b] and return [Comparable] objects.
* The result of comparison of these [Comparable] instances is returned.
*/
public inline fun compareValuesBy(a: T, b: T, selector: (T) -> Comparable<*>?): Int {
return compareValues(selector(a), selector(b))
}
/**
* Compares two values using the specified [selector] function to calculate the result of the comparison.
* The function is applied to the given values [a] and [b] and return objects of type K which are then being
* compared with the given [comparator].
*/
public inline fun compareValuesBy(a: T, b: T, comparator: Comparator, selector: (T) -> K): Int {
return comparator.compare(selector(a), selector(b))
}
//// Not so useful without type inference for receiver of expression
//// compareValuesWith(v1, v2, compareBy { it.prop1 } thenByDescending { it.prop2 })
///**
// * Compares two values using the specified [comparator].
// */
//@Suppress("NOTHING_TO_INLINE")
//public inline fun compareValuesWith(a: T, b: T, comparator: Comparator): Int = comparator.compare(a, b)
//
/**
* Compares two nullable [Comparable] values. Null is considered less than any value.
*/
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 to calculate a result of comparison.
* The functions are called sequentially, receive the given values `a` and `b` and return [Comparable]
* objects. As soon as the [Comparable] instances returned by a function for `a` and `b` values do not
* compare as equal, the result of that comparison is returned from the [Comparator].
*/
public fun compareBy(vararg selectors: (T) -> Comparable<*>?): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int = compareValuesBy(a, b, *selectors)
}
}
/**
* Creates a comparator using the sequence of functions to calculate a result of comparison.
*/
@Deprecated("Use compareBy() instead", ReplaceWith("compareBy(*functions)"))
public fun comparator(vararg functions: (T) -> Comparable<*>?): Comparator = compareBy(*functions)
/**
* Creates a comparator using the function to transform value to a [Comparable] instance for comparison.
*/
inline public fun compareBy(crossinline selector: (T) -> Comparable<*>?): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int = compareValuesBy(a, b, selector)
}
}
/**
* Creates a comparator using the [selector] function to transform values being compared and then applying
* the specified [comparator] to compare transformed values.
*/
inline public fun compareBy(comparator: Comparator, crossinline selector: (T) -> K): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int = compareValuesBy(a, b, comparator, selector)
}
}
/**
* Creates a descending comparator using the function to transform value to a [Comparable] instance for comparison.
*/
inline public fun compareByDescending(crossinline selector: (T) -> Comparable<*>?): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int = compareValuesBy(b, a, selector)
}
}
/**
* Creates a descending comparator using the [selector] function to transform values being compared and then applying
* the specified [comparator] to compare transformed values.
*
* Note that an order of [comparator] is reversed by this wrapper.
*/
inline public fun compareByDescending(comparator: Comparator, crossinline selector: (T) -> K): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int = compareValuesBy(b, a, comparator, selector)
}
}
/**
* Creates a comparator comparing values after the primary comparator defined them equal. It uses
* the function to transform value to a [Comparable] instance for comparison.
*/
inline public fun Comparator.thenBy(crossinline selector: (T) -> Comparable<*>?): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int {
val previousCompare = [email protected](a, b)
return if (previousCompare != 0) previousCompare else compareValuesBy(a, b, selector)
}
}
}
/**
* Creates a comparator comparing values after the primary comparator defined them equal. It uses
* the [selector] function to transform values and then compares them with the given [comparator].
*/
inline public fun Comparator.thenBy(comparator: Comparator, crossinline selector: (T) -> K): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int {
val previousCompare = [email protected](a, b)
return if (previousCompare != 0) previousCompare else compareValuesBy(a, b, comparator, selector)
}
}
}
/**
* Creates a descending comparator using the primary comparator and
* the function to transform value to a [Comparable] instance for comparison.
*/
inline public fun Comparator.thenByDescending(crossinline selector: (T) -> Comparable<*>?): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int {
val previousCompare = [email protected](a, b)
return if (previousCompare != 0) previousCompare else compareValuesBy(b, a, selector)
}
}
}
/**
* Creates a descending comparator comparing values after the primary comparator defined them equal. It uses
* the [selector] function to transform values and then compares them with the given [comparator].
*/
inline public fun Comparator.thenByDescending(comparator: Comparator, crossinline selector: (T) -> K): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int {
val previousCompare = [email protected](a, b)
return if (previousCompare != 0) previousCompare else compareValuesBy(b, a, comparator, selector)
}
}
}
/**
* Creates a comparator using the function to calculate a result of comparison.
*/
inline public fun comparator(crossinline comparison: (T, T) -> Int): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int = comparison(a, b)
}
}
/**
* Creates a comparator using the primary comparator and function to calculate a result of comparison.
*/
inline public fun Comparator.thenComparator(crossinline comparison: (T, T) -> Int): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int {
val previousCompare = [email protected](a, b)
return if (previousCompare != 0) previousCompare else comparison(a, b)
}
}
}
/**
* Combines this comparator and the given [comparator] such that the latter is applied only
* when the former considered values equal.
*/
public fun Comparator.then(comparator: Comparator): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int {
val previousCompare = [email protected](a, b)
return if (previousCompare != 0) previousCompare else comparator.compare(a, b)
}
}
}
/**
* Combines this comparator and the given [comparator] such that the latter is applied only
* when the former considered values equal.
*/
public fun Comparator.thenDescending(comparator: Comparator): Comparator {
return object : Comparator {
public override fun compare(a: T, b: T): Int {
val previousCompare = [email protected](a, b)
return if (previousCompare != 0) previousCompare else comparator.compare(b, a)
}
}
}
// Not so useful without type inference for receiver of expression
/**
* Extends the given [comparator] of non-nullable values to a comparator of nullable values
* considering `null` value less than any other value.
*/
public fun nullsFirst(comparator: Comparator): Comparator {
return object: Comparator {
override fun compare(a: T?, b: T?): Int {
if (a === b) return 0
if (a == null) return -1
if (b == null) return 1
return comparator.compare(a, b)
}
}
}
/**
* Provides a comparator of nullable [Comparable] values
* considering `null` value less than any other value.
*/
public fun > nullsFirst(): Comparator {
return object: Comparator {
override fun compare(a: T?, b: T?): Int {
if (a === b) return 0
if (a == null) return -1
if (b == null) return 1
return a.compareTo(b)
}
}
}
/**
* Extends the given [comparator] of non-nullable values to a comparator of nullable values
* considering `null` value greater than any other value.
*/
public fun nullsLast(comparator: Comparator): Comparator {
return object: Comparator {
override fun compare(a: T?, b: T?): Int {
if (a === b) return 0
if (a == null) return 1
if (b == null) return -1
return comparator.compare(a, b)
}
}
}
/**
* Provides a comparator of nullable [Comparable] values
* considering `null` value greater than any other value.
*/
public fun > nullsLast(): Comparator {
return object: Comparator {
override fun compare(a: T?, b: T?): Int {
if (a === b) return 0
if (a == null) return 1
if (b == null) return -1
return a.compareTo(b)
}
}
}
/** Returns a comparator that imposes the reverse ordering of this comparator. */
public fun Comparator.reversed(): Comparator = when (this) {
is ReversedComparator -> this.comparator
else -> ReversedComparator(this)
}
private class ReversedComparator(public val comparator: Comparator): Comparator {
override fun compare(a: T, b: T): Int = comparator.compare(b, a)
}