All Downloads are FREE. Search and download functionalities are using the official Maven repository.

alakazam.kotlin.core.BuilderExtensions.kt Maven / Gradle / Ivy

There is a newer version: 4.7.0
Show newest version
package alakazam.kotlin.core

/**
 * If [condition] is true, the subject [B] will have the [action] lambda called on it, then the subject is returned.
 * If [condition] is false, the subject is returned as-is.
 */
public inline fun  B.ifTrue(condition: Boolean, action: B.() -> B): B = if (condition) this.action() else this

/**
 * If [condition] is false, the subject [B] will have the [action] lambda called on it, then the subject is
 * returned. If [condition] is true, the subject is returned as-is.
 */
public inline fun  B.ifFalse(condition: Boolean, action: B.() -> B): B = if (!condition) this.action() else this

/**
 * If [value] is not null, the subject [B] will have the [action] lambda called on it, then the subject is returned.
 * If [value] is null, the subject is returned as-is.
 */
public inline fun  B.ifNotNull(value: Value?, action: B.(Value) -> B): B =
  if (value != null) this.action(value) else this

/**
 * If [value] is null, the subject [B] will have the [action] lambda called on it, then the subject is returned.
 * If [value] is not null, the subject is returned as-is.
 */
public inline fun  B.ifNull(value: Value?, action: B.() -> B): B = if (value == null) this.action() else this

public inline fun  B.ifEquals(value1: T?, value2: T?, action: B.() -> B): B =
  ifTrue(condition = value1 == value2, action)

public inline fun  B.ifNotEquals(value1: T?, value2: T?, action: B.() -> B): B =
  ifTrue(condition = value1 != value2, action)