Skip to content

Commit

Permalink
Feature: added the experimental not null and null function.
Browse files Browse the repository at this point in the history
  • Loading branch information
pokk committed Nov 13, 2018
1 parent a2f7c7e commit a52599b
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Kits.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

package com.devrapid.kotlinshaver

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

/**
*
* @author jieyi
Expand All @@ -10,7 +13,7 @@ package com.devrapid.kotlinshaver
inline infix fun (() -> Unit).iff(condition: Any?): Any? {
return when (condition) {
is Boolean -> if (condition) this() else null
is Float, Double, Int, Long -> condition.let { this() }
is Float, Double, Int, Long -> this()
is String -> if (condition.isNotBlank()) this() else null
is Collection<*> -> if (condition.isNotEmpty()) this() else null
else -> condition?.let { this() }
Expand All @@ -20,3 +23,19 @@ inline infix fun (() -> Unit).iff(condition: Any?): Any? {
inline fun Any?.isNull() = null == this

inline fun Any?.isNotNull() = null != this

@ExperimentalContracts
inline fun Any?.isNullExp(): Boolean {
contract {
returns(false) implies (this@isNullExp == null)
}
return null == this
}

@ExperimentalContracts
inline fun Any?.isNotNullExp(): Boolean {
contract {
returns(true) implies (this@isNotNullExp != null)
}
return null != this
}

0 comments on commit a52599b

Please sign in to comment.