More standard library documentation.

This commit is contained in:
Dmitry Jemerov
2015-03-05 19:07:02 +01:00
parent cb27797845
commit 32072d0a4f
44 changed files with 1702 additions and 136 deletions
+245
View File
@@ -6,198 +6,443 @@ package kotlin
//
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Byte.downTo(to: Byte): ByteProgression {
return ByteProgression(this, to, -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Byte.downTo(to: Char): CharProgression {
return CharProgression(this.toChar(), to, -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Byte.downTo(to: Short): ShortProgression {
return ShortProgression(this.toShort(), to, -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Byte.downTo(to: Int): IntProgression {
return IntProgression(this.toInt(), to, -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Byte.downTo(to: Long): LongProgression {
return LongProgression(this.toLong(), to, -1.toLong())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Byte.downTo(to: Float): FloatProgression {
return FloatProgression(this.toFloat(), to, -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Byte.downTo(to: Double): DoubleProgression {
return DoubleProgression(this.toDouble(), to, -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Char.downTo(to: Byte): CharProgression {
return CharProgression(this, to.toChar(), -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Char.downTo(to: Char): CharProgression {
return CharProgression(this, to, -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Char.downTo(to: Short): ShortProgression {
return ShortProgression(this.toShort(), to, -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Char.downTo(to: Int): IntProgression {
return IntProgression(this.toInt(), to, -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Char.downTo(to: Long): LongProgression {
return LongProgression(this.toLong(), to, -1.toLong())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Char.downTo(to: Float): FloatProgression {
return FloatProgression(this.toFloat(), to, -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Char.downTo(to: Double): DoubleProgression {
return DoubleProgression(this.toDouble(), to, -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Short.downTo(to: Byte): ShortProgression {
return ShortProgression(this, to.toShort(), -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Short.downTo(to: Char): ShortProgression {
return ShortProgression(this, to.toShort(), -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Short.downTo(to: Short): ShortProgression {
return ShortProgression(this, to, -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Short.downTo(to: Int): IntProgression {
return IntProgression(this.toInt(), to, -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Short.downTo(to: Long): LongProgression {
return LongProgression(this.toLong(), to, -1.toLong())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Short.downTo(to: Float): FloatProgression {
return FloatProgression(this.toFloat(), to, -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Short.downTo(to: Double): DoubleProgression {
return DoubleProgression(this.toDouble(), to, -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Int.downTo(to: Byte): IntProgression {
return IntProgression(this, to.toInt(), -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Int.downTo(to: Char): IntProgression {
return IntProgression(this, to.toInt(), -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Int.downTo(to: Short): IntProgression {
return IntProgression(this, to.toInt(), -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Int.downTo(to: Int): IntProgression {
return IntProgression(this, to, -1)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Int.downTo(to: Long): LongProgression {
return LongProgression(this.toLong(), to, -1.toLong())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Int.downTo(to: Float): FloatProgression {
return FloatProgression(this.toFloat(), to, -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Int.downTo(to: Double): DoubleProgression {
return DoubleProgression(this.toDouble(), to, -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Long.downTo(to: Byte): LongProgression {
return LongProgression(this, to.toLong(), -1.toLong())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Long.downTo(to: Char): LongProgression {
return LongProgression(this, to.toLong(), -1.toLong())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Long.downTo(to: Short): LongProgression {
return LongProgression(this, to.toLong(), -1.toLong())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Long.downTo(to: Int): LongProgression {
return LongProgression(this, to.toLong(), -1.toLong())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Long.downTo(to: Long): LongProgression {
return LongProgression(this, to, -1.toLong())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Long.downTo(to: Float): FloatProgression {
return FloatProgression(this.toFloat(), to, -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Long.downTo(to: Double): DoubleProgression {
return DoubleProgression(this.toDouble(), to, -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Float.downTo(to: Byte): FloatProgression {
return FloatProgression(this, to.toFloat(), -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Float.downTo(to: Char): FloatProgression {
return FloatProgression(this, to.toFloat(), -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Float.downTo(to: Short): FloatProgression {
return FloatProgression(this, to.toFloat(), -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Float.downTo(to: Int): FloatProgression {
return FloatProgression(this, to.toFloat(), -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Float.downTo(to: Long): FloatProgression {
return FloatProgression(this, to.toFloat(), -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Float.downTo(to: Float): FloatProgression {
return FloatProgression(this, to, -1.toFloat())
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Float.downTo(to: Double): DoubleProgression {
return DoubleProgression(this.toDouble(), to, -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Double.downTo(to: Byte): DoubleProgression {
return DoubleProgression(this, to.toDouble(), -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Double.downTo(to: Char): DoubleProgression {
return DoubleProgression(this, to.toDouble(), -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Double.downTo(to: Short): DoubleProgression {
return DoubleProgression(this, to.toDouble(), -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Double.downTo(to: Int): DoubleProgression {
return DoubleProgression(this, to.toDouble(), -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Double.downTo(to: Long): DoubleProgression {
return DoubleProgression(this, to.toDouble(), -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Double.downTo(to: Float): DoubleProgression {
return DoubleProgression(this, to.toDouble(), -1.0)
}
/**
* Returns a progression from this value down to the specified [to] value.
*
* The [to] value has to be less than this value.
*/
public fun Double.downTo(to: Double): DoubleProgression {
return DoubleProgression(this, to, -1.0)
}
+23 -23
View File
@@ -700,7 +700,7 @@ public fun String.first(): Char {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun <T> Array<out T>.first(predicate: (T) -> Boolean): T {
@@ -709,7 +709,7 @@ public inline fun <T> Array<out T>.first(predicate: (T) -> Boolean): T {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean {
@@ -718,7 +718,7 @@ public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte {
@@ -727,7 +727,7 @@ public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun CharArray.first(predicate: (Char) -> Boolean): Char {
@@ -736,7 +736,7 @@ public inline fun CharArray.first(predicate: (Char) -> Boolean): Char {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double {
@@ -745,7 +745,7 @@ public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float {
@@ -754,7 +754,7 @@ public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun IntArray.first(predicate: (Int) -> Boolean): Int {
@@ -763,7 +763,7 @@ public inline fun IntArray.first(predicate: (Int) -> Boolean): Int {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun LongArray.first(predicate: (Long) -> Boolean): Long {
@@ -772,7 +772,7 @@ public inline fun LongArray.first(predicate: (Long) -> Boolean): Long {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short {
@@ -781,7 +781,7 @@ public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun <T> Iterable<T>.first(predicate: (T) -> Boolean): T {
@@ -790,7 +790,7 @@ public inline fun <T> Iterable<T>.first(predicate: (T) -> Boolean): T {
}
/**
* "Returns the first element matching the given [predicate].
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun <T> Stream<T>.first(predicate: (T) -> Boolean): T {
@@ -925,7 +925,7 @@ public fun String.firstOrNull(): Char? {
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun <T> Array<out T>.firstOrNull(predicate: (T) -> Boolean): T? {
for (element in this) if (predicate(element)) return element
@@ -933,7 +933,7 @@ public inline fun <T> Array<out T>.firstOrNull(predicate: (T) -> Boolean): T? {
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun BooleanArray.firstOrNull(predicate: (Boolean) -> Boolean): Boolean? {
for (element in this) if (predicate(element)) return element
@@ -941,7 +941,7 @@ public inline fun BooleanArray.firstOrNull(predicate: (Boolean) -> Boolean): Boo
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun ByteArray.firstOrNull(predicate: (Byte) -> Boolean): Byte? {
for (element in this) if (predicate(element)) return element
@@ -949,7 +949,7 @@ public inline fun ByteArray.firstOrNull(predicate: (Byte) -> Boolean): Byte? {
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? {
for (element in this) if (predicate(element)) return element
@@ -957,7 +957,7 @@ public inline fun CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? {
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun DoubleArray.firstOrNull(predicate: (Double) -> Boolean): Double? {
for (element in this) if (predicate(element)) return element
@@ -965,7 +965,7 @@ public inline fun DoubleArray.firstOrNull(predicate: (Double) -> Boolean): Doubl
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun FloatArray.firstOrNull(predicate: (Float) -> Boolean): Float? {
for (element in this) if (predicate(element)) return element
@@ -973,7 +973,7 @@ public inline fun FloatArray.firstOrNull(predicate: (Float) -> Boolean): Float?
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun IntArray.firstOrNull(predicate: (Int) -> Boolean): Int? {
for (element in this) if (predicate(element)) return element
@@ -981,7 +981,7 @@ public inline fun IntArray.firstOrNull(predicate: (Int) -> Boolean): Int? {
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun LongArray.firstOrNull(predicate: (Long) -> Boolean): Long? {
for (element in this) if (predicate(element)) return element
@@ -989,7 +989,7 @@ public inline fun LongArray.firstOrNull(predicate: (Long) -> Boolean): Long? {
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun ShortArray.firstOrNull(predicate: (Short) -> Boolean): Short? {
for (element in this) if (predicate(element)) return element
@@ -997,7 +997,7 @@ public inline fun ShortArray.firstOrNull(predicate: (Short) -> Boolean): Short?
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun <T> Iterable<T>.firstOrNull(predicate: (T) -> Boolean): T? {
for (element in this) if (predicate(element)) return element
@@ -1005,7 +1005,7 @@ public inline fun <T> Iterable<T>.firstOrNull(predicate: (T) -> Boolean): T? {
}
/**
* Returns first element matching the given [predicate], or `null` if element was not found
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun <T> Stream<T>.firstOrNull(predicate: (T) -> Boolean): T? {
for (element in this) if (predicate(element)) return element
@@ -1013,7 +1013,7 @@ public inline fun <T> Stream<T>.firstOrNull(predicate: (T) -> Boolean): T? {
}
/**
* Returns first character matching the given [predicate], or `null` if character was not found
* Returns the first character matching the given [predicate], or `null` if character was not found
*/
public inline fun String.firstOrNull(predicate: (Char) -> Boolean): Char? {
for (element in this) if (predicate(element)) return element
@@ -1,5 +1,9 @@
package kotlin
/**
* Returns an array with the specified [size], where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun <reified T> Array(size: Int, init: (Int) -> T): Array<T> {
val result = arrayOfNulls<T>(size)
@@ -75,8 +75,12 @@ public fun ByteArray.toString(charset: String): String = String(this, charset)
*/
public fun ByteArray.toString(charset: Charset): String = String(this, charset)
/**
* Returns an array containing the elements of this collection.
*/
[Intrinsic("kotlin.collections.copyToArray")] public fun <reified T> Collection<T>.copyToArray(): Array<T> =
throw UnsupportedOperationException()
/** Returns the array if it's not null, or an empty array otherwise. */
public inline fun <reified T> Array<out T>?.orEmpty(): Array<out T> = this ?: array<T>()
@@ -17,7 +17,10 @@ public fun <T> Enumeration<T>.iterator(): Iterator<T> = object : Iterator<T> {
public fun <T> Iterator<T>.iterator(): Iterator<T> = this
/**
* Data class representing a value with an index
* Data class representing a value from a collection or sequence, along with its index in that collection or sequence.
*
* @property value the underlying value.
* @property index the index of the value in the collection or sequence.
*/
public data class IndexedValue<out T>(public val index: Int, public val value: T)
@@ -64,11 +64,14 @@ public fun hashSetOf<T>(vararg values: T): HashSet<T> = values.toCollection(Hash
public fun linkedSetOf<T>(vararg values: T): LinkedHashSet<T> = values.toCollection(LinkedHashSet(values.size()))
/**
* Returns an IntRange of the valid indices for this collection.
* Returns an [IntRange] of the valid indices for this collection.
*/
public val Collection<*>.indices: IntRange
get() = 0..size() - 1
/**
* Returns an [IntRange] that starts with zero and ends at the value of this number but does not include it.
*/
public val Int.indices: IntRange
get() = 0..this - 1
@@ -92,5 +95,12 @@ public fun <T> List<T>?.orEmpty(): List<T> = this ?: emptyList()
/** Returns this Set if it's not null and the empty set otherwise. */
public fun <T> Set<T>?.orEmpty(): Set<T> = this ?: emptySet()
/**
* Returns the size of this iterable if it is known, or `null` otherwise.
*/
public fun <T> Iterable<T>.collectionSizeOrNull(): Int? = if (this is Collection<*>) size() else null
/**
* Returns the size of this iterable if it is known, or the specified [default] value otherwise.
*/
public fun <T> Iterable<T>.collectionSizeOrDefault(default: Int): Int = if (this is Collection<*>) size() else default
@@ -2,12 +2,27 @@ package kotlin
import java.util.*
/**
* A sequence that returns values through its iterator. The values are evaluated lazily, and the sequence
* is potentially infinite.
*
* @param T the type of elements in the sequence.
*/
public trait Stream<out T> {
/**
* Returns an iterator that returns the values from the sequence.
*/
public fun iterator(): Iterator<T>
}
/**
* Creates a stream that returns the specified values.
*/
public fun <T> streamOf(vararg elements: T): Stream<T> = elements.stream()
/**
* Creates a stream that returns all values in the specified [progression].
*/
public fun <T> streamOf(progression: Progression<T>): Stream<T> = object : Stream<T> {
override fun iterator(): Iterator<T> = progression.iterator()
}
@@ -60,6 +75,10 @@ public class FilteringStream<T>(private val stream: Stream<T>,
}
}
/**
* A stream which returns the results of applying the given [transformer] function to the values
* in the underlying [stream].
*/
public class TransformingStream<T, R>(private val stream: Stream<T>, private val transformer: (T) -> R) : Stream<R> {
override fun iterator(): Iterator<R> = object : Iterator<R> {
val iterator = stream.iterator()
@@ -72,6 +91,11 @@ public class TransformingStream<T, R>(private val stream: Stream<T>, private val
}
}
/**
* A stream which returns the results of applying the given [transformer] function to the values
* in the underlying [stream], where the transformer function takes the index of the value in the underlying
* stream along with the value itself.
*/
public class TransformingIndexedStream<T, R>(private val stream: Stream<T>, private val transformer: (Int, T) -> R) : Stream<R> {
override fun iterator(): Iterator<R> = object : Iterator<R> {
val iterator = stream.iterator()
@@ -103,6 +127,11 @@ public class IndexingStream<T>(private val stream: Stream<T>) : Stream<IndexedVa
}
}
/**
* A stream which takes the values from two parallel underlying streams, passes them to the given
* [transform] function and returns the values returned by that function. The stream stops returning
* values as soon as one of the underlying streams stops returning values.
*/
public class MergingStream<T1, T2, V>(private val stream1: Stream<T1>,
private val stream2: Stream<T2>,
private val transform: (T1, T2) -> V
@@ -193,6 +222,10 @@ public class Multistream<T>(private val stream: Stream<Stream<T>>) : Stream<T> {
}
}
/**
* A stream that returns at most [count] values from the underlying [stream], and stops returning values
* as soon as that count is reached.
*/
public class TakeStream<T>(private val stream: Stream<T>,
private val count: Int
) : Stream<T> {
@@ -218,6 +251,10 @@ public class TakeStream<T>(private val stream: Stream<T>,
}
}
/**
* A stream that returns values from the underlying [stream] while the [predicate] function returns
* `true`, and stops returning values once the function returns `false` for the next element.
*/
public class TakeWhileStream<T>(private val stream: Stream<T>,
private val predicate: (T) -> Boolean
) : Stream<T> {
@@ -382,7 +419,7 @@ public class FunctionStream<T : Any>(private val producer: () -> T?) : Stream<T>
}
/**
* Returns a stream which invokes the function to calculate the next value on each iteration until the function returns *null*
* Returns a stream which invokes the function to calculate the next value on each iteration until the function returns `null`.
*/
public fun <T : Any> stream(nextFunction: () -> T?): Stream<T> {
return FunctionStream(nextFunction)
@@ -390,7 +427,7 @@ public fun <T : Any> stream(nextFunction: () -> T?): Stream<T> {
/**
* Returns a stream which invokes the function to calculate the next value based on the previous one on each iteration
* until the function returns *null*
* until the function returns `null`.
*/
public /*inline*/ fun <T : Any> stream(initialValue: T, nextFunction: (T) -> T?): Stream<T> =
stream(nextFunction.toGenerator(initialValue))
@@ -54,7 +54,8 @@ public inline fun <T> ReentrantReadWriteLock.write(action: () -> T): T {
}
/**
* Executes the given [action] and await for CountDownLatch
* Executes the given [operation] and awaits for CountDownLatch.
*
* @return the return value of the action.
*/
public fun <T> Int.latch(operation: CountDownLatch.() -> T): T {
@@ -2,36 +2,65 @@ package kotlin.concurrent
import java.util.concurrent.*
/**
* Returns the current thread.
*/
public val currentThread: Thread
get() = Thread.currentThread()
/**
* Exposes the name of this thread as a property.
*/
public var Thread.name: String
get() = getName()
set(value) {
setName(value)
}
/**
* Exposes the daemon flag of this thread as a property.
* The Java Virtual Machine exits when the only threads running are all daemon threads.
*/
public var Thread.daemon: Boolean
get() = isDaemon()
set(value) {
setDaemon(value)
}
/**
* Exposes the alive state of this thread as a property.
*/
public val Thread.alive: Boolean
get() = isAlive()
/**
* Exposes the priority of this thread as a property.
*/
public var Thread.priority: Int
get() = getPriority()
set(value) {
setPriority(value)
}
/**
* Exposes the context class loader of this thread as a property.
*/
public var Thread.contextClassLoader: ClassLoader?
get() = getContextClassLoader()
set(value) {
setContextClassLoader(value)
}
/**
* Creates a thread that runs the specified [block] of code.\
*
* @param start if `true`, the thread is immediately started.
* @param daemon if `true`, the thread is created as a daemon thread. The Java Virtual Machine exits when
* the only threads running are all daemon threads.
* @param contextClassLoader the class loader to use for loading classes and resources in this thread.
* @param name the name of the thread.
* @param priority the priority of the thread.
*/
public fun thread(start: Boolean = true, daemon: Boolean = false, contextClassLoader: ClassLoader? = null, name: String? = null, priority: Int = -1, block: () -> Unit): Thread {
val thread = object : Thread() {
public override fun run() {
@@ -53,15 +82,15 @@ public fun thread(start: Boolean = true, daemon: Boolean = false, contextClassLo
/**
* Allows you to use the executor as a function to
* execute the given block on the [[Executor]].
* execute the given block on the [Executor].
*/
public fun Executor.invoke(action: () -> Unit) {
execute(action)
}
/**
* Allows you to use the executor as a function to
* execute the given block on the [[Executor]].
* Allows you to use the executor service as a function to
* execute the given block on the [ExecutorService].
*/
public fun <T> ExecutorService.invoke(action: () -> T): Future<T> {
return submit(action)
@@ -4,66 +4,123 @@ import java.util.Timer
import java.util.TimerTask
import java.util.Date
/**
* Schedules an [action] to be executed after the specified [delay] (expressed in milliseconds).
*/
public fun Timer.schedule(delay: Long, action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
schedule(task, delay)
return task
}
/**
* Schedules an [action] to be executed at the specified [time].
*/
public fun Timer.schedule(time: Date, action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
schedule(task, time)
return task
}
/**
* Schedules an [action] to be executed periodically, starting after the specified [delay] (expressed
* in milliseconds) and with the interval of [period] milliseconds between the end of the previous task
* and the start of the next one.
*/
public fun Timer.schedule(delay: Long, period: Long, action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
schedule(task, delay, period)
return task
}
/**
* Schedules an [action] to be executed periodically, starting at the specified [time] and with the
* interval of [period] milliseconds between the end of the previous task and the start of the next one.
*/
public fun Timer.schedule(time: Date, period: Long, action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
schedule(task, time, period)
return task
}
/**
* Schedules an [action] to be executed periodically, starting after the specified [delay] (expressed
* in milliseconds) and with the interval of [period] milliseconds between the start of the previous task
* and the start of the next one.
*/
public fun Timer.scheduleAtFixedRate(delay: Long, period: Long, action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
scheduleAtFixedRate(task, delay, period)
return task
}
/**
* Schedules an [action] to be executed periodically, starting at the specified [time] and with the
* interval of [period] milliseconds between the start of the previous task and the start of the next one.
*/
public fun Timer.scheduleAtFixedRate(time: Date, period: Long, action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action)
scheduleAtFixedRate(task, time, period)
return task
}
/**
* Creates a timer that executes the specified [action] periodically, starting after the specified [initialDelay]
* (expressed in milliseconds) and with the interval of [period] milliseconds between the end of the previous task
* and the start of the next one.
*
* @param name the name to use for the thread which is running the timer.
* @param daemon if true, the thread is started as a daemon thread (the VM will exit when only daemon threads are running)
*/
public fun timer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, action: TimerTask.() -> Unit): Timer {
val timer = if (name == null) Timer(daemon) else Timer(name, daemon)
timer.schedule(initialDelay, period, action)
return timer
}
/**
* Creates a timer that executes the specified [action] periodically, starting at the specified [startAt] date
* and with the interval of [period] milliseconds between the end of the previous task and the start of the next one.
*
* @param name the name to use for the thread which is running the timer.
* @param daemon if true, the thread is started as a daemon thread (the VM will exit when only daemon threads are running)
*/
public fun timer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, action: TimerTask.() -> Unit): Timer {
val timer = if (name == null) Timer(daemon) else Timer(name, daemon)
timer.schedule(startAt, period, action)
return timer
}
/**
* Creates a timer that executes the specified [action] periodically, starting after the specified [initialDelay]
* (expressed in milliseconds) and with the interval of [period] milliseconds between the start of the previous task
* and the start of the next one.
*
* @param name the name to use for the thread which is running the timer.
* @param daemon if true, the thread is started as a daemon thread (the VM will exit when only daemon threads are running)
*/
public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, action: TimerTask.() -> Unit): Timer {
val timer = if (name == null) Timer(daemon) else Timer(name, daemon)
timer.scheduleAtFixedRate(initialDelay, period, action)
return timer
}
/**
* Creates a timer that executes the specified [action] periodically, starting at the specified [startAt] date
* and with the interval of [period] milliseconds between the start of the previous task and the start of the next one.
*
* @param name the name to use for the thread which is running the timer.
* @param daemon if true, the thread is started as a daemon thread (the VM will exit when only daemon threads are running)
*/
public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, action: TimerTask.() -> Unit): Timer {
val timer = if (name == null) Timer(daemon) else Timer(name, daemon)
timer.scheduleAtFixedRate(startAt, period, action)
return timer
}
/**
* Wraps the specified [action] in a `TimerTask`.
*/
public fun timerTask(action: TimerTask.() -> Unit): TimerTask = object : TimerTask() {
public override fun run() {
action()
+23 -22
View File
@@ -9,88 +9,88 @@ import java.io.BufferedReader
*/
public val defaultBufferSize: Int = 64 * 1024
/** Prints the given message to [System.out] */
/** Prints the given message to the standard output stream. */
public fun print(message: Any?) {
System.out.print(message)
}
/** Prints the given message to [System.out] */
/** Prints the given message to the standard output stream. */
public fun print(message: Int) {
System.out.print(message)
}
/** Prints the given message to [System.out] */
/** Prints the given message to the standard output stream. */
public fun print(message: Long) {
System.out.print(message)
}
/** Prints the given message to [System.out] */
/** Prints the given message to the standard output stream. */
public fun print(message: Byte) {
System.out.print(message)
}
/** Prints the given message to [System.out] */
/** Prints the given message to the standard output stream. */
public fun print(message: Short) {
System.out.print(message)
}
/** Prints the given message to [System.out] */
/** Prints the given message to the standard output stream. */
public fun print(message: Char) {
System.out.print(message)
}
/** Prints the given message to [System.out] */
/** Prints the given message to the standard output stream. */
public fun print(message: Boolean) {
System.out.print(message)
}
/** Prints the given message to [System.out] */
/** Prints the given message to the standard output stream. */
public fun print(message: Float) {
System.out.print(message)
}
/** Prints the given message to [System.out] */
/** Prints the given message to the standard output stream. */
public fun print(message: Double) {
System.out.print(message)
}
/** Prints the given message to [System.out] */
/** Prints the given message to the standard output stream. */
public fun print(message: CharArray) {
System.out.print(message)
}
/** Prints the given message and newline to [System.out] */
/** Prints the given message and newline to the standard output stream. */
public fun println(message: Any?) {
System.out.println(message)
}
/** Prints the given message and newline to [System.out] */
/** Prints the given message and newline to the standard output stream. */
public fun println(message: Int) {
System.out.println(message)
}
/** Prints the given message and newline to [System.out] */
/** Prints the given message and newline to the standard output stream. */
public fun println(message: Long) {
System.out.println(message)
}
/** Prints the given message and newline to [System.out] */
/** Prints the given message and newline to the standard output stream. */
public fun println(message: Byte) {
System.out.println(message)
}
/** Prints the given message and newline to [System.out] */
/** Prints the given message and newline to the standard output stream. */
public fun println(message: Short) {
System.out.println(message)
}
/** Prints the given message and newline to [System.out] */
/** Prints the given message and newline to the standard output stream. */
public fun println(message: Char) {
System.out.println(message)
}
/** Prints the given message and newline to [System.out] */
/** Prints the given message and newline to the standard output stream. */
public fun println(message: Boolean) {
System.out.println(message)
}
/** Prints the given message and newline to [System.out] */
/** Prints the given message and newline to the standard output stream. */
public fun println(message: Float) {
System.out.println(message)
}
/** Prints the given message and newline to [System.out] */
/** Prints the given message and newline to the standard output stream. */
public fun println(message: Double) {
System.out.println(message)
}
/** Prints the given message and newline to [System.out] */
/** Prints the given message and newline to the standard output stream. */
public fun println(message: CharArray) {
System.out.println(message)
}
/** Prints a newline to [System.out] */
/** Prints a newline to the standard output stream. */
public fun println() {
System.out.println()
}
@@ -137,7 +137,8 @@ private val stdin: BufferedReader = BufferedReader(InputStreamReader(object : In
}))
/**
* Reads a line of input from [System.in]
* Reads a line of input from the standard input stream.
*
* @return the line read or null if the input stream is redirected to a file and the end of file has been reached.
*/
public fun readLine(): String? = stdin.readLine()
@@ -15,20 +15,56 @@
*/
package kotlin.jvm
/**
* A constant holding the minimum value an `Int` can have, -2^31.
*/
public val Int.Default.MIN_VALUE: Int get() = java.lang.Integer.MIN_VALUE
/**
* A constant holding the maximum value an `Int` can have, 2^31-1.
*/
public val Int.Default.MAX_VALUE: Int get() = java.lang.Integer.MAX_VALUE
/**
* A constant holding the smallest positive nonzero value of type `Double`, 2^-1074.
*/
public val Double.Default.MIN_VALUE: Double get() = java.lang.Double.MIN_VALUE
/**
* A constant holding the largest positive finite value of type `Double`, (2-2^-52)*2^1023.
*/
public val Double.Default.MAX_VALUE: Double get() = java.lang.Double.MAX_VALUE
/**
* A constant holding the smallest positive nonzero value of type `Float`, 2^-149.
*/
public val Float.Default.MIN_VALUE: Float get() = java.lang.Float.MIN_VALUE
/**
* * A constant holding the largest positive finite value of type `Float`, (2-2^-23)*2^127.
*/
public val Float.Default.MAX_VALUE: Float get() = java.lang.Float.MAX_VALUE
/**
* A constant holding the minimum value a `Long` can have, -2^63.
*/
public val Long.Default.MIN_VALUE: Long get() = java.lang.Long.MIN_VALUE
/**
* A constant holding the maximum value a `Long` can have, 2^63-1.
*/
public val Long.Default.MAX_VALUE: Long get() = java.lang.Long.MAX_VALUE
/**
* A constant holding the minimum value a `Short` can have, -2^15.
*/
public val Short.Default.MIN_VALUE: Short get() = java.lang.Short.MIN_VALUE
/**
* A constant holding the maximum value a `Short` can have, 2^15-1.
*/
public val Short.Default.MAX_VALUE: Short get() = java.lang.Short.MAX_VALUE
/**
* A constant holding the minimum value a `Byte` can have, -128.
*/
public val Byte.Default.MIN_VALUE: Byte get() = java.lang.Byte.MIN_VALUE
/**
* A constant holding the maximum value a `Byte` can have, 127.
*/
public val Byte.Default.MAX_VALUE: Byte get() = java.lang.Byte.MAX_VALUE
@@ -21,6 +21,7 @@ package kotlin.platform
* which is generated from this element.
* See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/java-interop.html#handling-signature-clashes-with-platformname)
* for more information.
* @property name the name of the element.
*/
public annotation class platformName(public val name: String)
+32 -1
View File
@@ -89,18 +89,49 @@ public fun fails(block: () -> Unit): Throwable? {
}
/**
* A plugin for performing assertions which can reuse JUnit or TestNG
* Abstracts the logic for performing assertions. Specific implementations of [Asserter] can use JUnit
* or TestNG assertion facilities.
*/
public trait Asserter {
/**
* Asserts that the specified value is true.
*
* @param message the message to report if the assertion fails.
*/
public fun assertTrue(message: String, actual: Boolean): Unit
/**
* Asserts that the specified values are equal.
*
* @param message the message to report if the assertion fails.
*/
public fun assertEquals(message: String, expected: Any?, actual: Any?): Unit
/**
* Asserts that the specified values are not equal.
*
* @param message the message to report if the assertion fails.
*/
public fun assertNotEquals(message: String, illegal: Any?, actual: Any?): Unit
/**
* Asserts that the specified value is not null.
*
* @param message the message to report if the assertion fails.
*/
public fun assertNotNull(message: String, actual: Any?): Unit
/**
* Asserts that the specified value is null.
*
* @param message the message to report if the assertion fails.
*/
public fun assertNull(message: String, actual: Any?): Unit
/**
* Fails the current test with the specified message.
*
* @param message the message to report.
*/
public fun fail(message: String): Unit
}
+5 -1
View File
@@ -26,6 +26,10 @@ public inline fun todo(block: ()-> Any) {
private var _asserter: Asserter? = null
/**
* The active implementation of [Asserter]. An implementation of [Asserter] can be provided
* using the [Java service loader](http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html) mechanism.
*/
public var asserter: Asserter
get() {
if (_asserter == null) {
@@ -51,7 +55,7 @@ public var asserter: Asserter
/**
* Default implementation to avoid dependency on JUnit or TestNG
* Default [Asserter] implementation to avoid dependency on JUnit or TestNG.
*/
private class DefaultAsserter() : Asserter {
+27 -5
View File
@@ -16,37 +16,59 @@
package kotlin
/**
* Returns `true` if this character (Unicode code point) is defined in Unicode.
*/
public fun Char.isDefined(): Boolean = Character.isDefined(this)
/**
* Returns `true` if this character (Unicode code point) is a digit.
*/
public fun Char.isDigit(): Boolean = Character.isDigit(this)
/**
* Returns `true` if this character is a Unicode high-surrogate code unit (also known as leading-surrogate code unit).
*/
public fun Char.isHighSurrogate(): Boolean = Character.isHighSurrogate(this)
/**
* Returns `true` if this character (Unicode code point) should be regarded as an ignorable
* character in a Java identifier or a Unicode identifier.
*/
public fun Char.isIdentifierIgnorable(): Boolean = Character.isIdentifierIgnorable(this)
/**
* Returns `true` if this character is an ISO control character.
*/
public fun Char.isISOControl(): Boolean = Character.isISOControl(this)
/**
* Returns `true` if this character (Unicode code point) may be part of a Java identifier as other than the first character.
*/
public fun Char.isJavaIdentifierPart(): Boolean = Character.isJavaIdentifierPart(this)
/**
* Returns `true` if this character is permissible as the first character in a Java identifier.
*/
public fun Char.isJavaIdentifierStart(): Boolean = Character.isJavaIdentifierStart(this)
deprecated("Please use Char.isJavaIdentifierStart() instead")
public fun Char.isJavaLetter(): Boolean = Character.isJavaLetter(this)
deprecated("Please use Char.isJavaIdentifierPart() instead")
public fun Char.isJavaLetterOrDigit(): Boolean = Character.isJavaLetterOrDigit(this)
/**
* Returns true if the character is whitespace
*
* @includeFunctionBody ../../test/text/StringTest.kt count
* Returns `true` if the character is whitespace.
*/
public fun Char.isWhitespace(): Boolean = Character.isWhitespace(this)
/**
* Returns true if this character is upper case
* Returns `true` if this character is upper case.
*/
public fun Char.isUpperCase(): Boolean = Character.isUpperCase(this)
/**
* Returns true if this character is lower case
* Returns `true` if this character is lower case.
*/
public fun Char.isLowerCase(): Boolean = Character.isLowerCase(this)
+245 -2
View File
@@ -7,127 +7,363 @@ import java.util.regex.MatchResult
import java.util.regex.Pattern
import java.nio.charset.Charset
/**
* Returns the index within this string of the last occurrence of the specified substring.
*/
public fun String.lastIndexOf(str: String): Int = (this as java.lang.String).lastIndexOf(str)
/**
* Returns the index within this string of the last occurrence of the specified character.
*/
public fun String.lastIndexOf(ch: Char): Int = (this as java.lang.String).lastIndexOf(ch.toString())
/**
* Compares this string to another string, ignoring case considerations.
*/
public fun String.equalsIgnoreCase(anotherString: String): Boolean = (this as java.lang.String).equalsIgnoreCase(anotherString)
/**
* Returns the hash code of this string.
*/
public fun String.hashCode(): Int = (this as java.lang.String).hashCode()
/**
* Returns the index within this string of the first occurrence of the specified substring.
*/
public fun String.indexOf(str: String): Int = (this as java.lang.String).indexOf(str)
/**
* Returns the index within this string of the first occurrence of the specified substring, starting from the specified offset.
*/
public fun String.indexOf(str: String, fromIndex: Int): Int = (this as java.lang.String).indexOf(str, fromIndex)
/**
* Returns a copy of this string with all occurrences of [oldChar] replaced with [newChar].
*/
public fun String.replace(oldChar: Char, newChar: Char): String = (this as java.lang.String).replace(oldChar, newChar)
/**
* Returns a new string obtained by replacing each substring of this string that matches the given regular expression
* with the given [replacement].
*/
public fun String.replaceAll(regex: String, replacement: String): String = (this as java.lang.String).replaceAll(regex, replacement)
/**
* Returns a copy of this string with leading and trailing whitespace trimmed.
*/
public fun String.trim(): String = (this as java.lang.String).trim()
/**
* Returns a copy of this string converted to upper case using the rules of the default locale.
*/
public fun String.toUpperCase(): String = (this as java.lang.String).toUpperCase()
/**
* Returns a copy of this string converted to lower case using the rules of the default locale.
*/
public fun String.toLowerCase(): String = (this as java.lang.String).toLowerCase()
/**
* Returns a new character array containing the characters from this string.
*/
public fun String.toCharArray(): CharArray = (this as java.lang.String).toCharArray()
/**
* Uses this string as a format string and returns a string obtained by substituting the specified arguments,
* using the default locale.
*/
public fun String.format(vararg args: Any?): String = java.lang.String.format(this, *args)
/**
* Uses this string as a format string and returns a string obtained by substituting the specified arguments, using
* the specified locale.
*/
public fun String.format(locale: Locale, vararg args : Any?) : String = java.lang.String.format(locale, this, *args)
/**
* Splits this string around matches of the given regular expression.
*/
public fun String.split(regex: String): Array<String> = (this as java.lang.String).split(regex)
/**
* Splits this string around occurrences of the specified character.
*/
public fun String.split(ch: Char): Array<String> = (this as java.lang.String).split(java.util.regex.Pattern.quote(ch.toString()))
/**
* Returns a substring of this string starting with the specified index.
*/
public fun String.substring(beginIndex: Int): String = (this as java.lang.String).substring(beginIndex)
/**
* Returns the substring of this string starting and ending at the specified indices.
*/
public fun String.substring(beginIndex: Int, endIndex: Int): String = (this as java.lang.String).substring(beginIndex, endIndex)
/**
* Returns `true` if this string starts with the specified prefix.
*/
public fun String.startsWith(prefix: String): Boolean = (this as java.lang.String).startsWith(prefix)
/**
* Returns `true` if a subsring of this string starting at the specified offset starts with the specified prefix.
*/
public fun String.startsWith(prefix: String, toffset: Int): Boolean = (this as java.lang.String).startsWith(prefix, toffset)
/**
* Returns `true` if this string starts with the specified character.
*/
public fun String.startsWith(ch: Char): Boolean = (this as java.lang.String).startsWith(ch.toString())
/**
* Returns `true` if this string contains the specified sequence of characters as a substring.
*/
public fun String.contains(seq: CharSequence): Boolean = (this as java.lang.String).contains(seq)
/**
* Returns `true` if this string ends with the specified suffix.
*/
public fun String.endsWith(suffix: String): Boolean = (this as java.lang.String).endsWith(suffix)
/**
* Returns `true` if this string ends with the specified character.
*/
public fun String.endsWith(ch: Char): Boolean = (this as java.lang.String).endsWith(ch.toString())
// "constructors" for String
/**
* Converts the data from a portion of the specified array of bytes to characters using the specified character set
* and returns the conversion result as a string.
*
* @param bytes the source array for the conversion.
* @param offset the offset in the array of the data to be converted.
* @param length the number of bytes to be converted.
* @param charsetName the name of the character set to use.
*/
public fun String(bytes: ByteArray, offset: Int, length: Int, charsetName: String): String = java.lang.String(bytes, offset, length, charsetName) as String
/**
* Converts the data from a portion of the specified array of bytes to characters using the specified character set
* and returns the conversion result as a string.
*
* @param bytes the source array for the conversion.
* @param offset the offset in the array of the data to be converted.
* @param length the number of bytes to be converted.
* @param charset the character set to use.
*/
public fun String(bytes: ByteArray, offset: Int, length: Int, charset: Charset): String = java.lang.String(bytes, offset, length, charset) as String
/**
* Converts the data from the specified array of bytes to characters using the specified character set
* and returns the conversion result as a string.
*/
public fun String(bytes: ByteArray, charsetName: String): String = java.lang.String(bytes, charsetName) as String
/**
* Converts the data from the specified array of bytes to characters using the specified character set
* and returns the conversion result as a string.
*/
public fun String(bytes: ByteArray, charset: Charset): String = java.lang.String(bytes, charset) as String
public fun String(bytes: ByteArray, i: Int, i1: Int): String = java.lang.String(bytes, i, i1, Charsets.UTF_8) as String
/**
* Converts the data from a portion of the specified array of bytes to characters using the UTF-8 character set
* and returns the conversion result as a string.
*
* @param bytes the source array for the conversion.
* @param offset the offset in the array of the data to be converted.
* @param length the number of bytes to be converted.
*/
public fun String(bytes: ByteArray, offset: Int, length: Int): String = java.lang.String(bytes, offset, length, Charsets.UTF_8) as String
/**
* Converts the data from the specified array of bytes to characters using the UTF-8 character set
* and returns the conversion result as a string.
*/
public fun String(bytes: ByteArray): String = java.lang.String(bytes, Charsets.UTF_8) as String
/**
* Converts the characters in the specified array to a string.
*/
public fun String(chars: CharArray): String = java.lang.String(chars) as String
/**
* Converts the contents of the specified StringBuffer to a string.
*/
public fun String(stringBuffer: java.lang.StringBuffer): String = java.lang.String(stringBuffer) as String
/**
* Converts the contents of the specified StringBuilder to a string.
*/
public fun String(stringBuilder: java.lang.StringBuilder): String = java.lang.String(stringBuilder) as String
/**
* Replaces the first substring of this string that matches the given regular expression with the given replacement.
*/
public fun String.replaceFirst(regex: String, replacement: String): String = (this as java.lang.String).replaceFirst(regex, replacement)
/**
* Splits this string into at most [limit] chunks around matches of the given regular expression.
*/
public fun String.split(regex: String, limit: Int): Array<String> = (this as java.lang.String).split(regex, limit)
/**
* Returns the character (Unicode code point) at the specified index.
*/
public fun String.codePointAt(index: Int): Int = (this as java.lang.String).codePointAt(index)
/**
* Returns the character (Unicode code point) before the specified index.
*/
public fun String.codePointBefore(index: Int): Int = (this as java.lang.String).codePointBefore(index)
/**
* Returns the number of Unicode code points in the specified text range of this String.
*/
public fun String.codePointCount(beginIndex: Int, endIndex: Int): Int = (this as java.lang.String).codePointCount(beginIndex, endIndex)
/**
* Compares two strings lexicographically, ignoring case differences.
*/
public fun String.compareToIgnoreCase(str: String): Int = (this as java.lang.String).compareToIgnoreCase(str)
/**
* Returns a new string obtained by concatenating this string and the specified string.
*/
public fun String.concat(str: String): String = (this as java.lang.String).concat(str)
/**
* Returns `true` if this string is equal to the contents of the specified CharSequence.
*/
public fun String.contentEquals(cs: CharSequence): Boolean = (this as java.lang.String).contentEquals(cs)
/**
* Returns `true` if this string is equal to the contents of the specified StringBuffer.
*/
public fun String.contentEquals(sb: StringBuffer): Boolean = (this as java.lang.String).contentEquals(sb)
/**
* Copies the characters from a substring of this string into the specified character array.
* @param srcBegin the start offset (inclusive) of the substring to copy.
* @param srcEnd the end offset (exclusive) of the substring to copy.
* @param dst the array to copy to.
* @param dstBegin the position in the array to copy to.
*/
public fun String.getChars(srcBegin: Int, srcEnd: Int, dst: CharArray, dstBegin: Int): Unit = (this as java.lang.String).getChars(srcBegin, srcEnd, dst, dstBegin)
/**
* Returns the index within this string of the first occurrence of the specified character.
*/
public fun String.indexOf(ch: Char): Int = (this as java.lang.String).indexOf(ch.toString())
/**
* Returns the index within this string of the first occurrence of the specified character, starting from the specified offset.
*/
public fun String.indexOf(ch: Char, fromIndex: Int): Int = (this as java.lang.String).indexOf(ch.toString(), fromIndex)
/**
* Returns a canonical representation for this string object.
*/
public fun String.intern(): String = (this as java.lang.String).intern()
/**
* Returns `true` if this string is empty (contains no characters).
*/
public fun String.isEmpty(): Boolean = (this as java.lang.String).isEmpty()
/**
* Returns the index within this string of the last occurrence of the specified character.
*/
public fun String.lastIndexOf(ch: Char, fromIndex: Int): Int = (this as java.lang.String).lastIndexOf(ch.toString(), fromIndex)
/**
* Returns the index within this string of the last occurrence of the specified character, starting from the specified offset.
*/
public fun String.lastIndexOf(str: String, fromIndex: Int): Int = (this as java.lang.String).lastIndexOf(str, fromIndex)
/**
* Returns `true` if this string matches the given regular expression.
*/
public fun String.matches(regex: String): Boolean = (this as java.lang.String).matches(regex)
/**
* Returns the index within this string that is offset from the given [index] by [codePointOffset] code points.
*/
public fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int = (this as java.lang.String).offsetByCodePoints(index, codePointOffset)
/**
* Returns `true` if the specified range in this string is equal to the specified range in another string.
* @param ignoreCase if `true`, character case is ignored when comparing.
* @param toffset the start offset in this string of the substring to compare.
* @param other the string against a substring of which the comparison is performed.
* @param ooffset the start offset in the other string of the substring to compare.
* @param len the length of the substring to compare.
*/
public fun String.regionMatches(ignoreCase: Boolean, toffset: Int, other: String, ooffset: Int, len: Int): Boolean = (this as java.lang.String).regionMatches(ignoreCase, toffset, other, ooffset, len)
/**
* Returns `true` if the specified range in this string is equal to the specified range in another string.
* @param toffset the start offset in this string of the substring to compare.
* @param other the string against a substring of which the comparison is performed.
* @param ooffset the start offset in the other string of the substring to compare.
* @param len the length of the substring to compare.
*/
public fun String.regionMatches(toffset: Int, other: String, ooffset: Int, len: Int): Boolean = (this as java.lang.String).regionMatches(toffset, other, ooffset, len)
/**
* Returns a new string obtained by replacing all occurrences of the [target] substring in this string
* with the specified [replacement] string.
*/
public fun String.replace(target: CharSequence, replacement: CharSequence): String = (this as java.lang.String).replace(target, replacement)
/**
* Returns a copy of this string converted to lower case using the rules of the specified locale.
*/
public fun String.toLowerCase(locale: java.util.Locale): String = (this as java.lang.String).toLowerCase(locale)
/**
* Returns a copy of this string converted to upper case using the rules of the specified locale.
*/
public fun String.toUpperCase(locale: java.util.Locale): String = (this as java.lang.String).toUpperCase(locale)
/**
* Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise.
*/
public fun String.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this)
/**
* Parses the string as a [Short] number and returns the result.
* @throws NumberFormatException if the string is not a valid representation of a number.
*/
public fun String.toShort(): Short = java.lang.Short.parseShort(this)
/**
* Parses the string as an [Int] number and returns the result.
* @throws NumberFormatException if the string is not a valid representation of a number.
*/
public fun String.toInt(): Int = java.lang.Integer.parseInt(this)
/**
* Parses the string as a [Long] number and returns the result.
* @throws NumberFormatException if the string is not a valid representation of a number.
*/
public fun String.toLong(): Long = java.lang.Long.parseLong(this)
/**
* Parses the string as a [Float] number and returns the result.
* @throws NumberFormatException if the string is not a valid representation of a number.
*/
public fun String.toFloat(): Float = java.lang.Float.parseFloat(this)
/**
* Parses the string as a [Double] number and returns the result.
* @throws NumberFormatException if the string is not a valid representation of a number.
*/
public fun String.toDouble(): Double = java.lang.Double.parseDouble(this)
/**
* Returns the list of all characters in this string.
*/
public fun String.toCharList(): List<Char> = toCharArray().toList()
/**
@@ -138,7 +374,14 @@ public fun String.toCharList(): List<Char> = toCharArray().toList()
*/
public fun CharSequence.get(start: Int, end: Int): CharSequence = subSequence(start, end)
/**
* Encodes the contents of this string using the specified character set and returns the resulting byte array.
*/
public fun String.toByteArray(charset: String): ByteArray = (this as java.lang.String).getBytes(charset)
/**
* Encodes the contents of this string using the specified character set and returns the resulting byte array.
*/
public fun String.toByteArray(charset: Charset = Charsets.UTF_8): ByteArray = (this as java.lang.String).getBytes(charset)
deprecated("Use toByteArray() instead to emphasize copy behaviour")
@@ -151,7 +394,7 @@ deprecated("Use toByteArray(charset) instead to emphasize copy behaviour")
public fun String.getBytes(charset: String): ByteArray = (this as java.lang.String).getBytes(charset)
/**
* Returns a subsequence specified by given [range].
* Returns a subsequence of this sequence specified by given [range].
*/
public fun CharSequence.slice(range: IntRange): CharSequence {
return subSequence(range.start, range.end + 1) // inclusive
+39 -1
View File
@@ -4,42 +4,80 @@ package kotlin
* Defines names for Unicode symbols used in proper Typography
*/
public object Typography {
/** The character &#x22; */
public val quote: Char = '\u0022'
/** The character &#x26; */
public val amp: Char = '\u0026'
/** The character &#x3C; */
public val less: Char = '\u003C'
/** The character &#x3E; */
public val greater: Char = '\u003E'
/** The non-breaking space character */
public val nbsp: Char = '\u00A0'
/** The character &#xD7; */
public val times: Char = '\u00D7'
/** The character &#xA2; */
public val cent: Char = '\u00A2'
/** The character &#xA3; */
public val pound: Char = '\u00A3'
/** The character &#xA7; */
public val section: Char = '\u00A7'
/** The character &#xA9; */
public val copyright: Char = '\u00A9'
/** The character &#xAB; */
public val leftGuillemete: Char = '\u00AB'
/** The character &#xBB; */
public val rightGuillemete: Char = '\u00BB'
/** The character &#xAE; */
public val registered: Char = '\u00AE'
/** The character &#xB0; */
public val degree: Char = '\u00B0'
/** The character &#xB1; */
public val plusMinus: Char = '\u00B1'
/** The character &#xB6; */
public val paragraph: Char = '\u00B6'
/** The character &#xB7; */
public val middleDot: Char = '\u00B7'
/** The character &#xBD; */
public val half: Char = '\u00BD'
/** The character &#x2013; */
public val ndash: Char = '\u2013'
/** The character &#x2014; */
public val mdash: Char = '\u2014'
/** The character &#x2018; */
public val leftSingleQuote: Char = '\u2018'
/** The character &#x2019; */
public val rightSingleQuote: Char = '\u2019'
/** The character &#x201A; */
public val lowSingleQuote: Char = '\u201A'
/** The character &#x201C; */
public val leftDoubleQuote: Char = '\u201C'
/** The character &#x201D; */
public val rightDoubleQuote: Char = '\u201D'
public val lowDoubleQuote: Char = '\u201D'
/** The character &#x201E; */
public val lowDoubleQuote: Char = '\u201E'
/** The character &#x2020; */
public val dagger: Char = '\u2020'
/** The character &#x2021; */
public val doubleDagger: Char = '\u2021'
/** The character &#x2022; */
public val bullet: Char = '\u2022'
/** The character &#x2026; */
public val ellipsis: Char = '\u2026'
/** The character &#x2032; */
public val prime: Char = '\u2032'
/** The character &#x2033; */
public val doublePrime: Char = '\u2033'
/** The character &#x20AC; */
public val euro: Char = '\u20AC'
/** The character &#x2122; */
public val tm: Char = '\u2122'
/** The character &#x2248; */
public val almostEqual: Char = '\u2248'
/** The character &#x2260; */
public val notEqual: Char = '\u2260'
/** The character &#x2264; */
public val lessOrEqual: Char = '\u2264'
/** The character &#x2265; */
public val greaterOrEqual: Char = '\u2265'
}
@@ -1,5 +1,10 @@
package kotlin
/**
* Converts a function that takes one argument and returns a value of the same type to a generator function.
* The generator function calls this function, passing to it either [initialValue] on the first iteration
* or the previously returned value on subsequent iterations, and returns the returned value.
*/
public fun <T: Any> Function1<T, T?>.toGenerator(initialValue: T): Function0<T?> {
var nextValue: T? = initialValue
return {
+6 -1
View File
@@ -6,7 +6,7 @@ import kotlin.jvm.internal.unsafe.*
import kotlin.jvm.internal.Intrinsic
/**
* This annotation indicates what exceptions should be declared by a function when compiled to a JVM method
* This annotation indicates what exceptions should be declared by a function when compiled to a JVM method.
*
* Example:
*
@@ -20,6 +20,8 @@ import kotlin.jvm.internal.Intrinsic
* ```
* String readFile(String name) throws IOException {...}
* ```
*
* @property exceptionClasses the list of checked exception classes that may be thrown by the function.
*/
Retention(RetentionPolicy.SOURCE)
public annotation class throws(public vararg val exceptionClasses: Class<out Throwable>)
@@ -48,5 +50,8 @@ public inline fun <R> synchronized(lock: Any, block: () -> R): R {
}
}
/**
* Returns the annotation type of this annotation.
*/
public fun <T : Annotation> T.annotationType() : Class<out T> =
(this as java.lang.annotation.Annotation).annotationType() as Class<out T>
+7 -7
View File
@@ -13,8 +13,8 @@ import java.io.Serializable
*
* @param A type of the first value
* @param B type of the second value
* @param first First value
* @param second Second value
* @property first First value
* @property second Second value
* @constructor Creates a new instance of Pair.
*/
public data class Pair<out A, out B>(
@@ -29,7 +29,7 @@ public data class Pair<out A, out B>(
}
/**
* Converts a pair into a list.
* Converts this pair into a list.
*/
public fun <T> Pair<T, T>.toList(): List<T> = listOf(first, second)
@@ -44,9 +44,9 @@ public fun <T> Pair<T, T>.toList(): List<T> = listOf(first, second)
* @param A type of the first value
* @param B type of the second value
* @param C type of the third value
* @param first First value
* @param second Second value
* @param third Third value
* @property first First value
* @property second Second value
* @property third Third value
*/
public data class Triple<out A, out B, out C>(
public val first: A,
@@ -61,6 +61,6 @@ public data class Triple<out A, out B, out C>(
}
/**
* Converts triple into a list
* Converts this triple into a list.
*/
public fun <T> Triple<T, T, T>.toList(): List<T> = listOf(first, second, third)