Skip to content

Commit

Permalink
moved TwoWay from BoundProperty to utils; deduplicated implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
Miha-x64 committed May 19, 2020
1 parent 5afe7c0 commit aade0a4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
Expand Up @@ -81,10 +81,7 @@ inline fun <T, R> MutableProperty<T>.bind(
crossinline forward: (T) -> R,
crossinline backwards: (R) -> T
): MutableProperty<R> =
`Bound-`<Nothing, T, R>(this, object : `Bound-`.TwoWay<T, R> {
override fun invoke(p1: T): R = forward.invoke(p1)
override fun backwards(arg: R): T = backwards.invoke(arg)
})
`Bound-`<Nothing, T, R>(this, TwoWay(forward, backwards))

/**
* Returns a new [TransactionalProperty] with value equal to `forward(original.value)`.
Expand All @@ -94,10 +91,7 @@ inline fun <TRANSACTION, T, R> TransactionalProperty<TRANSACTION, T>.bind(
crossinline forward: (T) -> R,
crossinline backwards: (R) -> T
): TransactionalProperty<TRANSACTION, R> =
`Bound-`(this, object : `Bound-`.TwoWay<T, R> {
override fun invoke(p1: T): R = forward.invoke(p1)
override fun backwards(arg: R): T = backwards.invoke(arg)
})
`Bound-`(this, TwoWay(forward, backwards))

/**
* Returns a property which notifies its subscribers only when old and new values are not equal.
Expand Down
Expand Up @@ -18,9 +18,10 @@ internal class `Bound-`<TRANSACTION, T, R>(
mOrig.value = mapping.backwards(value)
}

override fun setValue(transaction: TRANSACTION, value: R) {
tOrig.setValue(transaction, mapping.backwards(value))
}
@Suppress("UNCHECKED_CAST")
override fun setValue(transaction: TRANSACTION, value: R) =
(original as TransactionalProperty<TRANSACTION, T>)
.setValue(transaction, mapping.backwards(value))

override fun bindTo(sample: Property<R>) {
mOrig.bindTo(sample.map(Backwards(mapping)))
Expand All @@ -36,21 +37,8 @@ internal class `Bound-`<TRANSACTION, T, R>(
}

private inline val mOrig get() = original as MutableProperty<T>
private inline val tOrig get() = original as TransactionalProperty<TRANSACTION, T>
private inline val mapping get() = map as TwoWay<T, R>

/**
* Represents a function which can be un-applied.
* For example, when `invoke(arg) = 10 * arg`, `backwards(arg) = arg / 10`.
*/
interface TwoWay<T, R> : (T) -> R {

/**
* Represents an action opposite to invoking this function.
*/
fun backwards(arg: R): T
}

private class Backwards<T, R>(
private val twoWay: TwoWay<T, R>
) : (R) -> T {
Expand Down
20 changes: 20 additions & 0 deletions properties/src/main/kotlin/net/aquadc/properties/internal/utils.kt
Expand Up @@ -90,3 +90,23 @@ inline fun <T> emptyArrayOf(): Array<T> =
@[JvmField JvmSynthetic PublishedApi] internal val TRUE = `Immutable-`(true)
@[JvmField JvmSynthetic PublishedApi] internal val FALSE = `Immutable-`(false)
@[JvmField JvmSynthetic PublishedApi] internal val UNIT = `Immutable-`(Unit)

/**
* Represents a function which can be un-applied.
* For example, when `invoke(arg) = 10 * arg`, `backwards(arg) = arg / 10`.
*/
internal interface TwoWay<T, R> : (T) -> R {

/**
* Represents an action opposite to invoking this function.
*/
fun backwards(arg: R): T
}

@PublishedApi internal inline fun <R, T> TwoWay(
crossinline forward: (T) -> R, crossinline backwards: (R) -> T
): TwoWay<T, R> =
object : TwoWay<T, R> {
override fun invoke(p1: T): R = forward.invoke(p1)
override fun backwards(arg: R): T = backwards.invoke(arg)
}

0 comments on commit aade0a4

Please sign in to comment.