Review and correct exception messages.
This commit is contained in:
@@ -480,10 +480,10 @@ fun aggregates(): List<GenericFunction> {
|
||||
typeParam("S")
|
||||
typeParam("T: S")
|
||||
returns("S")
|
||||
body {
|
||||
body { f ->
|
||||
"""
|
||||
val iterator = this.iterator()
|
||||
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.")
|
||||
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
|
||||
|
||||
var index = 1
|
||||
var accumulator: S = iterator.next()
|
||||
@@ -595,10 +595,10 @@ fun aggregates(): List<GenericFunction> {
|
||||
typeParam("S")
|
||||
typeParam("T: S")
|
||||
returns("S")
|
||||
body {
|
||||
body { f ->
|
||||
"""
|
||||
val iterator = this.iterator()
|
||||
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.")
|
||||
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
|
||||
|
||||
var accumulator: S = iterator.next()
|
||||
while (iterator.hasNext()) {
|
||||
@@ -627,10 +627,10 @@ fun aggregates(): List<GenericFunction> {
|
||||
only(CharSequences, ArraysOfPrimitives)
|
||||
doc { f -> "Accumulates value starting with last ${f.element} and applying [operation] from right to left to each ${f.element} and current accumulator value." }
|
||||
returns("T")
|
||||
body {
|
||||
body { f ->
|
||||
"""
|
||||
var index = lastIndex
|
||||
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
|
||||
if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
|
||||
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
@@ -650,10 +650,10 @@ fun aggregates(): List<GenericFunction> {
|
||||
typeParam("S")
|
||||
typeParam("T: S")
|
||||
returns("S")
|
||||
body {
|
||||
body { f ->
|
||||
"""
|
||||
var index = lastIndex
|
||||
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
|
||||
if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
|
||||
|
||||
var accumulator: S = get(index--)
|
||||
while (index >= 0) {
|
||||
|
||||
@@ -2,7 +2,6 @@ package templates
|
||||
|
||||
import templates.Family.*
|
||||
|
||||
|
||||
fun elements(): List<GenericFunction> {
|
||||
val templates = arrayListOf<GenericFunction>()
|
||||
|
||||
@@ -14,7 +13,7 @@ fun elements(): List<GenericFunction> {
|
||||
doc { f -> "Returns `true` if [element] is found in the ${f.collection}." }
|
||||
typeParam("@kotlin.internal.OnlyInputTypes T")
|
||||
returns("Boolean")
|
||||
body(Iterables) {
|
||||
body(Iterables) { f ->
|
||||
"""
|
||||
if (this is Collection)
|
||||
return contains(element)
|
||||
@@ -327,12 +326,7 @@ fun elements(): List<GenericFunction> {
|
||||
body {
|
||||
"""
|
||||
when (this) {
|
||||
is List -> {
|
||||
if (isEmpty())
|
||||
throw NoSuchElementException("Collection is empty.")
|
||||
else
|
||||
return this[0]
|
||||
}
|
||||
is List -> return this.first()
|
||||
else -> {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext())
|
||||
@@ -342,10 +336,10 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
"""
|
||||
}
|
||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f ->
|
||||
"""
|
||||
if (isEmpty())
|
||||
throw NoSuchElementException("Collection is empty.")
|
||||
throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
|
||||
return this[0]
|
||||
"""
|
||||
}
|
||||
@@ -401,10 +395,10 @@ fun elements(): List<GenericFunction> {
|
||||
doc { f -> """Returns the first ${f.element} matching the given [predicate].
|
||||
@throws [NoSuchElementException] if no such ${f.element} is found.""" }
|
||||
returns("T")
|
||||
body {
|
||||
body { f ->
|
||||
"""
|
||||
for (element in this) if (predicate(element)) return element
|
||||
throw NoSuchElementException("No element matching predicate was found.")
|
||||
throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.")
|
||||
"""
|
||||
}
|
||||
}
|
||||
@@ -438,12 +432,7 @@ fun elements(): List<GenericFunction> {
|
||||
body {
|
||||
"""
|
||||
when (this) {
|
||||
is List -> {
|
||||
if (isEmpty())
|
||||
throw NoSuchElementException("Collection is empty.")
|
||||
else
|
||||
return this[this.lastIndex]
|
||||
}
|
||||
is List -> return this.last()
|
||||
else -> {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext())
|
||||
@@ -467,10 +456,10 @@ fun elements(): List<GenericFunction> {
|
||||
return last
|
||||
"""
|
||||
}
|
||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f ->
|
||||
"""
|
||||
if (isEmpty())
|
||||
throw NoSuchElementException("Collection is empty.")
|
||||
throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
|
||||
return this[lastIndex]
|
||||
"""
|
||||
}
|
||||
@@ -541,18 +530,18 @@ fun elements(): List<GenericFunction> {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
|
||||
if (!found) throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.")
|
||||
return last as T
|
||||
"""
|
||||
}
|
||||
|
||||
body(CharSequences, ArraysOfPrimitives, ArraysOfObjects, Lists) {
|
||||
body(CharSequences, ArraysOfPrimitives, ArraysOfObjects, Lists) { f ->
|
||||
"""
|
||||
for (index in this.indices.reversed()) {
|
||||
val element = this[index]
|
||||
if (predicate(element)) return element
|
||||
}
|
||||
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
|
||||
throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.")
|
||||
"""
|
||||
}
|
||||
}
|
||||
@@ -605,11 +594,7 @@ fun elements(): List<GenericFunction> {
|
||||
body {
|
||||
"""
|
||||
when (this) {
|
||||
is List -> return when (size) {
|
||||
0 -> throw NoSuchElementException("Collection is empty.")
|
||||
1 -> this[0]
|
||||
else -> throw IllegalArgumentException("Collection has more than one element.")
|
||||
}
|
||||
is List -> return this.single()
|
||||
else -> {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext())
|
||||
@@ -633,21 +618,12 @@ fun elements(): List<GenericFunction> {
|
||||
return single
|
||||
"""
|
||||
}
|
||||
body(CharSequences) {
|
||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f ->
|
||||
"""
|
||||
return when (length) {
|
||||
0 -> throw NoSuchElementException("Collection is empty.")
|
||||
return when (${f.code.size}) {
|
||||
0 -> throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
|
||||
1 -> this[0]
|
||||
else -> throw IllegalArgumentException("Collection has more than one element.")
|
||||
}
|
||||
"""
|
||||
}
|
||||
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
return when (size) {
|
||||
0 -> throw NoSuchElementException("Collection is empty.")
|
||||
1 -> this[0]
|
||||
else -> throw IllegalArgumentException("Collection has more than one element.")
|
||||
else -> throw IllegalArgumentException("${f.doc.collection.capitalize()} has more than one element.")
|
||||
}
|
||||
"""
|
||||
}
|
||||
@@ -700,18 +676,18 @@ fun elements(): List<GenericFunction> {
|
||||
include(CharSequences)
|
||||
doc { f -> "Returns the single ${f.element} matching the given [predicate], or throws exception if there is no or more than one matching ${f.element}." }
|
||||
returns("T")
|
||||
body {
|
||||
body { f ->
|
||||
"""
|
||||
var single: T? = null
|
||||
var found = false
|
||||
for (element in this) {
|
||||
if (predicate(element)) {
|
||||
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
|
||||
if (found) throw IllegalArgumentException("${f.doc.collection.capitalize()} contains more than one matching element.")
|
||||
single = element
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
|
||||
if (!found) throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.")
|
||||
return single as T
|
||||
"""
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ fun filtering(): List<GenericFunction> {
|
||||
returns("List<T>")
|
||||
body {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return toList()
|
||||
val list: ArrayList<T>
|
||||
if (this is Collection<*>) {
|
||||
@@ -54,7 +54,7 @@ fun filtering(): List<GenericFunction> {
|
||||
returns(Sequences) { "Sequence<T>" }
|
||||
body(Sequences) {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
return when {
|
||||
n == 0 -> this
|
||||
this is DropTakeSequence -> this.drop(n)
|
||||
@@ -68,14 +68,14 @@ fun filtering(): List<GenericFunction> {
|
||||
returns(Strings, CharSequences) { "SELF" }
|
||||
body(Strings, CharSequences) { f ->
|
||||
"""
|
||||
require(n >= 0, { "Requested character count $n is less than zero." })
|
||||
require(n >= 0) { "Requested character count $n is less than zero." }
|
||||
return ${subsequence(f, "n.coerceAtMost(length)")}
|
||||
"""
|
||||
}
|
||||
|
||||
body(ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0)
|
||||
return toList()
|
||||
if (n >= size)
|
||||
@@ -96,7 +96,7 @@ fun filtering(): List<GenericFunction> {
|
||||
returns("List<T>")
|
||||
body {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
if (this is Collection<T> && n >= size) return toList()
|
||||
var count = 0
|
||||
@@ -115,7 +115,7 @@ fun filtering(): List<GenericFunction> {
|
||||
returns(Strings, CharSequences) { "SELF" }
|
||||
body(Strings, CharSequences) { f ->
|
||||
"""
|
||||
require(n >= 0, { "Requested character count $n is less than zero." })
|
||||
require(n >= 0) { "Requested character count $n is less than zero." }
|
||||
return ${subsequence(f, "0", "n.coerceAtMost(length)")}
|
||||
"""
|
||||
}
|
||||
@@ -124,7 +124,7 @@ fun filtering(): List<GenericFunction> {
|
||||
returns(Sequences) { "Sequence<T>" }
|
||||
body(Sequences) {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
return when {
|
||||
n == 0 -> emptySequence()
|
||||
this is DropTakeSequence -> this.take(n)
|
||||
@@ -156,10 +156,10 @@ fun filtering(): List<GenericFunction> {
|
||||
|
||||
doc { "Returns a list containing all elements except last [n] elements." }
|
||||
returns("List<T>")
|
||||
body {
|
||||
body { f ->
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
return take((size - n).coerceAtLeast(0))
|
||||
require(n >= 0) { "Requested ${f.doc.element} count $n is less than zero." }
|
||||
return take((${f.code.size} - n).coerceAtLeast(0))
|
||||
"""
|
||||
}
|
||||
|
||||
@@ -167,13 +167,6 @@ fun filtering(): List<GenericFunction> {
|
||||
doc(CharSequences) { "Returns a subsequence of this char sequence with the last [n] characters removed." }
|
||||
returns(Strings, CharSequences) { "SELF" }
|
||||
|
||||
body(Strings, CharSequences) {
|
||||
"""
|
||||
require(n >= 0, { "Requested character count $n is less than zero." })
|
||||
return take((length - n).coerceAtLeast(0))
|
||||
"""
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
templates add f("takeLast(n: Int)") {
|
||||
@@ -187,7 +180,7 @@ fun filtering(): List<GenericFunction> {
|
||||
returns(Strings, CharSequences) { "SELF" }
|
||||
body(Strings, CharSequences) { f ->
|
||||
"""
|
||||
require(n >= 0, { "Requested character count $n is less than zero." })
|
||||
require(n >= 0) { "Requested character count $n is less than zero." }
|
||||
val length = length
|
||||
return ${subsequence(f, "length - n.coerceAtMost(length)")}
|
||||
"""
|
||||
@@ -195,7 +188,7 @@ fun filtering(): List<GenericFunction> {
|
||||
|
||||
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
require(n >= 0) { "Requested element count $n is less than zero." }
|
||||
if (n == 0) return emptyList()
|
||||
val size = size
|
||||
if (n >= size) return toList()
|
||||
|
||||
Reference in New Issue
Block a user