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, Filtering,
Ordering, Ordering,
// ArrayOps, // ArrayOps,
// Snapshots, Snapshots,
// Mapping, // Mapping,
// SetOps, SetOps,
Aggregates, Aggregates,
Guards, Guards,
Generators, 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 package templates
import templates.Family.* import templates.Family.*
import templates.SequenceClass.* import templates.SequenceClass.*
fun sets(): List<GenericFunction> { object SetOps : TemplateGroupBase() {
val templates = arrayListOf<GenericFunction>()
templates add f("toMutableSet()") { val f_toMutableSet = fn("toMutableSet()") {
exclude(Strings) includeDefault()
doc { f -> } builder {
doc {
""" """
Returns a mutable set containing all distinct ${f.element.pluralize()} from the given ${f.collection}. 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()") { val f_distinct = fn("distinct()") {
exclude(Strings) includeDefault()
doc { f -> } builder {
doc {
""" """
Returns a ${f.mapResult} containing only distinct ${f.element.pluralize()} from the given ${f.collection}. 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>") returns("List<T>")
body { "return this.toMutableSet().toList()" } body { "return this.toMutableSet().toList()" }
sequenceClassification(intermediate, stateful) specialFor(Sequences) {
returns(Sequences) { "Sequence<T>" } sequenceClassification(intermediate, stateful)
body(Sequences) { "return this.distinctBy { it }" } returns("Sequence<T>")
body { "return this.distinctBy { it }" }
}
} }
templates add f("distinctBy(selector: (T) -> K)") { val f_distinctBy = fn("distinctBy(selector: (T) -> K)") {
exclude(Strings) includeDefault()
doc { f -> } builder {
doc {
""" """
Returns a ${f.mapResult} containing only ${f.element.pluralize()} from the given ${f.collection} Returns a ${f.mapResult} containing only ${f.element.pluralize()} from the given ${f.collection}
having distinct keys returned by the given [selector] function. having distinct keys returned by the given [selector] function.
@@ -69,7 +89,7 @@ fun sets(): List<GenericFunction> {
""" """
} }
inline(true) inline()
typeParam("K") typeParam("K")
returns("List<T>") returns("List<T>")
body { body {
@@ -85,21 +105,19 @@ fun sets(): List<GenericFunction> {
""" """
} }
inline(false, Sequences) specialFor(Sequences) {
returns(Sequences) { "Sequence<T>" } inline(Inline.No)
sequenceClassification(intermediate, stateful) returns("Sequence<T>")
body(Sequences) { sequenceClassification(intermediate, stateful)
""" body { """return DistinctSequence(this, selector)""" }
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) infix(true)
exclude(Strings, Sequences) doc {
doc { f ->
""" """
Returns a set containing all distinct elements from both collections. 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>)") { val f_intersect = fn("intersect(other: Iterable<T>)") {
infix(true) include(Family.defaultFamilies - Sequences)
exclude(Strings, Sequences) } builder {
doc { f -> infix()
doc {
""" """
Returns a set containing all elements that are contained by both this set and the specified collection. 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>)") { val f_subtract = fn("subtract(other: Iterable<T>)") {
infix(true) include(Family.defaultFamilies - Sequences)
exclude(Strings, Sequences) } builder {
doc { f -> infix()
doc {
""" """
Returns a set containing all elements that are contained by this ${f.collection} and not contained by the specified collection. 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 package templates
import templates.Family.* import templates.Family.*
fun snapshots(): List<GenericFunction> { object Snapshots : TemplateGroupBase() {
val templates = arrayListOf<GenericFunction>()
templates add f("toCollection(destination: C)") { init {
defaultBuilder {
sequenceClassification(SequenceClass.terminal)
}
}
val f_toCollection = fn("toCollection(destination: C)") {
includeDefault()
include(CharSequences) 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") returns("C")
typeParam("C : MutableCollection<in T>") typeParam("C : MutableCollection<in T>")
body { body {
@@ -20,8 +43,11 @@ fun snapshots(): List<GenericFunction> {
} }
} }
templates add f("toSet()") { val f_toSet = fn("toSet()") {
doc { f -> includeDefault()
include(CharSequences)
} builder {
doc {
""" """
Returns a [Set] of all ${f.element.pluralize()}. Returns a [Set] of all ${f.element.pluralize()}.
@@ -44,7 +70,7 @@ fun snapshots(): List<GenericFunction> {
} }
body(Sequences) { "return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()" } body(Sequences) { "return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()" }
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) { f -> body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
val size = if (f == CharSequences) "length" else "size" val size = if (f == CharSequences) "length" else "size"
""" """
return when ($size) { return when ($size) {
@@ -56,8 +82,11 @@ fun snapshots(): List<GenericFunction> {
} }
} }
templates add f("toHashSet()") { val f_toHashSet = fn("toHashSet()") {
doc { f -> "Returns a [HashSet] of all ${f.element.pluralize()}." } includeDefault()
include(CharSequences)
} builder {
doc { "Returns a [HashSet] of all ${f.element.pluralize()}." }
returns("HashSet<T>") returns("HashSet<T>")
body { "return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" } body { "return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" }
body(Sequences) { "return toCollection(HashSet<T>())" } body(Sequences) { "return toCollection(HashSet<T>())" }
@@ -65,19 +94,24 @@ fun snapshots(): List<GenericFunction> {
body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(HashSet<T>(mapCapacity(size)))" } body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(HashSet<T>(mapCapacity(size)))" }
} }
templates add f("toSortedSet()") { val f_toSortedSet = fn("toSortedSet()") {
includeDefault()
include(CharSequences) include(CharSequences)
jvmOnly(true) platforms(Platform.JVM)
} builder {
jvmOnly = true
typeParam("T: Comparable<T>") 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>") returns("SortedSet<T>")
body { "return toCollection(TreeSet<T>())" } body { "return toCollection(TreeSet<T>())" }
} }
templates add f("toSortedSet(comparator: Comparator<in T>)") { val f_toSortedSet_comparator = fn("toSortedSet(comparator: Comparator<in T>)") {
only(Iterables, ArraysOfObjects, Sequences) include(Iterables, ArraysOfObjects, Sequences)
jvmOnly(true) platforms(Platform.JVM)
doc { f -> } builder {
jvmOnly = true
doc {
""" """
Returns a [SortedSet] of all ${f.element.pluralize()}. Returns a [SortedSet] of all ${f.element.pluralize()}.
@@ -88,8 +122,11 @@ fun snapshots(): List<GenericFunction> {
body { "return toCollection(TreeSet<T>(comparator))" } body { "return toCollection(TreeSet<T>(comparator))" }
} }
templates add f("toMutableList()") { val f_toMutableList = fn("toMutableList()") {
doc { f -> "Returns a [MutableList] filled with all ${f.element.pluralize()} of this ${f.collection}." } includeDefault()
include(Collections, CharSequences)
} builder {
doc { "Returns a [MutableList] filled with all ${f.element.pluralize()} of this ${f.collection}." }
returns("MutableList<T>") returns("MutableList<T>")
body { "return toCollection(ArrayList<T>())" } body { "return toCollection(ArrayList<T>())" }
body(Iterables) { body(Iterables) {
@@ -111,33 +148,11 @@ fun snapshots(): List<GenericFunction> {
} }
} }
templates add f("toList()") { val f_toList = fn("toList()") {
only(Maps) includeDefault()
doc { "Returns a [List] containing all key-value pairs." } include(Maps, CharSequences)
returns("List<Pair<K, V>>") } builder {
body { doc { "Returns a [List] containing all ${f.element.pluralize()}." }
"""
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()}." }
returns("List<T>") returns("List<T>")
body { "return this.toMutableList().optimizeReadOnlyList()" } body { "return this.toMutableList().optimizeReadOnlyList()" }
body(Iterables) { body(Iterables) {
@@ -152,7 +167,7 @@ fun snapshots(): List<GenericFunction> {
return this.toMutableList().optimizeReadOnlyList() return this.toMutableList().optimizeReadOnlyList()
""" """
} }
body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) { f -> body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) {
""" """
return when (${ if (f == CharSequences) "length" else "size" }) { return when (${ if (f == CharSequences) "length" else "size" }) {
0 -> emptyList() 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>)") { val f_associate = fn("associate(transform: (T) -> Pair<K, V>)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
} builder {
inline()
typeParam("K") typeParam("K")
typeParam("V") typeParam("V")
returns("Map<K, V>") returns("Map<K, V>")
doc { f -> doc {
""" """
Returns a [Map] containing key-value pairs provided by [transform] function Returns a [Map] containing key-value pairs provided by [transform] function
applied to ${f.element.pluralize()} of the given ${f.collection}. 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>)") { val f_associateTo = fn("associateTo(destination: M, transform: (T) -> Pair<K, V>)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
} builder {
inline()
typeParam("K") typeParam("K")
typeParam("V") typeParam("V")
typeParam("M : MutableMap<in K, in V>") typeParam("M : MutableMap<in K, in V>")
returns("M") returns("M")
doc { f -> doc {
""" """
Populates and returns the [destination] mutable map with key-value pairs 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}. 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)") { val f_associateBy_key = fn("associateBy(keySelector: (T) -> K)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
} builder {
inline()
typeParam("K") typeParam("K")
doc { f -> doc {
""" """
Returns a [Map] containing the ${f.element.pluralize()} from the given ${f.collection} indexed by the key 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}. returned from [keySelector] function applied to each ${f.element}.
@@ -245,11 +288,8 @@ fun snapshots(): List<GenericFunction> {
} }
returns("Map<K, T>") returns("Map<K, T>")
/** // Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection
* Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection // constructor.
* constructor.
*/
body { body {
""" """
val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
@@ -275,13 +315,15 @@ fun snapshots(): List<GenericFunction> {
} }
} }
templates add f("associateByTo(destination: M, keySelector: (T) -> K)") { val f_associateByTo_key = fn("associateByTo(destination: M, keySelector: (T) -> K)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
} builder {
inline()
typeParam("K") typeParam("K")
typeParam("M : MutableMap<in K, in T>") typeParam("M : MutableMap<in K, in T>")
returns("M") returns("M")
doc { f -> doc {
""" """
Populates and returns the [destination] mutable map with key-value pairs, 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} 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)") { val f_associateBy_key_value = fn("associateBy(keySelector: (T) -> K, valueTransform: (T) -> V)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
} builder {
inline()
typeParam("K") typeParam("K")
typeParam("V") 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}. 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)") { val f_associateByTo_key_value = fn("associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
} builder {
inline()
typeParam("K") typeParam("K")
typeParam("V") typeParam("V")
typeParam("M : MutableMap<in K, in V>") typeParam("M : MutableMap<in K, in V>")
returns("M") returns("M")
doc { f -> doc {
""" """
Populates and returns the [destination] mutable map with key-value pairs, Populates and returns the [destination] mutable map with key-value pairs,
where key is provided by the [keySelector] function and 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
} }