WhatIf

Additional

Language
Kotlin
Version
1.1.4 (Oct 8, 2023)
Created
Sep 8, 2019
Updated
Oct 8, 2023
Owner
Jaewoong Eum (skydoves)
Contributor
Jaewoong Eum (skydoves)
1
Activity
Badge
Generate
Download
Source code


☔ Fluent Kotlin expressions for handling single if-else statements, nullable, collections, and boolean.



Download

☔ WhatIf has been downloaded in more than 110k Kotlin and Android projects all over the world!

Gradle

Add the dependency below to your module's build.gradle file:

dependencies {
    implementation "com.github.skydoves:whatif:1.1.4"
}

Usage

WhatIf

WhatIf is an expression for invoking whatIf lambda when the given boolean is true and not-null.

val nullableBoolean: Boolean? = true
whatIf(nullableBoolean) {
  log("not-null and true : $nullableBoolean")
}

Here is an extension for nullable booleans.

nullableBoolean.whatIf {
  log("not-null and true : $nullableBoolean")
}

WhatIfNot

The whatIf expression is basically composed with given, whatIf, whatIfNot.
If the target is null or false, whatIfNot will be invoked instead of the whatIf.
If we do not need to handle whatIfNot case, it can be omitted.

whatIf(
  given = nullableBoolean,
  whatIf = { log("not-null and true : $nullableBoolean") },
  whatIfNot = { log("null or false : $nullableBoolean") }
)

Or here is an extension for nullable boolean.

nullableBoolean.whatIf(
  whatIf = { log("not-null and true : $nullableBoolean") },
  whatIfNot = { log("not-null or false : $nullableBoolean") }
)

WhatIf in the builder pattern

Sometimes we should set builder differently to depend on options.
WhatIf is useful when using a chaining function like a builder pattern.
It can be applied to any builder patterns like AlertDialog.Builder or anything.

val balloon = Balloon.Builder(baseContext)
  .setArrowSize(10)
  .setArrowVisible(true)
  .whatIf(nullableBoolean) { setTextColor(Color.YELLOW) }
  .whatIf(nullableBoolean, { setText("Hello, whatIf") }, { setText("Good-Bye whatIf") })
  .setWidthRatio(1.0f)
  .build()

WhatIfNotNull

WhatIfNotNull is an expression for invoking whatIf lambda when the target object is not null.

val nullableObject: Person? = Person()
nullableObject.whatIfNotNull {
  log("$it is not null")
}

And we can handle the null case.
If the target is null, whatIfNot will be invoked instead of the whatIf.

nullableObject.whatIfNotNull(
  whatIf = { log("$it is not null.") },
  whatIfNot = { log("$it is null.") }
)

WhatIfNotNullAs

WhatIfNotNullAs is an expression for invoking whatIf lambda when the target object is not null and the target can be cast by the desired type, the receiver will get a casted type.

- (serializable as? Poster?)?.let { poster ->
- 
- }

+ parcelable.whatIfNotNullAs<Poster> { poster ->
+  log(poster.name)
+ }

