Rewrite snapshot and set operations in new DSL

This commit is contained in:
Ilya Gorbunov
2017-11-08 08:05:54 +03:00
parent 3b6c5880e2
commit efefa64ac5
3 changed files with 173 additions and 112 deletions
@@ -16,9 +16,9 @@ fun main(args: Array<String>) {
Filtering,
Ordering,
// ArrayOps,
// Snapshots,
Snapshots,
// Mapping,
// SetOps,
SetOps,
Aggregates,
Guards,
Generators,
@@ -1,14 +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 sets(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
object SetOps : TemplateGroupBase() {
templates add f("toMutableSet()") {
exclude(Strings)
doc { f ->
val f_toMutableSet = fn("toMutableSet()") {
includeDefault()
} builder {
doc {
"""
Returns a mutable set containing all distinct ${f.element.pluralize()} from the given ${f.collection}.
@@ -41,9 +57,10 @@ fun sets(): List<GenericFunction> {
}
}
templates add f("distinct()") {
exclude(Strings)
doc { f ->
val f_distinct = fn("distinct()") {
includeDefault()
} builder {
doc {
"""
Returns a ${f.mapResult} containing only distinct ${f.element.pluralize()} from the given ${f.collection}.
@@ -53,14 +70,17 @@ fun sets(): List<GenericFunction> {
returns("List<T>")
body { "return this.toMutableSet().toList()" }
sequenceClassification(intermediate, stateful)
returns(Sequences) { "Sequence<T>" }
body(Sequences) { "return this.distinctBy { it }" }
specialFor(Sequences) {
sequenceClassification(intermediate, stateful)
returns("Sequence<T>")
body { "return this.distinctBy { it }" }
}
}
templates add f("distinctBy(selector: (T) -> K)") {
exclude(Strings)
doc { f ->
val f_distinctBy = fn("distinctBy(selector: (T) -> K)") {
includeDefault()
} builder {
doc {
"""
Returns a ${f.mapResult} containing only ${f.element.pluralize()} from the given ${f.collection}
having distinct keys returned by the given [selector] function.
@@ -69,7 +89,7 @@ fun sets(): List<GenericFunction> {
"""
}
inline(true)
inline()
typeParam("K")
returns("List<T>")
body {
@@ -85,21 +105,19 @@ fun sets(): List<GenericFunction> {
"""
}
inline(false, Sequences)
returns(Sequences) { "Sequence<T>" }
sequenceClassification(intermediate, stateful)
body(Sequences) {
"""
return DistinctSequence(this, selector)
"""
specialFor(Sequences) {
inline(Inline.No)
returns("Sequence<T>")
sequenceClassification(intermediate, stateful)
body { """return DistinctSequence(this, selector)""" }
}
}
templates add f("union(other: Iterable<T>)") {
val f_union = fn("union(other: Iterable<T>)") {
include(Family.defaultFamilies - Sequences)
} builder {
infix(true)
exclude(Strings, Sequences)
doc { f ->
doc {
"""
Returns a set containing all distinct elements from both collections.
@@ -118,10 +136,11 @@ fun sets(): List<GenericFunction> {
}
}
templates add f("intersect(other: Iterable<T>)") {
infix(true)
exclude(Strings, Sequences)
doc { f ->
val f_intersect = fn("intersect(other: Iterable<T>)") {
include(Family.defaultFamilies - Sequences)
} builder {
infix()
doc {
"""
Returns a set containing all elements that are contained by both this set and the specified collection.
@@ -138,10 +157,11 @@ fun sets(): List<GenericFunction> {
}
}
templates add f("subtract(other: Iterable<T>)") {
infix(true)
exclude(Strings, Sequences)
doc { f ->
val f_subtract = fn("subtract(other: Iterable<T>)") {
include(Family.defaultFamilies - Sequences)
} builder {
infix()
doc {
"""
Returns a set containing all elements that are contained by this ${f.collection} and not contained by the specified collection.
@@ -158,5 +178,4 @@ fun sets(): List<GenericFunction> {
}
}
return templates
}
}
@@ -1,13 +1,36 @@
/*
* 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 snapshots(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
object Snapshots : TemplateGroupBase() {
templates add f("toCollection(destination: C)") {
init {
defaultBuilder {
sequenceClassification(SequenceClass.terminal)
}
}
val f_toCollection = fn("toCollection(destination: C)") {
includeDefault()
include(CharSequences)
doc { f -> "Appends all ${f.element.pluralize()} to the given [destination] collection." }
} builder {
doc { "Appends all ${f.element.pluralize()} to the given [destination] collection." }
returns("C")
typeParam("C : MutableCollection<in T>")
body {
@@ -20,8 +43,11 @@ fun snapshots(): List<GenericFunction> {
}
}
templates add f("toSet()") {
doc { f ->
val f_toSet = fn("toSet()") {
includeDefault()
include(CharSequences)
} builder {
doc {
"""
Returns a [Set] of all ${f.element.pluralize()}.
@@ -44,7 +70,7 @@ fun snapshots(): List<GenericFunction> {
}
body(Sequences) { "return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()" }
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) { f ->
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
val size = if (f == CharSequences) "length" else "size"
"""
return when ($size) {
@@ -56,8 +82,11 @@ fun snapshots(): List<GenericFunction> {
}
}
templates add f("toHashSet()") {
doc { f -> "Returns a [HashSet] of all ${f.element.pluralize()}." }
val f_toHashSet = fn("toHashSet()") {
includeDefault()
include(CharSequences)
} builder {
doc { "Returns a [HashSet] of all ${f.element.pluralize()}." }
returns("HashSet<T>")
body { "return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" }
body(Sequences) { "return toCollection(HashSet<T>())" }
@@ -65,19 +94,24 @@ fun snapshots(): List<GenericFunction> {
body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(HashSet<T>(mapCapacity(size)))" }
}
templates add f("toSortedSet()") {
val f_toSortedSet = fn("toSortedSet()") {
includeDefault()
include(CharSequences)
jvmOnly(true)
platforms(Platform.JVM)
} builder {
jvmOnly = true
typeParam("T: Comparable<T>")
doc { f -> "Returns a [SortedSet] of all ${f.element.pluralize()}." }
doc { "Returns a [SortedSet] of all ${f.element.pluralize()}." }
returns("SortedSet<T>")
body { "return toCollection(TreeSet<T>())" }
}
templates add f("toSortedSet(comparator: Comparator<in T>)") {
only(Iterables, ArraysOfObjects, Sequences)
jvmOnly(true)
doc { f ->
val f_toSortedSet_comparator = fn("toSortedSet(comparator: Comparator<in T>)") {
include(Iterables, ArraysOfObjects, Sequences)
platforms(Platform.JVM)
} builder {
jvmOnly = true
doc {
"""
Returns a [SortedSet] of all ${f.element.pluralize()}.
@@ -88,8 +122,11 @@ fun snapshots(): List<GenericFunction> {
body { "return toCollection(TreeSet<T>(comparator))" }
}
templates add f("toMutableList()") {
doc { f -> "Returns a [MutableList] filled with all ${f.element.pluralize()} of this ${f.collection}." }
val f_toMutableList = fn("toMutableList()") {
includeDefault()
include(Collections, CharSequences)
} builder {
doc { "Returns a [MutableList] filled with all ${f.element.pluralize()} of this ${f.collection}." }
returns("MutableList<T>")
body { "return toCollection(ArrayList<T>())" }
body(Iterables) {
@@ -111,33 +148,11 @@ fun snapshots(): List<GenericFunction> {
}
}
templates add f("toList()") {
only(Maps)
doc { "Returns a [List] containing all key-value pairs." }
returns("List<Pair<K, V>>")
body {
"""
if (size == 0)
return emptyList()
val iterator = entries.iterator()
if (!iterator.hasNext())
return emptyList()
val first = iterator.next()
if (!iterator.hasNext())
return listOf(first.toPair())
val result = ArrayList<Pair<K, V>>(size)
result.add(first.toPair())
do {
result.add(iterator.next().toPair())
} while (iterator.hasNext())
return result
"""
}
}
templates add f("toList()") {
include(CharSequences)
doc { f -> "Returns a [List] containing all ${f.element.pluralize()}." }
val f_toList = fn("toList()") {
includeDefault()
include(Maps, CharSequences)
} builder {
doc { "Returns a [List] containing all ${f.element.pluralize()}." }
returns("List<T>")
body { "return this.toMutableList().optimizeReadOnlyList()" }
body(Iterables) {
@@ -152,7 +167,7 @@ fun snapshots(): List<GenericFunction> {
return this.toMutableList().optimizeReadOnlyList()
"""
}
body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) { f ->
body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) {
"""
return when (${ if (f == CharSequences) "length" else "size" }) {
0 -> emptyList()
@@ -161,15 +176,39 @@ fun snapshots(): List<GenericFunction> {
}
"""
}
specialFor(Maps) {
doc { "Returns a [List] containing all key-value pairs." }
returns("List<Pair<K, V>>")
body {
"""
if (size == 0)
return emptyList()
val iterator = entries.iterator()
if (!iterator.hasNext())
return emptyList()
val first = iterator.next()
if (!iterator.hasNext())
return listOf(first.toPair())
val result = ArrayList<Pair<K, V>>(size)
result.add(first.toPair())
do {
result.add(iterator.next().toPair())
} while (iterator.hasNext())
return result
"""
}
}
}
templates add f("associate(transform: (T) -> Pair<K, V>)") {
inline(true)
val f_associate = fn("associate(transform: (T) -> Pair<K, V>)") {
includeDefault()
include(CharSequences)
} builder {
inline()
typeParam("K")
typeParam("V")
returns("Map<K, V>")
doc { f ->
doc {
"""
Returns a [Map] containing key-value pairs provided by [transform] function
applied to ${f.element.pluralize()} of the given ${f.collection}.
@@ -204,14 +243,16 @@ fun snapshots(): List<GenericFunction> {
}
}
templates add f("associateTo(destination: M, transform: (T) -> Pair<K, V>)") {
inline(true)
val f_associateTo = fn("associateTo(destination: M, transform: (T) -> Pair<K, V>)") {
includeDefault()
include(CharSequences)
} builder {
inline()
typeParam("K")
typeParam("V")
typeParam("M : MutableMap<in K, in V>")
returns("M")
doc { f ->
doc {
"""
Populates and returns the [destination] mutable map with key-value pairs
provided by [transform] function applied to each ${f.element} of the given ${f.collection}.
@@ -229,11 +270,13 @@ fun snapshots(): List<GenericFunction> {
}
}
templates add f("associateBy(keySelector: (T) -> K)") {
inline(true)
val f_associateBy_key = fn("associateBy(keySelector: (T) -> K)") {
includeDefault()
include(CharSequences)
} builder {
inline()
typeParam("K")
doc { f ->
doc {
"""
Returns a [Map] containing the ${f.element.pluralize()} from the given ${f.collection} indexed by the key
returned from [keySelector] function applied to each ${f.element}.
@@ -245,11 +288,8 @@ fun snapshots(): List<GenericFunction> {
}
returns("Map<K, T>")
/**
* Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection
* constructor.
*/
// Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection
// constructor.
body {
"""
val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
@@ -275,13 +315,15 @@ fun snapshots(): List<GenericFunction> {
}
}
templates add f("associateByTo(destination: M, keySelector: (T) -> K)") {
inline(true)
val f_associateByTo_key = fn("associateByTo(destination: M, keySelector: (T) -> K)") {
includeDefault()
include(CharSequences)
} builder {
inline()
typeParam("K")
typeParam("M : MutableMap<in K, in T>")
returns("M")
doc { f ->
doc {
"""
Populates and returns the [destination] mutable map with key-value pairs,
where key is provided by the [keySelector] function applied to each ${f.element} of the given ${f.collection}
@@ -300,12 +342,14 @@ fun snapshots(): List<GenericFunction> {
}
}
templates add f("associateBy(keySelector: (T) -> K, valueTransform: (T) -> V)") {
inline(true)
val f_associateBy_key_value = fn("associateBy(keySelector: (T) -> K, valueTransform: (T) -> V)") {
includeDefault()
include(CharSequences)
} builder {
inline()
typeParam("K")
typeParam("V")
doc { f ->
doc {
"""
Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to ${f.element.pluralize()} of the given ${f.collection}.
@@ -346,15 +390,17 @@ fun snapshots(): List<GenericFunction> {
}
}
templates add f("associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") {
inline(true)
val f_associateByTo_key_value = fn("associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") {
includeDefault()
include(CharSequences)
} builder {
inline()
typeParam("K")
typeParam("V")
typeParam("M : MutableMap<in K, in V>")
returns("M")
doc { f ->
doc {
"""
Populates and returns the [destination] mutable map with key-value pairs,
where key is provided by the [keySelector] function and
@@ -372,8 +418,4 @@ fun snapshots(): List<GenericFunction> {
"""
}
}
templates.forEach { it.sequenceClassification(SequenceClass.terminal) }
return templates
}
}