Rewrite Ordering.kt in new DSL

This commit is contained in:
Ilya Gorbunov
2017-11-05 07:27:08 +03:00
parent ff336cdf84
commit d2e1baa9d4
2 changed files with 121 additions and 78 deletions
@@ -14,7 +14,7 @@ fun main(args: Array<String>) {
val templateGroups = sequenceOf<TemplateGroup>( val templateGroups = sequenceOf<TemplateGroup>(
Elements, Elements,
Filtering, Filtering,
// Ordering, Ordering,
// ArrayOps, // ArrayOps,
// Snapshots, // Snapshots,
// Mapping, // Mapping,
@@ -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 package templates
import templates.Family.* import templates.Family.*
import templates.SequenceClass.* import templates.SequenceClass.*
fun ordering(): List<GenericFunction> { object Ordering : TemplateGroupBase() {
val templates = arrayListOf<GenericFunction>()
templates add f("reverse()") { val f_reverse = fn("reverse()") {
doc { f -> "Reverses ${f.element.pluralize()} in the ${f.collection} in-place." } include(Lists, InvariantArraysOfObjects, ArraysOfPrimitives)
only(Lists, InvariantArraysOfObjects, ArraysOfPrimitives) } builder {
customReceiver(Lists) { "MutableList<T>" } doc { "Reverses ${f.element.pluralize()} in the ${f.collection} in-place." }
returns { "Unit" } returns("Unit")
body { body {
""" """
val midPoint = (size / 2) - 1 val midPoint = (size / 2) - 1
@@ -24,12 +39,19 @@ fun ordering(): List<GenericFunction> {
} }
""" """
} }
body(Platform.JVM, Lists) { """java.util.Collections.reverse(this)""" } specialFor(Lists) {
receiver("MutableList<T>")
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." } doc { "Returns a list with elements in reversed order." }
returns { "List<T>" } returns("List<T>")
body { body {
""" """
if (this is Collection && size <= 1) return toList() if (this is Collection && size <= 1) return toList()
@@ -48,24 +70,20 @@ fun ordering(): List<GenericFunction> {
""" """
} }
doc(CharSequences, Strings) { f -> "Returns a ${f.collection} with characters in reversed order." } specialFor(CharSequences, Strings) {
returns(CharSequences, Strings) { "SELF" } returns("SELF")
body(CharSequences) { f -> doc { "Returns a ${f.collection} with characters in reversed order." }
"""
return StringBuilder(this).reverse()
"""
}
inline(Strings) { Inline.Only }
body(Strings) {
"return (this as CharSequence).reversed().toString()"
} }
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." } doc { "Returns an array with elements of this array in reversed order." }
only(InvariantArraysOfObjects, ArraysOfPrimitives)
returns("SELF") returns("SELF")
body(InvariantArraysOfObjects) { body(InvariantArraysOfObjects) {
""" """
@@ -89,8 +107,10 @@ fun ordering(): List<GenericFunction> {
} }
} }
templates add f("sorted()") { val f_sorted = fn("sorted()") {
includeDefault()
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
} builder {
doc { doc {
""" """
@@ -120,11 +140,13 @@ fun ordering(): List<GenericFunction> {
""" """
} }
returns("SELF", Sequences) specialFor(Sequences) {
doc(Sequences) { returns("SELF")
"Returns a sequence that yields elements of this sequence sorted according to their natural sort order." 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) { body(Sequences) {
""" """
return object : Sequence<T> { return object : Sequence<T> {
@@ -138,15 +160,16 @@ fun ordering(): List<GenericFunction> {
} }
} }
templates add f("sortedArray()") { val f_sortedArray = fn("sortedArray()") {
only(InvariantArraysOfObjects, ArraysOfPrimitives) include(InvariantArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
} builder {
doc { doc {
"Returns an array with all elements of this array sorted according to their natural sort order." "Returns an array with all elements of this array sorted according to their natural sort order."
} }
typeParam("T : Comparable<T>") typeParam("T : Comparable<T>")
returns("SELF") returns("SELF")
body() { body {
""" """
if (isEmpty()) return this if (isEmpty()) return this
return this.copyOf().apply { sort() } return this.copyOf().apply { sort() }
@@ -154,13 +177,16 @@ fun ordering(): List<GenericFunction> {
} }
} }
templates add f("sortDescending()") { val f_sortDescending = fn("sortDescending()") {
only(Lists, ArraysOfObjects, ArraysOfPrimitives) include(Lists, ArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean) 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") returns("Unit")
typeParam("T : Comparable<T>") typeParam("T : Comparable<T>")
customReceiver(Lists) { "MutableList<T>" } specialFor(Lists) {
receiver("MutableList<T>")
}
body { """sortWith(reverseOrder())""" } body { """sortWith(reverseOrder())""" }
body(ArraysOfPrimitives) { body(ArraysOfPrimitives) {
@@ -173,8 +199,10 @@ fun ordering(): List<GenericFunction> {
} }
} }
templates add f("sortedDescending()") { val f_sortedDescending = fn("sortedDescending()") {
includeDefault()
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
} builder {
doc { doc {
""" """
@@ -194,16 +222,19 @@ fun ordering(): List<GenericFunction> {
""" """
} }
returns("SELF", Sequences) specialFor(Sequences) {
doc(Sequences) { returns("SELF")
"Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order." 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()") { val f_sortedArrayDescending = fn("sortedArrayDescending()") {
only(InvariantArraysOfObjects, ArraysOfPrimitives) include(InvariantArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
} builder {
doc { doc {
"Returns an array with all elements of this array sorted descending according to their natural sort order." "Returns an array with all elements of this array sorted descending according to their natural sort order."
} }
@@ -214,9 +245,8 @@ fun ordering(): List<GenericFunction> {
if (isEmpty()) return this if (isEmpty()) return this
return this.copyOf().apply { sortWith(reverseOrder()) } return this.copyOf().apply { sortWith(reverseOrder()) }
""" """
} }
body() { body(ArraysOfPrimitives) {
""" """
if (isEmpty()) return this if (isEmpty()) return this
return this.copyOf().apply { sortDescending() } return this.copyOf().apply { sortDescending() }
@@ -224,7 +254,9 @@ fun ordering(): List<GenericFunction> {
} }
} }
templates add f("sortedWith(comparator: Comparator<in T>)") { val f_sortedWith = fn("sortedWith(comparator: Comparator<in T>)") {
includeDefault()
} builder {
returns("List<T>") returns("List<T>")
doc { doc {
""" """
@@ -252,11 +284,13 @@ fun ordering(): List<GenericFunction> {
""" """
} }
returns("SELF", Sequences) specialFor(Sequences) {
doc(Sequences) { returns("SELF")
"Returns a sequence that yields elements of this sequence sorted according to the specified [comparator]." doc {
"Returns a sequence that yields elements of this sequence sorted according to the specified [comparator]."
}
sequenceClassification(intermediate, stateful)
} }
sequenceClassification(intermediate, stateful)
body(Sequences) { body(Sequences) {
""" """
return object : Sequence<T> { return object : Sequence<T> {
@@ -270,13 +304,14 @@ fun ordering(): List<GenericFunction> {
} }
} }
templates add f("sortedArrayWith(comparator: Comparator<in T>)") { val f_sortedArrayWith = fn("sortedArrayWith(comparator: Comparator<in T>)") {
only(ArraysOfObjects) include(ArraysOfObjects)
} builder {
doc { doc {
"Returns an array with all elements of this array sorted according the specified [comparator]." "Returns an array with all elements of this array sorted according the specified [comparator]."
} }
returns("SELF") returns("SELF")
body() { body {
""" """
if (isEmpty()) return this if (isEmpty()) return this
return this.copyOf().apply { sortWith(comparator) } return this.copyOf().apply { sortWith(comparator) }
@@ -284,19 +319,22 @@ fun ordering(): List<GenericFunction> {
} }
} }
templates add f("sortBy(crossinline selector: (T) -> R?)") { val f_sortBy = fn("sortBy(crossinline selector: (T) -> R?)") {
inline(true) include(Lists, ArraysOfObjects)
only(Lists, ArraysOfObjects) } builder {
doc { f -> """Sorts elements in the ${f.collection} in-place according to natural sort order of the value returned by specified [selector] function.""" } 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") returns("Unit")
typeParam("R : Comparable<R>") typeParam("R : Comparable<R>")
customReceiver(Lists) { "MutableList<T>" } specialFor(Lists) { receiver("MutableList<T>") }
body { """if (size > 1) sortWith(compareBy(selector))""" } body { """if (size > 1) sortWith(compareBy(selector))""" }
} }
templates add f("sortedBy(crossinline selector: (T) -> R?)") { val f_sortedBy = fn("sortedBy(crossinline selector: (T) -> R?)") {
inline(true) includeDefault()
} builder {
inline()
returns("List<T>") returns("List<T>")
typeParam("R : Comparable<R>") typeParam("R : Comparable<R>")
@@ -306,31 +344,35 @@ fun ordering(): List<GenericFunction> {
""" """
} }
returns("SELF", Sequences) specialFor(Sequences) {
doc(Sequences) { returns("SELF")
"Returns a sequence that yields elements of this sequence sorted according to natural sort order of the value returned by specified [selector] function." 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 { body {
"return sortedWith(compareBy(selector))" "return sortedWith(compareBy(selector))"
} }
} }
templates add f("sortByDescending(crossinline selector: (T) -> R?)") { val f_sortByDescending = fn("sortByDescending(crossinline selector: (T) -> R?)") {
inline(true) include(Lists, ArraysOfObjects)
only(Lists, ArraysOfObjects) } builder {
doc { f -> """Sorts elements in the ${f.collection} in-place descending according to natural sort order of the value returned by specified [selector] function.""" } 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") returns("Unit")
typeParam("R : Comparable<R>") typeParam("R : Comparable<R>")
customReceiver(Lists) { "MutableList<T>" } specialFor(Lists) { receiver("MutableList<T>") }
body { body {
"""if (size > 1) sortWith(compareByDescending(selector))""" } """if (size > 1) sortWith(compareByDescending(selector))""" }
} }
templates add f("sortedByDescending(crossinline selector: (T) -> R?)") { val f_sortedByDescending = fn("sortedByDescending(crossinline selector: (T) -> R?)") {
inline(true) includeDefault()
} builder {
inline()
returns("List<T>") returns("List<T>")
typeParam("R : Comparable<R>") typeParam("R : Comparable<R>")
@@ -340,16 +382,17 @@ fun ordering(): List<GenericFunction> {
""" """
} }
returns("SELF", Sequences) specialFor(Sequences) {
doc(Sequences) { returns("SELF")
"Returns a sequence that yields elements of this sequence sorted descending according to natural sort order of the value returned by specified [selector] function." 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 { body {
"return sortedWith(compareByDescending(selector))" "return sortedWith(compareByDescending(selector))"
} }
} }
return templates
} }