From d2e1baa9d497996ffa2713bef0623675dceac9c3 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sun, 5 Nov 2017 07:27:08 +0300 Subject: [PATCH] Rewrite Ordering.kt in new DSL --- .../src/generators/GenerateStandardLib.kt | 2 +- .../src/templates/Ordering.kt | 197 +++++++++++------- 2 files changed, 121 insertions(+), 78 deletions(-) diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index 364a4759e52..7e1a80b7453 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -14,7 +14,7 @@ fun main(args: Array) { val templateGroups = sequenceOf( Elements, Filtering, -// Ordering, + Ordering, // ArrayOps, // Snapshots, // Mapping, diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index 401d624d794..44f85359f68 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -1,16 +1,31 @@ +/* + * Copyright 2010-2017 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 templates import templates.Family.* import templates.SequenceClass.* -fun ordering(): List { - val templates = arrayListOf() +object Ordering : TemplateGroupBase() { - templates add f("reverse()") { - doc { f -> "Reverses ${f.element.pluralize()} in the ${f.collection} in-place." } - only(Lists, InvariantArraysOfObjects, ArraysOfPrimitives) - customReceiver(Lists) { "MutableList" } - returns { "Unit" } + val f_reverse = fn("reverse()") { + include(Lists, InvariantArraysOfObjects, ArraysOfPrimitives) + } builder { + doc { "Reverses ${f.element.pluralize()} in the ${f.collection} in-place." } + returns("Unit") body { """ val midPoint = (size / 2) - 1 @@ -24,12 +39,19 @@ fun ordering(): List { } """ } - body(Platform.JVM, Lists) { """java.util.Collections.reverse(this)""" } + specialFor(Lists) { + receiver("MutableList") + on(Platform.JVM) { + body { """java.util.Collections.reverse(this)""" } + } + } } - templates add f("reversed()") { + val f_reversed = fn("reversed()") { + include(Iterables, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings) + } builder { doc { "Returns a list with elements in reversed order." } - returns { "List" } + returns("List") body { """ if (this is Collection && size <= 1) return toList() @@ -48,24 +70,20 @@ fun ordering(): List { """ } - doc(CharSequences, Strings) { f -> "Returns a ${f.collection} with characters in reversed order." } - returns(CharSequences, Strings) { "SELF" } - body(CharSequences) { f -> - """ - return StringBuilder(this).reverse() - """ - } - inline(Strings) { Inline.Only } - body(Strings) { - "return (this as CharSequence).reversed().toString()" + specialFor(CharSequences, Strings) { + returns("SELF") + doc { "Returns a ${f.collection} with characters in reversed order." } } + body(CharSequences) { "return StringBuilder(this).reverse()" } + specialFor(Strings) { inlineOnly() } + body(Strings) { "return (this as CharSequence).reversed().toString()" } - exclude(Sequences) } - templates add f("reversedArray()") { + val f_reversedArray = fn("reversedArray()") { + include(InvariantArraysOfObjects, ArraysOfPrimitives) + } builder { doc { "Returns an array with elements of this array in reversed order." } - only(InvariantArraysOfObjects, ArraysOfPrimitives) returns("SELF") body(InvariantArraysOfObjects) { """ @@ -89,8 +107,10 @@ fun ordering(): List { } } - templates add f("sorted()") { + val f_sorted = fn("sorted()") { + includeDefault() exclude(PrimitiveType.Boolean) + } builder { doc { """ @@ -120,11 +140,13 @@ fun ordering(): List { """ } - returns("SELF", Sequences) - doc(Sequences) { - "Returns a sequence that yields elements of this sequence sorted according to their natural sort order." + specialFor(Sequences) { + returns("SELF") + doc { + "Returns a sequence that yields elements of this sequence sorted according to their natural sort order." + } + sequenceClassification(intermediate, stateful) } - sequenceClassification(intermediate, stateful) body(Sequences) { """ return object : Sequence { @@ -138,15 +160,16 @@ fun ordering(): List { } } - templates add f("sortedArray()") { - only(InvariantArraysOfObjects, ArraysOfPrimitives) + val f_sortedArray = fn("sortedArray()") { + include(InvariantArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) + } builder { doc { "Returns an array with all elements of this array sorted according to their natural sort order." } typeParam("T : Comparable") returns("SELF") - body() { + body { """ if (isEmpty()) return this return this.copyOf().apply { sort() } @@ -154,13 +177,16 @@ fun ordering(): List { } } - templates add f("sortDescending()") { - only(Lists, ArraysOfObjects, ArraysOfPrimitives) + val f_sortDescending = fn("sortDescending()") { + include(Lists, ArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) - doc { f -> """Sorts elements in the ${f.collection} in-place descending according to their natural sort order.""" } + } builder { + doc { """Sorts elements in the ${f.collection} in-place descending according to their natural sort order.""" } returns("Unit") typeParam("T : Comparable") - customReceiver(Lists) { "MutableList" } + specialFor(Lists) { + receiver("MutableList") + } body { """sortWith(reverseOrder())""" } body(ArraysOfPrimitives) { @@ -173,8 +199,10 @@ fun ordering(): List { } } - templates add f("sortedDescending()") { + val f_sortedDescending = fn("sortedDescending()") { + includeDefault() exclude(PrimitiveType.Boolean) + } builder { doc { """ @@ -194,16 +222,19 @@ fun ordering(): List { """ } - returns("SELF", Sequences) - doc(Sequences) { - "Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order." + specialFor(Sequences) { + returns("SELF") + doc { + "Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order." + } + sequenceClassification(intermediate, stateful) } - sequenceClassification(intermediate, stateful) } - templates add f("sortedArrayDescending()") { - only(InvariantArraysOfObjects, ArraysOfPrimitives) + val f_sortedArrayDescending = fn("sortedArrayDescending()") { + include(InvariantArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) + } builder { doc { "Returns an array with all elements of this array sorted descending according to their natural sort order." } @@ -214,9 +245,8 @@ fun ordering(): List { if (isEmpty()) return this return this.copyOf().apply { sortWith(reverseOrder()) } """ - } - body() { + body(ArraysOfPrimitives) { """ if (isEmpty()) return this return this.copyOf().apply { sortDescending() } @@ -224,7 +254,9 @@ fun ordering(): List { } } - templates add f("sortedWith(comparator: Comparator)") { + val f_sortedWith = fn("sortedWith(comparator: Comparator)") { + includeDefault() + } builder { returns("List") doc { """ @@ -252,11 +284,13 @@ fun ordering(): List { """ } - returns("SELF", Sequences) - doc(Sequences) { - "Returns a sequence that yields elements of this sequence sorted according to the specified [comparator]." + specialFor(Sequences) { + returns("SELF") + doc { + "Returns a sequence that yields elements of this sequence sorted according to the specified [comparator]." + } + sequenceClassification(intermediate, stateful) } - sequenceClassification(intermediate, stateful) body(Sequences) { """ return object : Sequence { @@ -270,13 +304,14 @@ fun ordering(): List { } } - templates add f("sortedArrayWith(comparator: Comparator)") { - only(ArraysOfObjects) + val f_sortedArrayWith = fn("sortedArrayWith(comparator: Comparator)") { + include(ArraysOfObjects) + } builder { doc { "Returns an array with all elements of this array sorted according the specified [comparator]." } returns("SELF") - body() { + body { """ if (isEmpty()) return this return this.copyOf().apply { sortWith(comparator) } @@ -284,19 +319,22 @@ fun ordering(): List { } } - templates add f("sortBy(crossinline selector: (T) -> R?)") { - inline(true) - only(Lists, ArraysOfObjects) - doc { f -> """Sorts elements in the ${f.collection} in-place according to natural sort order of the value returned by specified [selector] function.""" } + val f_sortBy = fn("sortBy(crossinline selector: (T) -> R?)") { + include(Lists, ArraysOfObjects) + } builder { + inline() + doc { """Sorts elements in the ${f.collection} in-place according to natural sort order of the value returned by specified [selector] function.""" } returns("Unit") typeParam("R : Comparable") - customReceiver(Lists) { "MutableList" } + specialFor(Lists) { receiver("MutableList") } body { """if (size > 1) sortWith(compareBy(selector))""" } } - templates add f("sortedBy(crossinline selector: (T) -> R?)") { - inline(true) + val f_sortedBy = fn("sortedBy(crossinline selector: (T) -> R?)") { + includeDefault() + } builder { + inline() returns("List") typeParam("R : Comparable") @@ -306,31 +344,35 @@ fun ordering(): List { """ } - returns("SELF", Sequences) - doc(Sequences) { - "Returns a sequence that yields elements of this sequence sorted according to natural sort order of the value returned by specified [selector] function." + specialFor(Sequences) { + returns("SELF") + doc { + "Returns a sequence that yields elements of this sequence sorted according to natural sort order of the value returned by specified [selector] function." + } + sequenceClassification(intermediate, stateful) } - sequenceClassification(intermediate, stateful) - body { "return sortedWith(compareBy(selector))" } } - templates add f("sortByDescending(crossinline selector: (T) -> R?)") { - inline(true) - only(Lists, ArraysOfObjects) - doc { f -> """Sorts elements in the ${f.collection} in-place descending according to natural sort order of the value returned by specified [selector] function.""" } + val f_sortByDescending = fn("sortByDescending(crossinline selector: (T) -> R?)") { + include(Lists, ArraysOfObjects) + } builder { + inline() + doc { """Sorts elements in the ${f.collection} in-place descending according to natural sort order of the value returned by specified [selector] function.""" } returns("Unit") typeParam("R : Comparable") - customReceiver(Lists) { "MutableList" } + specialFor(Lists) { receiver("MutableList") } body { """if (size > 1) sortWith(compareByDescending(selector))""" } } - templates add f("sortedByDescending(crossinline selector: (T) -> R?)") { - inline(true) + val f_sortedByDescending = fn("sortedByDescending(crossinline selector: (T) -> R?)") { + includeDefault() + } builder { + inline() returns("List") typeParam("R : Comparable") @@ -340,16 +382,17 @@ fun ordering(): List { """ } - returns("SELF", Sequences) - doc(Sequences) { - "Returns a sequence that yields elements of this sequence sorted descending according to natural sort order of the value returned by specified [selector] function." + specialFor(Sequences) { + returns("SELF") + doc { + "Returns a sequence that yields elements of this sequence sorted descending according to natural sort order of the value returned by specified [selector] function." + } + sequenceClassification(intermediate, stateful) } - sequenceClassification(intermediate, stateful) body { "return sortedWith(compareByDescending(selector))" } } - return templates -} \ No newline at end of file +}