Introduce minOf/maxOf in kotlin.comparisons. #KT-7417

This commit is contained in:
Ilya Gorbunov
2016-11-16 07:33:03 +03:00
parent 35d433160e
commit 51c24a0c3c
8 changed files with 1011 additions and 1 deletions
@@ -1699,6 +1699,14 @@ public final class kotlin/comparisons/ComparisonsKt {
public static final fun compareBy ([Lkotlin/jvm/functions/Function1;)Ljava/util/Comparator;
public static final fun compareValues (Ljava/lang/Comparable;Ljava/lang/Comparable;)I
public static final fun compareValuesBy (Ljava/lang/Object;Ljava/lang/Object;[Lkotlin/jvm/functions/Function1;)I
public static final fun maxOf (Ljava/lang/Comparable;Ljava/lang/Comparable;)Ljava/lang/Comparable;
public static final fun maxOf (Ljava/lang/Comparable;Ljava/lang/Comparable;Ljava/lang/Comparable;)Ljava/lang/Comparable;
public static final fun maxOf (Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/util/Comparator;)Ljava/lang/Object;
public static final fun maxOf (Ljava/lang/Object;Ljava/lang/Object;Ljava/util/Comparator;)Ljava/lang/Object;
public static final fun minOf (Ljava/lang/Comparable;Ljava/lang/Comparable;)Ljava/lang/Comparable;
public static final fun minOf (Ljava/lang/Comparable;Ljava/lang/Comparable;Ljava/lang/Comparable;)Ljava/lang/Comparable;
public static final fun minOf (Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/util/Comparator;)Ljava/lang/Object;
public static final fun minOf (Ljava/lang/Object;Ljava/lang/Object;Ljava/util/Comparator;)Ljava/lang/Object;
public static final fun naturalOrder ()Ljava/util/Comparator;
public static final fun nullsFirst (Ljava/util/Comparator;)Ljava/util/Comparator;
public static final fun nullsLast (Ljava/util/Comparator;)Ljava/util/Comparator;
@@ -114,6 +114,176 @@ fun comparables(): List<GenericFunction> {
}
}
templates add f("minOf(a: T, b: T)") {
sourceFile(SourceFile.Comparisons)
only(Primitives, Generic)
only(numericPrimitives)
since("1.1")
typeParam("T: Comparable<T>")
returns("T")
customReceiver("")
inline(Primitives) { Inline.Only }
doc {
"""
Returns the smaller of two values.
If values are equal, returns the first one.
"""
}
doc(Primitives) {
"""Returns the smaller of two values."""
}
bodyForTypes(Primitives, PrimitiveType.Byte, PrimitiveType.Short) { p ->
"return Math.min(a.toInt(), b.toInt()).to$p()"
}
body(Primitives) {
"return Math.min(a, b)"
}
body(Generic) {
"return if (a <= b) a else b"
}
}
templates add f("minOf(a: T, b: T, c: T)") {
sourceFile(SourceFile.Comparisons)
only(Primitives, Generic)
only(numericPrimitives)
since("1.1")
typeParam("T: Comparable<T>")
returns("T")
customReceiver("")
inline(Primitives) { Inline.Only }
doc {
"""
Returns the smaller of three values.
"""
}
bodyForTypes(Primitives, PrimitiveType.Byte, PrimitiveType.Short) { p ->
"return Math.min(a.toInt(), Math.min(b.toInt(), c.toInt())).to$p()"
}
body {
"return minOf(a, minOf(b, c))"
}
}
templates add f("minOf(a: T, b: T, comparator: Comparator<in T>)") {
sourceFile(SourceFile.Comparisons)
only(Generic)
since("1.1")
returns("T")
customReceiver("")
doc {
"""
Returns the smaller of two values according to the order specified by the given [comparator].
If values are equal, returns the first one.
"""
}
body {
"return if (comparator.compare(a, b) <= 0) a else b"
}
}
templates add f("minOf(a: T, b: T, c: T, comparator: Comparator<in T>)") {
sourceFile(SourceFile.Comparisons)
only(Generic)
since("1.1")
returns("T")
customReceiver("")
doc {
"""
Returns the smaller of three values according to the order specified by the given [comparator].
"""
}
body {
"return minOf(a, minOf(b, c, comparator), comparator)"
}
}
templates add f("maxOf(a: T, b: T)") {
sourceFile(SourceFile.Comparisons)
only(Primitives, Generic)
only(numericPrimitives)
since("1.1")
typeParam("T: Comparable<T>")
returns("T")
customReceiver("")
inline(Primitives) { Inline.Only }
doc {
"""
Returns the greater of two values.
If values are equal, returns the first one.
"""
}
doc(Primitives) {
"""Returns the greater of two values."""
}
bodyForTypes(Primitives, PrimitiveType.Byte, PrimitiveType.Short) { p ->
"return Math.max(a.toInt(), b.toInt()).to$p()"
}
body(Primitives) {
"return Math.max(a, b)"
}
body(Generic) {
"return if (a >= b) a else b"
}
}
templates add f("maxOf(a: T, b: T, c: T)") {
sourceFile(SourceFile.Comparisons)
only(Primitives, Generic)
only(numericPrimitives)
since("1.1")
typeParam("T: Comparable<T>")
returns("T")
customReceiver("")
inline(Primitives) { Inline.Only }
doc {
"""
Returns the greater of three values.
"""
}
bodyForTypes(Primitives, PrimitiveType.Byte, PrimitiveType.Short) { p ->
"return Math.max(a.toInt(), Math.max(b.toInt(), c.toInt())).to$p()"
}
body {
"return maxOf(a, maxOf(b, c))"
}
}
templates add f("maxOf(a: T, b: T, comparator: Comparator<in T>)") {
sourceFile(SourceFile.Comparisons)
only(Generic)
since("1.1")
returns("T")
customReceiver("")
doc {
"""
Returns the greater of two values according to the order specified by the given [comparator].
If values are equal, returns the first one.
"""
}
body {
"return if (comparator.compare(a, b) >= 0) a else b"
}
}
templates add f("maxOf(a: T, b: T, c: T, comparator: Comparator<in T>)") {
sourceFile(SourceFile.Comparisons)
only(Generic)
since("1.1")
returns("T")
customReceiver("")
doc {
"""
Returns the greater of three values according to the order specified by the given [comparator].
"""
}
body {
"return maxOf(a, maxOf(b, c, comparator), comparator)"
}
}
templates add f("coerceIn(minimumValue: SELF, maximumValue: SELF)") {
sourceFile(SourceFile.Ranges)
only(Primitives, Generic)
@@ -8,6 +8,7 @@ enum class SourceFile(jvmClassName: String? = null, val multifile: Boolean = tru
Maps(packageName = "kotlin.collections"),
Sequences(packageName = "kotlin.sequences"),
Ranges(packageName = "kotlin.ranges"),
Comparisons(packageName = "kotlin.comparisons"),
Strings(packageName = "kotlin.text"),
Misc(),
;