core: cleanup 'public', property access syntax
This commit is contained in:
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.storage
|
||||
|
||||
import java.util.concurrent.ConcurrentMap
|
||||
|
||||
public abstract class ObservableStorageManager(private val delegate: StorageManager) : StorageManager {
|
||||
abstract class ObservableStorageManager(private val delegate: StorageManager) : StorageManager {
|
||||
protected abstract val <T> (() -> T).observable: () -> T
|
||||
protected abstract val <K, V> ((K) -> V).observable: (K) -> V
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.storage
|
||||
|
||||
import java.util.concurrent.ConcurrentMap
|
||||
|
||||
public interface StorageManager {
|
||||
interface StorageManager {
|
||||
/**
|
||||
* Given a function compute: K -> V create a memoized version of it that computes a value only once for each key
|
||||
* @param compute the function to be memoized
|
||||
@@ -27,17 +27,17 @@ public interface StorageManager {
|
||||
* NOTE: if compute() has side-effects the WEAK reference kind is dangerous: the side-effects will be repeated if
|
||||
* the value gets collected and then re-computed
|
||||
*/
|
||||
public fun <K, V : Any> createMemoizedFunction(compute: (K) -> V): MemoizedFunctionToNotNull<K, V>
|
||||
fun <K, V : Any> createMemoizedFunction(compute: (K) -> V): MemoizedFunctionToNotNull<K, V>
|
||||
|
||||
public fun <K, V : Any> createMemoizedFunctionWithNullableValues(compute: (K) -> V?): MemoizedFunctionToNullable<K, V>
|
||||
fun <K, V : Any> createMemoizedFunctionWithNullableValues(compute: (K) -> V?): MemoizedFunctionToNullable<K, V>
|
||||
|
||||
public fun <K, V : Any> createMemoizedFunction(compute: (K) -> V, map: ConcurrentMap<K, Any>): MemoizedFunctionToNotNull<K, V>
|
||||
fun <K, V : Any> createMemoizedFunction(compute: (K) -> V, map: ConcurrentMap<K, Any>): MemoizedFunctionToNotNull<K, V>
|
||||
|
||||
public fun <K, V : Any> createMemoizedFunctionWithNullableValues(compute: (K) -> V, map: ConcurrentMap<K, Any>): MemoizedFunctionToNullable<K, V>
|
||||
fun <K, V : Any> createMemoizedFunctionWithNullableValues(compute: (K) -> V, map: ConcurrentMap<K, Any>): MemoizedFunctionToNullable<K, V>
|
||||
|
||||
public fun <T : Any> createLazyValue(computable: () -> T): NotNullLazyValue<T>
|
||||
fun <T : Any> createLazyValue(computable: () -> T): NotNullLazyValue<T>
|
||||
|
||||
public fun <T : Any> createRecursionTolerantLazyValue(computable: () -> T, onRecursiveCall: T): NotNullLazyValue<T>
|
||||
fun <T : Any> createRecursionTolerantLazyValue(computable: () -> T, onRecursiveCall: T): NotNullLazyValue<T>
|
||||
|
||||
/**
|
||||
* @param onRecursiveCall is called if the computation calls itself recursively.
|
||||
@@ -46,17 +46,17 @@ public interface StorageManager {
|
||||
* otherwise it's executed and its result is returned
|
||||
* @param postCompute is called after the value is computed, but before any other thread sees it
|
||||
*/
|
||||
public fun <T : Any> createLazyValueWithPostCompute(computable: () -> T, onRecursiveCall: ((Boolean) -> T)?, postCompute: (T) -> Unit): NotNullLazyValue<T>
|
||||
fun <T : Any> createLazyValueWithPostCompute(computable: () -> T, onRecursiveCall: ((Boolean) -> T)?, postCompute: (T) -> Unit): NotNullLazyValue<T>
|
||||
|
||||
public fun <T : Any> createNullableLazyValue(computable: () -> T?): NullableLazyValue<T>
|
||||
fun <T : Any> createNullableLazyValue(computable: () -> T?): NullableLazyValue<T>
|
||||
|
||||
public fun <T : Any> createRecursionTolerantNullableLazyValue(computable: () -> T?, onRecursiveCall: T?): NullableLazyValue<T>
|
||||
fun <T : Any> createRecursionTolerantNullableLazyValue(computable: () -> T?, onRecursiveCall: T?): NullableLazyValue<T>
|
||||
|
||||
/**
|
||||
* {@code postCompute} is called after the value is computed, but before any other thread sees it (the current thread may
|
||||
* see it in between)
|
||||
*/
|
||||
public fun <T : Any> createNullableLazyValueWithPostCompute(computable: () -> T?, postCompute: (T?) -> Unit): NullableLazyValue<T>
|
||||
fun <T : Any> createNullableLazyValueWithPostCompute(computable: () -> T?, postCompute: (T?) -> Unit): NullableLazyValue<T>
|
||||
|
||||
public fun <T> compute(computable: () -> T): T
|
||||
fun <T> compute(computable: () -> T): T
|
||||
}
|
||||
|
||||
@@ -18,24 +18,24 @@ package org.jetbrains.kotlin.storage
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
public interface MemoizedFunctionToNotNull<P, R : Any> : Function1<P, R> {
|
||||
public fun isComputed(key: P): Boolean
|
||||
interface MemoizedFunctionToNotNull<P, R : Any> : Function1<P, R> {
|
||||
fun isComputed(key: P): Boolean
|
||||
}
|
||||
|
||||
public interface MemoizedFunctionToNullable<P, R : Any> : Function1<P, R?> {
|
||||
public fun isComputed(key: P): Boolean
|
||||
interface MemoizedFunctionToNullable<P, R : Any> : Function1<P, R?> {
|
||||
fun isComputed(key: P): Boolean
|
||||
}
|
||||
|
||||
public interface NotNullLazyValue<T : Any> : Function0<T> {
|
||||
public fun isComputed(): Boolean
|
||||
public fun isComputing(): Boolean
|
||||
interface NotNullLazyValue<T : Any> : Function0<T> {
|
||||
fun isComputed(): Boolean
|
||||
fun isComputing(): Boolean
|
||||
}
|
||||
|
||||
public interface NullableLazyValue<T : Any> : Function0<T?> {
|
||||
public fun isComputed(): Boolean
|
||||
public fun isComputing(): Boolean
|
||||
interface NullableLazyValue<T : Any> : Function0<T?> {
|
||||
fun isComputed(): Boolean
|
||||
fun isComputing(): Boolean
|
||||
}
|
||||
|
||||
public operator fun <T : Any> NotNullLazyValue<T>.getValue(_this: Any?, p: KProperty<*>): T = invoke()
|
||||
operator fun <T : Any> NotNullLazyValue<T>.getValue(_this: Any?, p: KProperty<*>): T = invoke()
|
||||
|
||||
public operator fun <T : Any> NullableLazyValue<T>.getValue(_this: Any?, p: KProperty<*>): T? = invoke()
|
||||
operator fun <T : Any> NullableLazyValue<T>.getValue(_this: Any?, p: KProperty<*>): T? = invoke()
|
||||
|
||||
@@ -20,43 +20,43 @@ import java.lang.reflect.Modifier
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
public fun <T: Any> T?.singletonOrEmptyList(): List<T> = if (this != null) Collections.singletonList(this) else Collections.emptyList()
|
||||
fun <T: Any> T?.singletonOrEmptyList(): List<T> = if (this != null) Collections.singletonList(this) else Collections.emptyList()
|
||||
|
||||
public fun <T> T.singletonList(): List<T> = Collections.singletonList(this)
|
||||
fun <T> T.singletonList(): List<T> = Collections.singletonList(this)
|
||||
|
||||
public fun <T: Any> T?.singletonOrEmptySet(): Set<T> = if (this != null) Collections.singleton(this) else Collections.emptySet()
|
||||
fun <T: Any> T?.singletonOrEmptySet(): Set<T> = if (this != null) Collections.singleton(this) else Collections.emptySet()
|
||||
|
||||
public inline fun <reified T : Any> Sequence<*>.firstIsInstanceOrNull(): T? {
|
||||
inline fun <reified T : Any> Sequence<*>.firstIsInstanceOrNull(): T? {
|
||||
for (element in this) if (element is T) return element
|
||||
return null
|
||||
}
|
||||
|
||||
public inline fun <reified T : Any> Iterable<*>.firstIsInstanceOrNull(): T? {
|
||||
inline fun <reified T : Any> Iterable<*>.firstIsInstanceOrNull(): T? {
|
||||
for (element in this) if (element is T) return element
|
||||
return null
|
||||
}
|
||||
|
||||
public inline fun <reified T : Any> Array<*>.firstIsInstanceOrNull(): T? {
|
||||
inline fun <reified T : Any> Array<*>.firstIsInstanceOrNull(): T? {
|
||||
for (element in this) if (element is T) return element
|
||||
return null
|
||||
}
|
||||
|
||||
public inline fun <reified T> Sequence<*>.firstIsInstance(): T {
|
||||
inline fun <reified T> Sequence<*>.firstIsInstance(): T {
|
||||
for (element in this) if (element is T) return element
|
||||
throw NoSuchElementException("No element of given type found")
|
||||
}
|
||||
|
||||
public inline fun <reified T> Iterable<*>.firstIsInstance(): T {
|
||||
inline fun <reified T> Iterable<*>.firstIsInstance(): T {
|
||||
for (element in this) if (element is T) return element
|
||||
throw NoSuchElementException("No element of given type found")
|
||||
}
|
||||
|
||||
public inline fun <reified T> Array<*>.firstIsInstance(): T {
|
||||
inline fun <reified T> Array<*>.firstIsInstance(): T {
|
||||
for (element in this) if (element is T) return element
|
||||
throw NoSuchElementException("No element of given type found")
|
||||
}
|
||||
|
||||
public inline fun <reified T : Any> Iterable<*>.lastIsInstanceOrNull(): T? {
|
||||
inline fun <reified T : Any> Iterable<*>.lastIsInstanceOrNull(): T? {
|
||||
when (this) {
|
||||
is List<*> -> {
|
||||
for (i in this.indices.reversed()) {
|
||||
@@ -72,18 +72,18 @@ public inline fun <reified T : Any> Iterable<*>.lastIsInstanceOrNull(): T? {
|
||||
}
|
||||
}
|
||||
|
||||
public fun <T> sequenceOfLazyValues(vararg elements: () -> T): Sequence<T> = elements.asSequence().map { it() }
|
||||
fun <T> sequenceOfLazyValues(vararg elements: () -> T): Sequence<T> = elements.asSequence().map { it() }
|
||||
|
||||
public fun <T1, T2> Pair<T1, T2>.swap(): Pair<T2, T1> = Pair(second, first)
|
||||
fun <T1, T2> Pair<T1, T2>.swap(): Pair<T2, T1> = Pair(second, first)
|
||||
|
||||
public fun <T: Any> T.check(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null
|
||||
fun <T: Any> T.check(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null
|
||||
|
||||
public fun <T : Any> constant(calculator: () -> T): T {
|
||||
fun <T : Any> constant(calculator: () -> T): T {
|
||||
val cached = constantMap[calculator]
|
||||
if (cached != null) return cached as T
|
||||
|
||||
// safety check
|
||||
val fields = calculator.javaClass.getDeclaredFields().filter { it.getModifiers().and(Modifier.STATIC) == 0 }
|
||||
val fields = calculator.javaClass.declaredFields.filter { it.modifiers.and(Modifier.STATIC) == 0 }
|
||||
assert(fields.isEmpty()) {
|
||||
"No fields in the passed lambda expected but ${fields.joinToString()} found"
|
||||
}
|
||||
@@ -95,12 +95,12 @@ public fun <T : Any> constant(calculator: () -> T): T {
|
||||
|
||||
private val constantMap = ConcurrentHashMap<Function0<*>, Any>()
|
||||
|
||||
public fun String.indexOfOrNull(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int? {
|
||||
fun String.indexOfOrNull(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int? {
|
||||
val index = indexOf(char, startIndex, ignoreCase)
|
||||
return if (index >= 0) index else null
|
||||
}
|
||||
|
||||
public fun String.lastIndexOfOrNull(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int? {
|
||||
fun String.lastIndexOfOrNull(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int? {
|
||||
val index = lastIndexOf(char, startIndex, ignoreCase)
|
||||
return if (index >= 0) index else null
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ package org.jetbrains.kotlin.util.capitalizeDecapitalize
|
||||
* "FOOBar" -> "fooBar"
|
||||
* "FOO" -> "foo"
|
||||
*/
|
||||
public fun String.decapitalizeSmart(asciiOnly: Boolean = false): String {
|
||||
fun String.decapitalizeSmart(asciiOnly: Boolean = false): String {
|
||||
fun isUpperCaseCharAt(index: Int): Boolean {
|
||||
val c = this[index]
|
||||
return if (asciiOnly) c in 'A'..'Z' else c.isUpperCase()
|
||||
@@ -45,7 +45,7 @@ public fun String.decapitalizeSmart(asciiOnly: Boolean = false): String {
|
||||
* "FooBar" -> "FOOBar"
|
||||
* "foo" -> "FOO"
|
||||
*/
|
||||
public fun String.capitalizeFirstWord(asciiOnly: Boolean = false): String {
|
||||
fun String.capitalizeFirstWord(asciiOnly: Boolean = false): String {
|
||||
fun toUpperCase(string: String) = if (asciiOnly) string.toUpperCaseAsciiOnly() else string.toUpperCase()
|
||||
|
||||
fun isLowerCaseCharAt(index: Int): Boolean {
|
||||
@@ -58,7 +58,7 @@ public fun String.capitalizeFirstWord(asciiOnly: Boolean = false): String {
|
||||
return toUpperCase(substring(0, secondWordStart)) + substring(secondWordStart)
|
||||
}
|
||||
|
||||
public fun String.capitalizeAsciiOnly(): String {
|
||||
fun String.capitalizeAsciiOnly(): String {
|
||||
if (isEmpty()) return this
|
||||
val c = this[0]
|
||||
return if (c in 'a'..'z')
|
||||
@@ -67,7 +67,7 @@ public fun String.capitalizeAsciiOnly(): String {
|
||||
this
|
||||
}
|
||||
|
||||
public fun String.decapitalizeAsciiOnly(): String {
|
||||
fun String.decapitalizeAsciiOnly(): String {
|
||||
if (isEmpty()) return this
|
||||
val c = this[0]
|
||||
return if (c in 'A'..'Z')
|
||||
@@ -76,7 +76,7 @@ public fun String.decapitalizeAsciiOnly(): String {
|
||||
this
|
||||
}
|
||||
|
||||
public fun String.toLowerCaseAsciiOnly(): String {
|
||||
fun String.toLowerCaseAsciiOnly(): String {
|
||||
val builder = StringBuilder(length)
|
||||
for (c in this) {
|
||||
builder.append(if (c in 'A'..'Z') c.toLowerCase() else c)
|
||||
@@ -84,7 +84,7 @@ public fun String.toLowerCaseAsciiOnly(): String {
|
||||
return builder.toString()
|
||||
}
|
||||
|
||||
public fun String.toUpperCaseAsciiOnly(): String {
|
||||
fun String.toUpperCaseAsciiOnly(): String {
|
||||
val builder = StringBuilder(length)
|
||||
for (c in this) {
|
||||
builder.append(if (c in 'a'..'z') c.toUpperCase() else c)
|
||||
|
||||
@@ -18,11 +18,11 @@ package org.jetbrains.kotlin.utils
|
||||
|
||||
import java.util.*
|
||||
|
||||
public fun <K, V> Iterable<K>.keysToMap(value: (K) -> V): Map<K, V> {
|
||||
fun <K, V> Iterable<K>.keysToMap(value: (K) -> V): Map<K, V> {
|
||||
return toMapBy({ it }, value)
|
||||
}
|
||||
|
||||
public fun <K, V: Any> Iterable<K>.keysToMapExceptNulls(value: (K) -> V?): Map<K, V> {
|
||||
fun <K, V: Any> Iterable<K>.keysToMapExceptNulls(value: (K) -> V?): Map<K, V> {
|
||||
val map = LinkedHashMap<K, V>()
|
||||
for (k in this) {
|
||||
val v = value(k)
|
||||
@@ -33,7 +33,7 @@ public fun <K, V: Any> Iterable<K>.keysToMapExceptNulls(value: (K) -> V?): Map<K
|
||||
return map
|
||||
}
|
||||
|
||||
public fun <K> Iterable<K>.mapToIndex(): Map<K, Int> {
|
||||
fun <K> Iterable<K>.mapToIndex(): Map<K, Int> {
|
||||
val map = LinkedHashMap<K, Int>()
|
||||
for ((index, k) in this.withIndex()) {
|
||||
map[k] = index
|
||||
@@ -41,30 +41,30 @@ public fun <K> Iterable<K>.mapToIndex(): Map<K, Int> {
|
||||
return map
|
||||
}
|
||||
|
||||
public inline fun <T, C: Collection<T>> C.ifEmpty(body: () -> C): C = if (isEmpty()) body() else this
|
||||
inline fun <T, C: Collection<T>> C.ifEmpty(body: () -> C): C = if (isEmpty()) body() else this
|
||||
|
||||
public inline fun <T> Array<out T>.ifEmpty(body: () -> Array<out T>): Array<out T> = if (isEmpty()) body() else this
|
||||
inline fun <T> Array<out T>.ifEmpty(body: () -> Array<out T>): Array<out T> = if (isEmpty()) body() else this
|
||||
|
||||
public fun <T: Any> emptyOrSingletonList(item: T?): List<T> = listOfNotNull(item)
|
||||
fun <T: Any> emptyOrSingletonList(item: T?): List<T> = listOfNotNull(item)
|
||||
|
||||
public fun <T: Any> MutableCollection<T>.addIfNotNull(t: T?) {
|
||||
fun <T: Any> MutableCollection<T>.addIfNotNull(t: T?) {
|
||||
if (t != null) add(t)
|
||||
}
|
||||
|
||||
public fun <K, V> newHashMapWithExpectedSize(expectedSize: Int): HashMap<K, V> {
|
||||
fun <K, V> newHashMapWithExpectedSize(expectedSize: Int): HashMap<K, V> {
|
||||
return HashMap(if (expectedSize < 3) 3 else expectedSize + expectedSize / 3 + 1)
|
||||
}
|
||||
|
||||
public fun <E> newHashSetWithExpectedSize(expectedSize: Int): HashSet<E> {
|
||||
fun <E> newHashSetWithExpectedSize(expectedSize: Int): HashSet<E> {
|
||||
return HashSet(if (expectedSize < 3) 3 else expectedSize + expectedSize / 3 + 1)
|
||||
}
|
||||
|
||||
public fun <T> Collection<T>.toReadOnlyList(): List<T> =
|
||||
fun <T> Collection<T>.toReadOnlyList(): List<T> =
|
||||
when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(first())
|
||||
else -> ArrayList(this)
|
||||
}
|
||||
|
||||
public fun <T: Any> T?.singletonOrEmptyList(): List<T> =
|
||||
fun <T: Any> T?.singletonOrEmptyList(): List<T> =
|
||||
if (this != null) listOf(this) else emptyList()
|
||||
|
||||
@@ -16,4 +16,4 @@
|
||||
|
||||
package org.jetbrains.kotlin.utils
|
||||
|
||||
public inline fun <T : Any> T?.sure(message: () -> String): T = this ?: throw AssertionError(message())
|
||||
inline fun <T : Any> T?.sure(message: () -> String): T = this ?: throw AssertionError(message())
|
||||
|
||||
@@ -25,11 +25,11 @@ import java.io.Closeable
|
||||
* throw ExceptionUtils.rethrow(e);
|
||||
* In this case compiler knows that code after this rethrowing won't be executed.
|
||||
*/
|
||||
public fun rethrow(e: Throwable): RuntimeException {
|
||||
fun rethrow(e: Throwable): RuntimeException {
|
||||
throw e
|
||||
}
|
||||
|
||||
public fun closeQuietly(closeable: Closeable?) {
|
||||
fun closeQuietly(closeable: Closeable?) {
|
||||
if (closeable != null) {
|
||||
try {
|
||||
closeable.close()
|
||||
|
||||
@@ -18,15 +18,14 @@ package org.jetbrains.kotlin.utils
|
||||
|
||||
private val IDENTITY: (Any?) -> Any? = { it }
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public fun <T> identity(): (T) -> T = IDENTITY as (T) -> T
|
||||
@Suppress("UNCHECKED_CAST") fun <T> identity(): (T) -> T = IDENTITY as (T) -> T
|
||||
|
||||
|
||||
private val ALWAYS_TRUE: (Any?) -> Boolean = { true }
|
||||
|
||||
public fun <T> alwaysTrue(): (T) -> Boolean = ALWAYS_TRUE
|
||||
fun <T> alwaysTrue(): (T) -> Boolean = ALWAYS_TRUE
|
||||
|
||||
|
||||
public val DO_NOTHING: (Any?) -> Unit = { }
|
||||
val DO_NOTHING: (Any?) -> Unit = { }
|
||||
|
||||
public fun <T> doNothing(): (T) -> Unit = DO_NOTHING
|
||||
fun <T> doNothing(): (T) -> Unit = DO_NOTHING
|
||||
|
||||
Reference in New Issue
Block a user