main.kotlin.ch.tutteli.atrium.logic.utils.nullable.kt Maven / Gradle / Ivy
@file:Suppress("NOTHING_TO_INLINE")
package ch.tutteli.atrium.logic.utils
import kotlin.jvm.JvmName
import kotlin.reflect.*
/**
* Turns the given type into a nullable type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `String!` or in other words, when you deal with Java and you want to turn a platform type into a nullable type.
*
* Basically it is a replacement for cast but without introducing one explicitly (we only give an additional hint to the
* compiler that we expect a nullable type and not a non-nullable one). For instance, instead of writing
* `getPersons() as List?` you can write `nullable(getPersons())`
*/
inline fun nullable(t: T): T? = t
/**
* Turns an [Iterable] into an iterable with a nullable element type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `Iterable` or in other words, when you deal with Java and you want to turn a platform type into a nullable type.
*
* Basically it is a replacement for cast but without introducing one explicitly (we only give an additional hint to the
* compiler that we expect a nullable type and not a non-nullable one). For instance, instead of writing
* `getPersons() as Iterable` you can write `nullableContainer(getPersons())`
*/
inline fun nullableContainer(iterable: Iterable): Iterable = iterable
/**
* Turns an [Array] into an array with a nullable element type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `Array` or in other words, when you deal with Java and you want to turn a platform type into a nullable type.
*
* Basically it is a replacement for cast but without introducing one explicitly (we only give an additional hint to the
* compiler that we expect a nullable type and not a non-nullable one). For instance, instead of writing
* `getPersons() as Array` you can write `nullableContainer(getPersons())`
*/
inline fun nullableContainer(arr: Array): Array = arr
/**
* Turns a [Map] into a map with a nullable key type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `Map! or in other words, when you deal with Java and you want to turn a platform type into a
* nullable type.
*
* Basically it is a replacement for cast but without introducing one explicitly (we only give an additional hint to the
* compiler that we expect a nullable type and not a non-nullable one). For instance, instead of writing
* `getPersons() as Map` you can write `nullableKeyMap(getPersons())`
*/
inline fun nullableKeyMap(map: Map): Map = map
/**
* Turns a [Map] into a map with a nullable value type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `Map! or in other words, when you deal with Java and you want to turn a platform type into a
* nullable type.
*
* Basically it is a replacement for cast but without introducing one explicitly (we only give an additional hint to the
* compiler that we expect a nullable type and not a non-nullable one). For instance, instead of writing
* `getPersons() as Map` you can write `nullableValueMap(getPersons())`
*/
inline fun nullableValueMap(map: Map): Map = map
/**
* Turns a [Map] into a map with a nullable key and a nullable value type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `Map! or in other words, when you deal with Java and you want to turn a platform type into a
* nullable type.
*
* Basically it is a replacement for cast but without introducing one explicitly (we only give an additional hint to the
* compiler that we expect a nullable type and not a non-nullable one). For instance, instead of writing
* `getPersons() as Map` you can write `nullableKeyValueMap(getPersons())`
*/
inline fun nullableKeyValueMap(map: Map): Map = map
/**
* Turns the given function reference into a function reference with a nullable return type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `String!` or in other words, when you deal with Java and you want to turn the return type (which is a platform
* type) of your function into a nullable type.
*/
inline fun nullable(t: KFunction0): KFunction0 = t
/**
* Turns the given function reference into a function reference with a nullable return type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `String!` or in other words, when you deal with Java and you want to turn the return type (which is a platform
* type) of your function into a nullable type.
*/
@JvmName("nullable1")
inline fun nullable(t: KFunction1): KFunction1 = t
/**
* Turns the given function reference into a function reference with a nullable return type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `String!` or in other words, when you deal with Java and you want to turn the return type (which is a platform
* type) of your function into a nullable type.
*/
@JvmName("nullable2")
inline fun nullable(t: KFunction2): KFunction2 = t
/**
* Turns the given function reference into a function reference with a nullable return type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `String!` or in other words, when you deal with Java and you want to turn the return type (which is a platform
* type) of your function into a nullable type.
*/
@JvmName("nullable3")
inline fun nullable(t: KFunction3): KFunction3 = t
/**
* Turns the given function reference into a function reference with a nullable return type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `String!` or in other words, when you deal with Java and you want to turn the return type (which is a platform
* type) of your function into a nullable type.
*/
@JvmName("nullable4")
inline fun nullable(t: KFunction4): KFunction4 = t
/**
* Turns the given function reference into a function reference with a nullable return type.
*
* Intended to be used in conjunction with [platform types](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types)
* such as `String!` or in other words, when you deal with Java and you want to turn the return type (which is a platform
* type) of your function into a nullable type.
*/
@JvmName("nullable5")
inline fun nullable(t: KFunction5): KFunction5 =
t