🥄
One Library
  • 🤖onelib
  • 🏡Home
    • Overview
    • Setup Project
    • What's New
  • ☀️Fundamental
    • Basic Android
    • Data Flow
    • Dependency Injection
      • Hilt
      • Koin
  • 📿Layers
    • Data Layer
    • Domain Layer
    • Presentation Layer
  • 🪶Features
    • Views
      • Activity
      • Fragment
      • Bottom Sheet
      • Dialog
    • Adapters
      • Recycler Adapter
      • Filterable Adapter
      • Paging Adapter
      • Selectable Adapter
      • ViewPager Adapter
    • Firebase
      • One Firebase Auth
      • One Firebase Firestore
      • One Firebase Realtime
      • One Firebase Storage
    • Extension Functions
      • Any
      • Common
      • Context
      • Edittext
      • Image View
      • LiveData
      • OneMap
      • State Flow (Jetpack Compose)
      • View
    • Notification
    • One State View
    • Local Database
    • Permission
    • Validation
      • Passive Validator
      • Reactive Validator
      • Extension Validation
Powered by GitBook
On this page
  • Fallback
  • Is Not Null
  • Is Null
  • To Selectable
  1. Features
  2. Extension Functions

Any

Fallback

This function provides a fallback value if the object it's called on is null.

fun <T> T?.fallback(value: T): T =
    this ?: value
  • Parameters: value (the fallback value of type T).

  • Process: Returns this if it is not null; otherwise, returns the provided value.

  • Return: A non-null value of type T.

Example:

val nullableString: String? = null
val result = nullableString.fallback("default value")
println(result)  // Output: "default value"

Is Not Null

This function checks if the object is not null.

fun Any?.isNotNull(): Boolean = !isNull()
  • Return: true if the object is not null, otherwise false.

Example:

val nullableObject: Any? = "Hello"
println(nullableObject.isNotNull())  // Output: true

val nullObject: Any? = null
println(nullObject.isNotNull())  // Output: false

Is Null

This function checks if the object is null.

fun Any?.isNull(): Boolean = this == null
  • Return: true if the object is null, otherwise false.

Example:

val nullableObject: Any? = null
println(nullableObject.isNull())  // Output: true

val nonNullObject: Any? = "Hello"
println(nonNullObject.isNull())  // Output: false

To Selectable

This function converts a list to a list of Selectable objects.

fun <T> List<T>?.toSelectable(): List<Selectable<T>> {
    return this?.map { Selectable(it) } ?: emptyList()
}
  • Return: A list of Selectable objects if the original list is not null; otherwise, an empty list.

Example:

val items: List<String>? = listOf("Item1", "Item2", "Item3")
val selectableItems = items.toSelectable()
selectableItems.forEach { println(it) }

Last updated 11 months ago

🪶