Deprecate 'comparator { ... }' in favor of Comparator SAM-constructor. Provide SAM-like constructor for JS.
This commit is contained in:
@@ -11,6 +11,10 @@ public interface Comparator<T> {
|
|||||||
public fun compare(obj1: T, obj2: T): Int;
|
public fun compare(obj1: T, obj2: T): Int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public inline fun <T> Comparator(crossinline comparison: (T, T) -> Int): Comparator<T> = object : Comparator<T> {
|
||||||
|
override fun compare(obj1: T, obj2: T): Int = comparison(obj1, obj2)
|
||||||
|
}
|
||||||
|
|
||||||
@library
|
@library
|
||||||
public abstract class AbstractCollection<E>() : MutableCollection<E> {
|
public abstract class AbstractCollection<E>() : MutableCollection<E> {
|
||||||
override fun isEmpty(): Boolean = noImpl
|
override fun isEmpty(): Boolean = noImpl
|
||||||
|
|||||||
@@ -182,15 +182,6 @@ inline public fun <T, K> Comparator<T>.thenByDescending(comparator: Comparator<i
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a comparator using the function to calculate a result of comparison.
|
|
||||||
*/
|
|
||||||
inline public fun <T> comparator(crossinline comparison: (T, T) -> Int): Comparator<T> {
|
|
||||||
return object : Comparator<T> {
|
|
||||||
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.
|
* Creates a comparator using the primary comparator and function to calculate a result of comparison.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -194,16 +194,11 @@ inline public fun <T, K> Comparator<T>.thenByDescending(comparator: Comparator<i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a comparator using the function to calculate a result of comparison.
|
* Creates a comparator using the function to calculate a result of comparison.
|
||||||
*/
|
*/
|
||||||
@Deprecated("Use comparator function from kotlin.comparisons package.", ReplaceWith("comparator(comparison)", "kotlin.comparisons.comparator"))
|
@Deprecated("Use Comparator SAM-constructor instead.", ReplaceWith("Comparator(comparison)", "java.util.Comparator"))
|
||||||
inline public fun <T> comparator(crossinline comparison: (T, T) -> Int): Comparator<T> {
|
inline public fun <T> comparator(crossinline comparison: (T, T) -> Int) = Comparator<T> { a, b -> comparison(a, b) }
|
||||||
return object : Comparator<T> {
|
|
||||||
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.
|
* Creates a comparator using the primary comparator and function to calculate a result of comparison.
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ class OrderingTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun sortComparatorThenComparator() {
|
fun sortComparatorThenComparator() {
|
||||||
val comparator = comparator<Item> { a, b -> a.name.compareTo(b.name) }.thenComparator { a, b -> a.rating.compareTo(b.rating) }
|
val comparator = Comparator<Item> { a, b -> a.name.compareTo(b.name) }.thenComparator { a, b -> a.rating.compareTo(b.rating) }
|
||||||
|
|
||||||
val diff = comparator.compare(v1, v2)
|
val diff = comparator.compare(v1, v2)
|
||||||
assertTrue(diff > 0)
|
assertTrue(diff > 0)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package test.collections
|
package test.collections
|
||||||
|
|
||||||
import java.util.Collections
|
import java.util.*
|
||||||
import java.util.ArrayList
|
|
||||||
|
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
import org.junit.Test as test
|
import org.junit.Test as test
|
||||||
@@ -13,7 +12,7 @@ class JavautilCollectionsTest {
|
|||||||
val TEST_LIST = arrayOf(2, 0, 9, 7, 1).toList()
|
val TEST_LIST = arrayOf(2, 0, 9, 7, 1).toList()
|
||||||
val SORTED_TEST_LIST = arrayOf(0, 1, 2, 7, 9).toList()
|
val SORTED_TEST_LIST = arrayOf(0, 1, 2, 7, 9).toList()
|
||||||
val MAX_ELEMENT = 9
|
val MAX_ELEMENT = 9
|
||||||
val COMPARATOR = comparator { x: Int, y: Int -> if (x > y) 1 else if (x < y) -1 else 0 }
|
val COMPARATOR = Comparator { x: Int, y: Int -> if (x > y) 1 else if (x < y) -1 else 0 }
|
||||||
|
|
||||||
@test fun maxWithComparator() {
|
@test fun maxWithComparator() {
|
||||||
assertEquals(MAX_ELEMENT, Collections.max(TEST_LIST, COMPARATOR))
|
assertEquals(MAX_ELEMENT, Collections.max(TEST_LIST, COMPARATOR))
|
||||||
|
|||||||
Reference in New Issue
Block a user