Skip to content

Commit

Permalink
Add return types to the whatIf expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Nov 8, 2020
1 parent e3bafdc commit 1c89e35
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 51 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package="com.skydoves.whatifdemo">

<application
android:allowBackup="true"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/java/com/skydoves/whatifdemo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import com.skydoves.balloon.showAlignTop
import com.skydoves.whatif.whatIf
import com.skydoves.whatif.whatIfLet
import com.skydoves.whatif.whatIfNotNull
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.button

class MainActivity : AppCompatActivity() {

Expand Down Expand Up @@ -55,20 +55,19 @@ class MainActivity : AppCompatActivity() {
)

// example3 : nullable String true-false check extension with default value.
nullableString = nullableBoolean?.whatIfLet(nullableBoolean, "null") {
nullableString = nullableBoolean.whatIfLet(nullableBoolean, "null") {
"notNull"
}
if (nullableString != null) {
log(nullableString)
}

log(nullableString)

// example4 : nullable any type null check extension.
nullableString.whatIfNotNull {
log("$it is not null.")
}

// example5 : nullable any type null check extension.
val newString = nullableString?.whatIfLet(
val newString = nullableString.whatIfLet(
given = nullableString.length > 3,
whatIf = {
log("$it is long.")
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
android:layout_centerInParent="true"
android:text="button" />
</RelativeLayout>
40 changes: 25 additions & 15 deletions whatif/src/main/java/com/skydoves/whatif/WhatIf.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ package com.skydoves.whatif
public inline fun <T> T.whatIf(
given: (T) -> Boolean?,
whatIf: () -> Unit
) {
): T {

if (given(this) == true) {
whatIf()
}
return this
}

/**
Expand All @@ -47,13 +48,14 @@ public inline fun <T> T.whatIf(
given: (T) -> Boolean?,
whatIf: () -> Unit,
whatIfNot: () -> Unit
) {
): T {

if (given(this) == true) {
whatIf()
} else {
whatIfNot()
}
return this
}

/**
Expand Down Expand Up @@ -177,9 +179,9 @@ public inline fun <T, R> T.whatIfLet(
@WhatIfInlineOnly
public inline fun <T> T?.whatIfNotNull(
whatIf: (T) -> Unit
) {
): T? {

this.whatIfNotNull(
return this.whatIfNotNull(
whatIf = { whatIf(it) },
whatIfNot = { }
)
Expand All @@ -194,13 +196,14 @@ public inline fun <T> T?.whatIfNotNull(
public inline fun <T> T?.whatIfNotNull(
whatIf: (T) -> Unit,
whatIfNot: (T?) -> Unit
) {
): T? {

if (this != null) {
whatIf(this)
} else {
whatIfNot(this)
}
return this
}

/**
Expand All @@ -211,11 +214,13 @@ public inline fun <T> T?.whatIfNotNull(
@WhatIfInlineOnly
public inline fun <reified R> Any?.whatIfNotNullAs(
whatIf: (R) -> Unit
) {
): Any? {

if (this != null) {
whatIf(this as R)
return this
}
return this
}

/**
Expand All @@ -228,13 +233,14 @@ public inline fun <reified R> Any?.whatIfNotNullAs(
public inline fun <reified R> Any?.whatIfNotNullAs(
whatIf: (R) -> Unit,
whatIfNot: () -> Unit
) {
): Any? {

if (this != null) {
whatIf(this as R)
} else {
whatIfNot()
return this
}
whatIfNot()
return this
}

/**
Expand All @@ -260,9 +266,9 @@ public inline fun <T, R> T?.whatIfNotNullWith(
@WhatIfInlineOnly
public inline fun Boolean?.whatIf(
whatIf: () -> Unit
) {
): Boolean? {

this.whatIf(
return this.whatIf(
whatIf = { whatIf() },
whatIfNot = { }
)
Expand All @@ -277,25 +283,27 @@ public inline fun Boolean?.whatIf(
public inline fun Boolean?.whatIf(
whatIf: () -> Unit,
whatIfNot: () -> Unit
) {
): Boolean? {

if (this == true) {
whatIf()
} else {
whatIfNot()
}
return this
}

/** An expression for invoking [whatIf] when the target object is not null and false. */
@JvmSynthetic
@WhatIfInlineOnly
public inline fun Boolean?.whatIfElse(
whatIf: () -> Unit
) {
): Boolean? {

if (this == false) {
whatIf()
}
return this
}

/** An expression for invoking [whatIf] when the target Boolean is true and the [predicate] is also true. */
Expand All @@ -304,11 +312,12 @@ public inline fun Boolean?.whatIfElse(
public inline fun Boolean?.whatIfAnd(
predicate: Boolean?,
whatIf: () -> Unit
) {
): Boolean? {

if (this == true && predicate == true) {
whatIf()
}
return this
}

/** An expression for invoking [whatIf] when the target Boolean is true or the [predicate] is true. */
Expand All @@ -317,9 +326,10 @@ public inline fun Boolean?.whatIfAnd(
public inline fun Boolean?.whatIfOr(
predicate: Boolean?,
whatIf: () -> Unit
) {
): Boolean? {

if (this == true || predicate == true) {
whatIf()
}
return this
}
39 changes: 19 additions & 20 deletions whatif/src/main/java/com/skydoves/whatif/WhatIfArray.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package com.skydoves.whatif
@WhatIfInlineOnly
public inline fun <T> Array<out T>?.whatIfNotNullOrEmpty(
whatIf: (Array<out T>) -> Unit
) {
): Array<out T>? = apply {

this.whatIfNotNullOrEmpty(
whatIf = { whatIf(it) },
Expand All @@ -41,8 +41,7 @@ public inline fun <T> Array<out T>?.whatIfNotNullOrEmpty(
public inline fun <T> Array<out T>?.whatIfNotNullOrEmpty(
whatIf: (Array<out T>) -> Unit,
whatIfNot: () -> Unit
) {

): Array<out T>? = apply {
if (!this.isNullOrEmpty()) {
whatIf(this)
} else {
Expand All @@ -55,9 +54,9 @@ public inline fun <T> Array<out T>?.whatIfNotNullOrEmpty(
@WhatIfInlineOnly
public inline fun ByteArray?.whatIfNotNullOrEmpty(
whatIf: (ByteArray) -> Unit
) {
): ByteArray? = apply {

this.whatIfNotNullOrEmpty(
return this.whatIfNotNullOrEmpty(
whatIf = { whatIf(it) },
whatIfNot = { }
)
Expand All @@ -72,7 +71,7 @@ public inline fun ByteArray?.whatIfNotNullOrEmpty(
public inline fun ByteArray?.whatIfNotNullOrEmpty(
whatIf: (ByteArray) -> Unit,
whatIfNot: () -> Unit
) {
): ByteArray? = apply {

if (this != null && this.isNotEmpty()) {
whatIf(this)
Expand All @@ -86,7 +85,7 @@ public inline fun ByteArray?.whatIfNotNullOrEmpty(
@WhatIfInlineOnly
public inline fun ShortArray?.whatIfNotNullOrEmpty(
whatIf: (ShortArray) -> Unit
) {
): ShortArray? = apply {

this.whatIfNotNullOrEmpty(
whatIf = { whatIf(it) },
Expand All @@ -103,7 +102,7 @@ public inline fun ShortArray?.whatIfNotNullOrEmpty(
public inline fun ShortArray?.whatIfNotNullOrEmpty(
whatIf: (ShortArray) -> Unit,
whatIfNot: () -> Unit
) {
): ShortArray? = apply {

if (this != null && this.isNotEmpty()) {
whatIf(this)
Expand All @@ -117,7 +116,7 @@ public inline fun ShortArray?.whatIfNotNullOrEmpty(
@WhatIfInlineOnly
public inline fun IntArray?.whatIfNotNullOrEmpty(
whatIf: (IntArray) -> Unit
) {
): IntArray? = apply {

this.whatIfNotNullOrEmpty(
whatIf = { whatIf(it) },
Expand All @@ -134,7 +133,7 @@ public inline fun IntArray?.whatIfNotNullOrEmpty(
public inline fun IntArray?.whatIfNotNullOrEmpty(
whatIf: (IntArray) -> Unit,
whatIfNot: () -> Unit
) {
): IntArray? = apply {

if (this != null && this.isNotEmpty()) {
whatIf(this)
Expand All @@ -148,7 +147,7 @@ public inline fun IntArray?.whatIfNotNullOrEmpty(
@WhatIfInlineOnly
public inline fun LongArray?.whatIfNotNullOrEmpty(
whatIf: (LongArray) -> Unit
) {
): LongArray? = apply {

this.whatIfNotNullOrEmpty(
whatIf = { whatIf(it) },
Expand All @@ -165,7 +164,7 @@ public inline fun LongArray?.whatIfNotNullOrEmpty(
public inline fun LongArray?.whatIfNotNullOrEmpty(
whatIf: (LongArray) -> Unit,
whatIfNot: () -> Unit
) {
): LongArray? = apply {

if (this != null && this.isNotEmpty()) {
whatIf(this)
Expand All @@ -179,7 +178,7 @@ public inline fun LongArray?.whatIfNotNullOrEmpty(
@WhatIfInlineOnly
public inline fun FloatArray?.whatIfNotNullOrEmpty(
whatIf: (FloatArray) -> Unit
) {
): FloatArray? = apply {

this.whatIfNotNullOrEmpty(
whatIf = { whatIf(it) },
Expand All @@ -196,7 +195,7 @@ public inline fun FloatArray?.whatIfNotNullOrEmpty(
public inline fun FloatArray?.whatIfNotNullOrEmpty(
whatIf: (FloatArray) -> Unit,
whatIfNot: () -> Unit
) {
): FloatArray? = apply {

if (this != null && this.isNotEmpty()) {
whatIf(this)
Expand All @@ -210,7 +209,7 @@ public inline fun FloatArray?.whatIfNotNullOrEmpty(
@WhatIfInlineOnly
public inline fun DoubleArray?.whatIfNotNullOrEmpty(
whatIf: (DoubleArray) -> Unit
) {
): DoubleArray? = apply {

this.whatIfNotNullOrEmpty(
whatIf = { whatIf(it) },
Expand All @@ -227,7 +226,7 @@ public inline fun DoubleArray?.whatIfNotNullOrEmpty(
public inline fun DoubleArray?.whatIfNotNullOrEmpty(
whatIf: (DoubleArray) -> Unit,
whatIfNot: () -> Unit
) {
): DoubleArray? = apply {

if (this != null && this.isNotEmpty()) {
whatIf(this)
Expand All @@ -241,7 +240,7 @@ public inline fun DoubleArray?.whatIfNotNullOrEmpty(
@WhatIfInlineOnly
public inline fun BooleanArray?.whatIfNotNullOrEmpty(
whatIf: (BooleanArray) -> Unit
) {
): BooleanArray? = apply {

this.whatIfNotNullOrEmpty(
whatIf = { whatIf(it) },
Expand All @@ -258,7 +257,7 @@ public inline fun BooleanArray?.whatIfNotNullOrEmpty(
public inline fun BooleanArray?.whatIfNotNullOrEmpty(
whatIf: (BooleanArray) -> Unit,
whatIfNot: () -> Unit
) {
): BooleanArray? = apply {

if (this != null && this.isNotEmpty()) {
whatIf(this)
Expand All @@ -272,7 +271,7 @@ public inline fun BooleanArray?.whatIfNotNullOrEmpty(
@WhatIfInlineOnly
public inline fun CharArray?.whatIfNotNullOrEmpty(
whatIf: (CharArray) -> Unit
) {
): CharArray? = apply {

this.whatIfNotNullOrEmpty(
whatIf = { whatIf(it) },
Expand All @@ -289,7 +288,7 @@ public inline fun CharArray?.whatIfNotNullOrEmpty(
public inline fun CharArray?.whatIfNotNullOrEmpty(
whatIf: (CharArray) -> Unit,
whatIfNot: () -> Unit
) {
): CharArray? = apply {

if (this != null && this.isNotEmpty()) {
whatIf(this)
Expand Down
Loading

0 comments on commit 1c89e35

Please sign in to comment.