added Array<T>?.orElse(), String.toRegex() and collection?.filterNulls() to convert a possibly nullable collection of T? into a collection T for KT-1129
This commit is contained in:
@@ -102,7 +102,11 @@ inline val ByteArray.inputStream : ByteArrayInputStream
|
||||
inline fun ByteArray.inputStream(offset: Int, length: Int) = ByteArrayInputStream(this, offset, length)
|
||||
|
||||
/** Returns true if the array is not empty */
|
||||
inline fun <T> Array<T>.notEmpty() : Boolean = this.size > 0
|
||||
inline fun <T> Array<T>.notEmpty() : Boolean = !this.isEmpty()
|
||||
|
||||
/** Returns true if the array is empty */
|
||||
inline fun <T> Array<T>.isEmpty() : Boolean = this.size == 0
|
||||
|
||||
/** Returns the array if its not null or else returns an empty array */
|
||||
inline fun <T> Array<T?>?.orEmpty() : Array<T?> = if (this != null) this else array<T?>()
|
||||
|
||||
|
||||
@@ -53,6 +53,20 @@ inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, pr
|
||||
return result
|
||||
}
|
||||
|
||||
/** Returns a List containing all the non null elements in this collection */
|
||||
inline fun <T> java.lang.Iterable<T?>?.filterNulls() : Collection<T> = filterNullsTo(java.util.ArrayList<T>())
|
||||
|
||||
/** Filters all the null elements in this collection winto the given result collection */
|
||||
inline fun <T, C: Collection<in T>> java.lang.Iterable<T?>?.filterNullsTo(result: C) : C {
|
||||
if (this != null) {
|
||||
for (elem in this) {
|
||||
if (elem != null)
|
||||
result.add(elem)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
|
||||
inline fun <T> java.lang.Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package kotlin.util
|
||||
|
||||
import java.util.*
|
||||
import java.util.regex.Pattern
|
||||
|
||||
/** Returns the size of the collection */
|
||||
val Collection<*>.size : Int
|
||||
@@ -103,3 +104,15 @@ inline fun <T> java.util.Collection<T>.notEmpty() : Boolean = !this.isEmpty()
|
||||
inline fun <T> java.util.Collection<T>?.orEmpty() : Collection<T>
|
||||
= if (this != null) this else Collections.EMPTY_LIST as Collection<T>
|
||||
|
||||
/** Converts the string into a regular expression [[Pattern]] so that strings can be split or matched on */
|
||||
inline fun String.toRegex(): Pattern {
|
||||
return Pattern.compile(this).sure()
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the string into a regular expression [[Pattern]] with the given flags from [[Pattern]] or'd together
|
||||
* so that strings can be split or matched on
|
||||
*/
|
||||
inline fun String.toRegex(flags: Int): Pattern {
|
||||
return Pattern.compile(this, flags).sure()
|
||||
}
|
||||
@@ -56,6 +56,20 @@ inline fun <T, C: Collection<in T>> Array<T>.filterTo(result: C, predicate: (T)-
|
||||
return result
|
||||
}
|
||||
|
||||
/** Returns a List containing all the non null elements in this collection */
|
||||
inline fun <T> Array<T?>?.filterNulls() : Collection<T> = filterNullsTo(java.util.ArrayList<T>())
|
||||
|
||||
/** Filters all the null elements in this collection winto the given result collection */
|
||||
inline fun <T, C: Collection<in T>> Array<T?>?.filterNullsTo(result: C) : C {
|
||||
if (this != null) {
|
||||
for (elem in this) {
|
||||
if (elem != null)
|
||||
result.add(elem)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
|
||||
inline fun <T> Array<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
|
||||
|
||||
@@ -160,7 +174,7 @@ inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
|
||||
/** Converts the collection into a LinkedList */
|
||||
inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
|
||||
|
||||
/** Converts the collection into a List */
|
||||
/** Converts the collection into a List */
|
||||
inline fun <T> Array<T>.toList() : List<T> = this.to(ArrayList<T>())
|
||||
|
||||
/** Converts the collection into a Set */
|
||||
|
||||
@@ -56,6 +56,8 @@ inline fun <T, C: Collection<in T>> Iterable<T>.filterTo(result: C, predicate: (
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
|
||||
inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
|
||||
|
||||
@@ -160,7 +162,7 @@ inline fun <T, C: Collection<T>> Iterable<T>.to(result: C) : C {
|
||||
/** Converts the collection into a LinkedList */
|
||||
inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
|
||||
|
||||
/** Converts the collection into a List */
|
||||
/** Converts the collection into a List */
|
||||
inline fun <T> Iterable<T>.toList() : List<T> = this.to(ArrayList<T>())
|
||||
|
||||
/** Converts the collection into a Set */
|
||||
|
||||
@@ -59,25 +59,25 @@ fun main(args: Array<String>) {
|
||||
|
||||
// JavaIterables - Generic iterable stuff
|
||||
generateFile(File(outDir, "ArraysFromJavaIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JavaIterables.kt")) {
|
||||
it.replaceAll("java.lang.Iterable<T>", "Array<T>")
|
||||
it.replaceAll("java.lang.Iterable<T", "Array<T").replaceAll("java.lang.Iterable<T", "Array<T")
|
||||
}
|
||||
|
||||
generateFile(File(outDir, "StandardFromJavaIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JavaIterables.kt")) {
|
||||
it.replaceAll("java.lang.Iterable<T>", "Iterable<T>")
|
||||
it.replaceAll("java.lang.Iterable<T", "Iterable<T")
|
||||
}
|
||||
|
||||
|
||||
// JavaCollections - methods returning a collection of the same input size (if its a collection)
|
||||
|
||||
generateFile(File(outDir, "ArraysFromJavaCollections.kt"), "package kotlin", File(srcDir, "JavaCollections.kt")) {
|
||||
it.replaceAll("java.util.Collection<T>", "Array<T>")
|
||||
it.replaceAll("java.util.Collection<T", "Array<T")
|
||||
}
|
||||
|
||||
generateFile(File(outDir, "JavaUtilIterablesFromJavaCollections.kt"), "package kotlin.util", File(srcDir, "JavaCollections.kt")) {
|
||||
it.replaceAll("java.util.Collection<T>", "java.lang.Iterable<T>").replaceAll("(this.size)", "")
|
||||
it.replaceAll("java.util.Collection<T", "java.lang.Iterable<T").replaceAll("(this.size)", "")
|
||||
}
|
||||
|
||||
generateFile(File(outDir, "StandardFromJavaCollections.kt"), "package kotlin", File(srcDir, "JavaCollections.kt")) {
|
||||
it.replaceAll("java.util.Collection<T>", "Iterable<T>").replaceAll("(this.size)", "")
|
||||
it.replaceAll("java.util.Collection<T", "Iterable<T").replaceAll("(this.size)", "")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package test.string
|
||||
|
||||
import kotlin.*
|
||||
import kotlin.test.*
|
||||
import kotlin.util.*
|
||||
|
||||
import junit.framework.*
|
||||
|
||||
class StringUtilTest() : TestCase() {
|
||||
|
||||
fun testToRegex() {
|
||||
val re = """foo""".toRegex()
|
||||
val list = re.split("hellofoobar").filterNulls()
|
||||
assertEquals(arrayList("hello", "bar"), list)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user