generated from JavierSegoviaCordoba/kotlin-template-javiersc
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b6d152
commit e0a27c2
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
kotlin-stdlib/common/main/kotlin/com/javiersc/kotlin/stdlib/T.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.javiersc.kotlin.stdlib | ||
|
||
public inline fun <T> T?.ifNotNull(block: () -> Unit): T? { | ||
if (this != null) block() | ||
return this | ||
} | ||
|
||
public inline fun <T> T?.ifNull(block: () -> Unit): T? { | ||
if (this == null) block() | ||
return this | ||
} |
50 changes: 50 additions & 0 deletions
50
kotlin-stdlib/common/test/kotlin/com/javiersc/kotlin/stdlib/TTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.javiersc.kotlin.stdlib | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
@Suppress("RedundantNullableReturnType") | ||
class TTest { | ||
|
||
private val a: String? = "a" | ||
private val b: String? = null | ||
private val one: Int? = 1 | ||
private val two: Int? = null | ||
|
||
@Test | ||
fun ifNotNull() { | ||
var isA = false | ||
var isB = false | ||
var isOne = false | ||
var isTwo = false | ||
|
||
a.ifNotNull { isA = true } | ||
b.ifNotNull { isB = true } | ||
one.ifNotNull { isOne = true } | ||
two.ifNotNull { isTwo = true } | ||
|
||
assertTrue { isA } | ||
assertFalse { isB } | ||
assertTrue { isOne } | ||
assertFalse { isTwo } | ||
} | ||
|
||
@Test | ||
fun ifNull() { | ||
var isA = false | ||
var isB = false | ||
var isOne = false | ||
var isTwo = false | ||
|
||
a.ifNull { isA = true } | ||
b.ifNull { isB = true } | ||
one.ifNull { isOne = true } | ||
two.ifNull { isTwo = true } | ||
|
||
assertFalse { isA } | ||
assertTrue { isB } | ||
assertFalse { isOne } | ||
assertTrue { isTwo } | ||
} | ||
} |