And we can also handle the exception case (target is null or can't cast by the desired type) using whatIfNot.

- (serializable as? Poster?)?.let { poster ->
-  log(poster.name)
- } ?: let {
-  // do something
- }

+ serializable.whatIfNotNullAs<Poster>(
+  whatIf = { poster -> log(poster.name) },
+  whatIfNot = { 
+    // do something
+  })

WhatIfNotNullOrEmpty

An expression for invoking whatIf lambda when the string, collections and array type is not null and not empty.
If the collections or array type target is null or empty, whatIfNot will be invoked instead of the whatIf.

val nullableString: String? = "NotNullOrEmpty"
nullableString.whatIfNotNullOrEmpty { 
  log("$it is not null and not empty")
}

Here is an example for collections.

nullableList.whatIfNotNullOrEmpty {
  log("list $it is not null and not empty")
}

And we can handle the null or empty case.

nullableList.whatIfNotNullOrEmpty(
  whatIf = { log("list $it is not null and not empty") },
  whatIfNot = { log("list is null or empty") }
)

Here is the same example for the array.

nullableArray.whatIfNotNullOrEmpty {
  log("$it is not null and not empty")
}

Array

Array, ByteArray, ShortArray, IntArray, LongArray, FloatArray, DoubleArray, BooleanArray, CharArray

Collections

We can use some expressions for List, Map, and Set.

  • whatIfNotNullOrEmpty: An expression for invoking whatIf when the List is not null and not empty.
  • addWhatIfNotNull: An expression for adding an element and invoking whatIf when the element is not null.
  • addAllWhatIfNotNull: An expression for adding an element and invoking whatIf when the element is not null.
  • removeWhatIfNotNull: An expression for removing an element and invoking whatIf when the element is not null.
  • removeAllWhatIfNotNull: An expression for removing a collection of element and invoking whatIf when the element is not null.

WhatIfMap

The basic concept is the same as whatIf. An expression for invoking whatIf when the target object is not null. It is useful when the type of the receiver and the result should be different.

val length: Int = nullableString.whatIfMap(
  whatIf = { it.length },
  whatIfNot = {
    log("$it, nullableString is null.")
    -1
  }
)

We can use a default value instead of the whatIfNot and it can be omitted.

val length = nullableString.whatIfMap(
    default = -1
  ) { 
  log("$it, length can not over than 5.")
  5
}

whatIfElse

An expression for invoking whatIf lambda when the target boolean is not null and false.

nullableBoolean.whatIfElse {
  log("nullableBoolean is not null and false")
}

WhatIfAnd, WhatIfOr

An expression for invoking whatIf lambda when the target boolean is true and/or a predicate receiver is true.

nullableBoolean.whatIfAnd(predicate) {
  log("nullableBoolean is true and the predicate is also true")
}

nullableBoolean.whatIfOr(predicate) {
  log("nullableBoolean is true or the predicate is true")
}

WhatIf for Android Extension

We can use the whatIf extensions related to the Android project.

Gradle

Add a dependency code to your module's build.gradle file.

dependencies {
    implementation "com.github.skydoves:whatif-android:version"
}

WhatIf for Android Activity

WhatIfHasExtras

An expression for invoking whatIf lambda when the Activity's intent extras is not null and not empty.

var foo: String? = null
this@MainActivity.whatIfHasExtras {
  foo = "${it.getString("foo")}"
}

And we can handle the null case.

this@MainActivity.whatIfHasExtras(
   whatIf = { foo = "${it.getString("foo")}" },
   whatIfNot = { log("intent extras is null or empty.") }
)

We can null-check and typecast simultaneously when we getting intent extra data.

whatIfHasSerializableExtra<Poster>("poster") { poster ->
 ...
}
whatIfHasParcelableExtra<Poster>("poster") { poster ->
 ...
}

WhatIf for Android fragment

whatIfNotNullContext

An expression for invoking whatIf lambda when the Context is not null.

class WhatIfFragment : Fragment() {

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    whatIfNotNullContext {
      Toast.makeText(it, "context is not null!", Toast.LENGTH_SHORT).show()
    }
  }
}

whatIfNotNullArguments

An expression for invoking whatIf when the Fragment.getArguments is not null.

whatIfNotNullArguments {
  it.getString("name").whatIfNotNull {
    log("my name is $it")
  }
}

whatIfNotNullActivity

An expression for invoking whatIf lambda when the Activity is not null.

whatIfNotNullActivity { activity ->
  activity.supportFragmentManager.addOnBackStackChangedListener {
    // .. //
  }
}

whatIfFindParentInterface

An expression for invoking whatIf lambe when the Fragment has an T interface as a parent. Let's assume we have a MainActivity that implements OnClickCallback interface,

class MainActivity : AppCompatActivity(), OnClickCallback {
 ...
}

We can get the parent Activity's OnClickCallback interface on Fragment as following with the whatIfFindParentInterface:

class MainFragment: Fragment() {

 override fun onCreateView(
  inflater: LayoutInflater,
  container: ViewGroup?,
  savedInstanceState: Bundle?
 ): View? {
  return super.onCreateView(inflater, container, savedInstanceState)
  
  whatIfFindParentInterface<OnClickCallback> { 
   it.onClickedButtonFromFragment()
  }
 }
}

Find this library useful? ❤️

Support it by joining stargazers for this repository. ⭐
Also, follow me for my next creations! ????

License

Copyright 2019 skydoves (Jaewoong Eum)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.