diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index 7e1a80b7453..6caed68cdaa 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -20,12 +20,12 @@ fun main(args: Array) { // Mapping, // SetOps, Aggregates, -// Guards, + Guards, Generators, -// StringJoinOps, -// SequenceOps, + StringJoinOps, + SequenceOps, // RangeOps, -// Numeric, + Numeric, // ComparableOps, // CommonArrays, // PlatformSpecialized, diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt index 84506f5113d..eb7d84894e3 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt @@ -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 { - val THIS = "\$this" +object Guards : TemplateGroupBase() { + private val THIS = "\$this" - val templates = arrayListOf() - - 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 { """ } } - - return templates } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt index 7721f9aea85..32ff5e533bc 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt @@ -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 { - val templates = arrayListOf() + 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") body { @@ -22,10 +45,10 @@ fun numeric(): List { } } - 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") body { @@ -41,7 +64,4 @@ fun numeric(): List { } } - templates.forEach { it.sequenceClassification(SequenceClass.terminal) } - - return templates } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Sequence.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Sequence.kt index 154824ca54b..a1e5a039569 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Sequence.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Sequence.kt @@ -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 { - val templates = arrayListOf() +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") - body { f -> + body { """ ${ when(f) { ArraysOfObjects, ArraysOfPrimitives -> "if (isEmpty()) return emptyList()" @@ -20,16 +36,20 @@ fun sequences(): List { """ } - 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 { """ } returns("Sequence") - body { f -> + body { """ ${ when(f) { ArraysOfObjects, ArraysOfPrimitives -> "if (isEmpty()) return emptySequence()" @@ -50,11 +70,10 @@ fun sequences(): List { 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 } - diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Strings.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Strings.kt index d5566d7d342..f5617f95d82 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Strings.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Strings.kt @@ -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 { - val templates = arrayListOf() +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 { } sequenceClassification(terminal) typeParam("A : Appendable") - returns { "A" } + returns("A") body { """ buffer.append(prefix) @@ -33,8 +50,7 @@ fun strings(): List { return buffer """ } - exclude(Strings) - bodyForTypes(ArraysOfPrimitives, *defaultPrimitives.toTypedArray()) { primitive -> + body(ArraysOfPrimitives) { """ buffer.append(prefix) var count = 0 @@ -54,7 +70,9 @@ fun strings(): List { } } - 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 { } sequenceClassification(terminal) - exclude(Strings) returns("String") body { """ @@ -73,6 +90,4 @@ fun strings(): List { """ } } - - return templates -} \ No newline at end of file +}