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>(
Elements,
Filtering,
// Ordering,
Ordering,
// ArrayOps,
// Snapshots,
// 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
import templates.Family.*
import templates.SequenceClass.*
fun ordering(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
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<T>" }
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<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." }
returns { "List<T>" }
returns("List<T>")
body {
"""
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." }
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<GenericFunction> {
}
}
templates add f("sorted()") {
val f_sorted = fn("sorted()") {
includeDefault()
exclude(PrimitiveType.Boolean)
} builder {
doc {
"""
@@ -120,11 +140,13 @@ fun ordering(): List<GenericFunction> {
"""
}
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<T> {
@@ -138,15 +160,16 @@ fun ordering(): List<GenericFunction> {
}
}
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<T>")
returns("SELF")
body() {
body {
"""
if (isEmpty()) return this
return this.copyOf().apply { sort() }
@@ -154,13 +177,16 @@ fun ordering(): List<GenericFunction> {
}
}
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<T>")
customReceiver(Lists) { "MutableList<T>" }
specialFor(Lists) {
receiver("MutableList<T>")
}
body { """sortWith(reverseOrder())""" }
body(ArraysOfPrimitives) {
@@ -173,8 +199,10 @@ fun ordering(): List<GenericFunction> {
}
}
templates add f("sortedDescending()") {
val f_sortedDescending = fn("sortedDescending()") {
includeDefault()
exclude(PrimitiveType.Boolean)
} builder {
doc {
"""
@@ -194,16 +222,19 @@ fun ordering(): List<GenericFunction> {
"""
}
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<GenericFunction> {
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<GenericFunction> {
}
}
templates add f("sortedWith(comparator: Comparator<in T>)") {
val f_sortedWith = fn("sortedWith(comparator: Comparator<in T>)") {
includeDefault()
} builder {
returns("List<T>")
doc {
"""
@@ -252,11 +284,13 @@ fun ordering(): List<GenericFunction> {
"""
}
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<T> {
@@ -270,13 +304,14 @@ fun ordering(): List<GenericFunction> {
}
}
templates add f("sortedArrayWith(comparator: Comparator<in T>)") {
only(ArraysOfObjects)
val f_sortedArrayWith = fn("sortedArrayWith(comparator: Comparator<in T>)") {
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<GenericFunction> {
}
}
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<R>")
customReceiver(Lists) { "MutableList<T>" }
specialFor(Lists) { receiver("MutableList<T>") }
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<T>")
typeParam("R : Comparable<R>")
@@ -306,31 +344,35 @@ fun ordering(): List<GenericFunction> {
"""
}
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<R>")
customReceiver(Lists) { "MutableList<T>" }
specialFor(Lists) { receiver("MutableList<T>") }
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<T>")
typeParam("R : Comparable<R>")
@@ -340,16 +382,17 @@ fun ordering(): List<GenericFunction> {
"""
}
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
}
}