Rewrite Guards, Numeric, SequenceOps, StringJoinOps in new DSL

This commit is contained in:
Ilya Gorbunov
2017-11-05 07:53:54 +03:00
parent d2e1baa9d4
commit c3b894f041
5 changed files with 122 additions and 55 deletions
@@ -20,12 +20,12 @@ fun main(args: Array<String>) {
// Mapping,
// SetOps,
Aggregates,
// Guards,
Guards,
Generators,
// StringJoinOps,
// SequenceOps,
StringJoinOps,
SequenceOps,
// RangeOps,
// Numeric,
Numeric,
// ComparableOps,
// CommonArrays,
// PlatformSpecialized,
@@ -1,15 +1,30 @@
/*
* 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 guards(): List<GenericFunction> {
val THIS = "\$this"
object Guards : TemplateGroupBase() {
private val THIS = "\$this"
val templates = arrayListOf<GenericFunction>()
templates add f("requireNoNulls()") {
only(Iterables, Sequences, InvariantArraysOfObjects, Lists)
val f_requireNoNulls = fn("requireNoNulls()") {
include(Iterables, Sequences, InvariantArraysOfObjects, Lists)
} builder {
doc { "Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements." }
sequenceClassification(intermediate, stateless)
typeParam("T : Any")
@@ -32,6 +47,4 @@ fun guards(): List<GenericFunction> {
"""
}
}
return templates
}
@@ -1,14 +1,37 @@
/*
* 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.*
object Numeric : TemplateGroupBase() {
fun numeric(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
init {
defaultBuilder {
sequenceClassification(SequenceClass.terminal)
}
}
templates add f("sum()") {
exclude(Strings)
buildFamilies.default!!.forEach { family -> onlyPrimitives(family, numericPrimitives) }
doc { f -> "Returns the sum of all elements in the ${f.collection}." }
// TODO: use just numericPrimitives
private val numericPrimitivesDefaultOrder = PrimitiveType.defaultPrimitives intersect PrimitiveType.numericPrimitives
val f_sum = fn("sum()") {
Family.defaultFamilies.forEach { family -> include(family, numericPrimitivesDefaultOrder) }
} builder {
doc { "Returns the sum of all elements in the ${f.collection}." }
returns("SUM")
platformName("sumOf<T>")
body {
@@ -22,10 +45,10 @@ fun numeric(): List<GenericFunction> {
}
}
templates add f("average()") {
exclude(Strings)
buildFamilies.default!!.forEach { family -> onlyPrimitives(family, numericPrimitives) }
doc { f -> "Returns an average value of elements in the ${f.collection}."}
val f_average = fn("average()") {
Family.defaultFamilies.forEach { family -> include(family, numericPrimitivesDefaultOrder) }
} builder {
doc { "Returns an average value of elements in the ${f.collection}."}
returns("Double")
platformName("averageOf<T>")
body {
@@ -41,7 +64,4 @@ fun numeric(): List<GenericFunction> {
}
}
templates.forEach { it.sequenceClassification(SequenceClass.terminal) }
return templates
}
@@ -1,15 +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.*
fun sequences(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
object SequenceOps : TemplateGroupBase() {
templates add f("asIterable()") {
only(Iterables, ArraysOfObjects, ArraysOfPrimitives, Sequences, CharSequences, Maps)
doc { f -> "Creates an [Iterable] instance that wraps the original ${f.collection} returning its ${f.element.pluralize()} when being iterated." }
val f_asIterable = fn("asIterable()") {
include(Iterables, ArraysOfObjects, ArraysOfPrimitives, Sequences, CharSequences, Maps)
} builder {
doc { "Creates an [Iterable] instance that wraps the original ${f.collection} returning its ${f.element.pluralize()} when being iterated." }
returns("Iterable<T>")
body { f ->
body {
"""
${ when(f) {
ArraysOfObjects, ArraysOfPrimitives -> "if (isEmpty()) return emptyList()"
@@ -20,16 +36,20 @@ fun sequences(): List<GenericFunction> {
"""
}
inline(Iterables, Maps) { Inline.Only }
specialFor(Iterables, Maps) { inlineOnly() }
specialFor(Iterables) {
doc { "Returns this collection as an [Iterable]." }
body { "return this" }
}
doc(Iterables) { "Returns this collection as an [Iterable]." }
body(Iterables) { "return this" }
body(Maps) { "return entries" }
}
templates add f("asSequence()") {
val f_asSequence = fn("asSequence()") {
includeDefault()
include(CharSequences, Maps)
doc { f ->
} builder {
doc {
"""
Creates a [Sequence] instance that wraps the original ${f.collection} returning its ${f.element.pluralize()} when being iterated.
@@ -37,7 +57,7 @@ fun sequences(): List<GenericFunction> {
"""
}
returns("Sequence<T>")
body { f ->
body {
"""
${ when(f) {
ArraysOfObjects, ArraysOfPrimitives -> "if (isEmpty()) return emptySequence()"
@@ -50,11 +70,10 @@ fun sequences(): List<GenericFunction> {
body(Maps) { "return entries.asSequence()" }
doc(Sequences) { "Returns this sequence as a [Sequence]."}
inline(Sequences) { Inline.Only }
body(Sequences) { "return this" }
specialFor(Sequences) {
doc { "Returns this sequence as a [Sequence]."}
inlineOnly()
body { "return this" }
}
}
return templates
}
@@ -1,12 +1,29 @@
/*
* 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 strings(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
object StringJoinOps : TemplateGroupBase() {
templates add f("joinTo(buffer: A, separator: CharSequence = \", \", prefix: CharSequence = \"\", postfix: CharSequence = \"\", limit: Int = -1, truncated: CharSequence = \"...\", transform: ((T) -> CharSequence)? = null)") {
val f_joinTo = fn("joinTo(buffer: A, separator: CharSequence = \", \", prefix: CharSequence = \"\", postfix: CharSequence = \"\", limit: Int = -1, truncated: CharSequence = \"...\", transform: ((T) -> CharSequence)? = null)") {
includeDefault()
} builder {
doc {
"""
Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
@@ -17,7 +34,7 @@ fun strings(): List<GenericFunction> {
}
sequenceClassification(terminal)
typeParam("A : Appendable")
returns { "A" }
returns("A")
body {
"""
buffer.append(prefix)
@@ -33,8 +50,7 @@ fun strings(): List<GenericFunction> {
return buffer
"""
}
exclude(Strings)
bodyForTypes(ArraysOfPrimitives, *defaultPrimitives.toTypedArray()) { primitive ->
body(ArraysOfPrimitives) {
"""
buffer.append(prefix)
var count = 0
@@ -54,7 +70,9 @@ fun strings(): List<GenericFunction> {
}
}
templates add f("joinToString(separator: CharSequence = \", \", prefix: CharSequence = \"\", postfix: CharSequence = \"\", limit: Int = -1, truncated: CharSequence = \"...\", transform: ((T) -> CharSequence)? = null)") {
val f_joinToString = fn("joinToString(separator: CharSequence = \", \", prefix: CharSequence = \"\", postfix: CharSequence = \"\", limit: Int = -1, truncated: CharSequence = \"...\", transform: ((T) -> CharSequence)? = null)") {
includeDefault()
} builder {
doc {
"""
Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
@@ -65,7 +83,6 @@ fun strings(): List<GenericFunction> {
}
sequenceClassification(terminal)
exclude(Strings)
returns("String")
body {
"""
@@ -73,6 +90,4 @@ fun strings(): List<GenericFunction> {
"""
}
}
return templates
}
}