Skip to content

Commit

Permalink
Add loading or error types. (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux committed Sep 7, 2022
1 parent 835c4f1 commit 347f410
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@

# Site
/site

# Jenv
.java-version
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
6 changes: 3 additions & 3 deletions lce/src/main/java/com/laimiux/lce/ContentOrElse.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.laimiux.lce

inline fun <L, C, E> LCE<L, C, E>.contentOrElse(crossinline onOther: () -> C): C {
inline fun <L, C, E> LCE<L, C, E>.contentOrElse(crossinline onOther: (LE<L, E>) -> C): C {
return foldContent(
onContent = { it },
onOther = onOther
)
}

inline fun <C, E> UCE<C, E>.contentOrElse(crossinline onOther: () -> C): C {
inline fun <C, E> UCE<C, E>.contentOrElse(crossinline onOther: (UE<E>) -> C): C {
return foldContent(
onContent = { it },
onOther = onOther
)
}

inline fun <C> UCT<C>.contentOrElse(crossinline onOther: () -> C): C {
inline fun <C> UCT<C>.contentOrElse(crossinline onOther: (UT) -> C): C {
return foldContent(
onContent = { it },
onOther = onOther
Expand Down
50 changes: 50 additions & 0 deletions lce/src/main/java/com/laimiux/lce/Convert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -467,4 +467,54 @@ fun <C> UC<C>.asLC(): LC<Unit, C> {
onLoading = { it },
onContent = { it }
)
}

/**
* Converts `LE<L, E>` to `LCE<L, Nothing, E>`
*/
fun <L, E> LE<L, E>.asLCE(): LCE<L, Nothing, E> {
return foldTypes(
onLoading = { it },
onError = { it },
)
}

/**
* Converts `LT<L>` to `LCE<L, Nothing, Throwable>`
*/
fun <L> LT<L>.asLCE(): LCE<L, Nothing, Throwable> {
return foldTypes(
onLoading = { it },
onError = { it },
)
}

/**
* Converts `UE<E>` to `UCE<L, Nothing, E>`
*/
fun <E> UE<E>.asUCE(): UCE<Nothing, E> {
return foldTypes(
onLoading = { it },
onError = { it },
)
}

/**
* Converts `UT` to `UCT<Nothing>`
*/
fun UT.asUCT(): UCT<Nothing> {
return foldTypes(
onLoading = { it },
onError = { it },
)
}

/**
* Converts `UT` to `UCE<Nothing, Throwable>`
*/
fun UT.asUCE(): UCE<Nothing, Throwable> {
return foldTypes(
onLoading = { it },
onError = { it },
)
}
6 changes: 3 additions & 3 deletions lce/src/main/java/com/laimiux/lce/FoldContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.laimiux.lce

inline fun <L, C, E, T> LCE<L, C, E>.foldContent(
crossinline onContent: (C) -> T,
crossinline onOther: () -> T
crossinline onOther: (LE<L, E>) -> T
): T {
return foldTypes(
onContent = { onContent(it.value) },
Expand All @@ -12,7 +12,7 @@ inline fun <L, C, E, T> LCE<L, C, E>.foldContent(

inline fun <C, E, T> UCE<C, E>.foldContent(
crossinline onContent: (C) -> T,
crossinline onOther: () -> T
crossinline onOther: (UE<E>) -> T
): T {
return foldTypes(
onContent = { onContent(it.value) },
Expand All @@ -22,7 +22,7 @@ inline fun <C, E, T> UCE<C, E>.foldContent(

inline fun <C, T> UCT<C>.foldContent(
crossinline onContent: (C) -> T,
crossinline onOther: () -> T
crossinline onOther: (UT) -> T
): T {
return foldTypes(
onContent = { onContent(it.value) },
Expand Down
62 changes: 53 additions & 9 deletions lce/src/main/java/com/laimiux/lce/FoldTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ inline fun <L, C, E, T> LCE<L, C, E>.foldTypes(
@JvmName("foldContentType")
inline fun <L, C, E, T> LCE<L, C, E>.foldTypes(
crossinline onContent: (Type.Content<C>) -> T,
crossinline onOther: () -> T
crossinline onOther: (LE<L, E>) -> T
): T {
return foldTypes(
onLoading = { onOther() },
onLoading = onOther,
onContent = onContent,
onError = { onOther() }
onError = onOther,
)
}

Expand Down Expand Up @@ -76,12 +76,12 @@ inline fun <C, E, T> UCE<C, E>.foldTypes(
@JvmName("foldContentType")
inline fun <C, E, T> UCE<C, E>.foldTypes(
crossinline onContent: (Type.Content<C>) -> T,
crossinline onOther: () -> T
crossinline onOther: (UE<E>) -> T
): T {
return foldTypes(
onLoading = { onOther() },
onLoading = onOther,
onContent = onContent,
onError = { onOther() }
onError = onOther,
)
}

Expand Down Expand Up @@ -125,12 +125,12 @@ inline fun <C, T> UCT<C>.foldTypes(
@JvmName("foldContentType")
inline fun <C, T> UCT<C>.foldTypes(
crossinline onContent: (Type.Content<C>) -> T,
crossinline onOther: () -> T
crossinline onOther: (UT) -> T
): T {
return foldTypes(
onLoading = { onOther() },
onLoading = onOther,
onContent = onContent,
onError = { onOther() }
onError = onOther,
)
}

Expand Down Expand Up @@ -188,4 +188,48 @@ inline fun <C, T> UC<C>.foldTypes(
is Type.Content -> onContent(type)
else -> throw IllegalStateException("this should not happen: $type")
}
}

inline fun <L, E, T> LE<L, E>.foldTypes(
crossinline onLoading: (Type.Loading<L>) -> T,
crossinline onError: (Type.Error<E>) -> T
): T {
return when (val type = asLceType()) {
is Type.Loading -> onLoading(type)
is Type.Error -> onError(type)
else -> throw IllegalStateException("this should not happen: $type")
}
}

inline fun <L, T> LT<L>.foldTypes(
crossinline onLoading: (Type.Loading<L>) -> T,
crossinline onError: (Type.Error.ThrowableType) -> T
): T {
return when (val type = asLceType()) {
is Type.Loading -> onLoading(type)
is Type.Error.ThrowableType -> onError(type)
else -> throw IllegalStateException("this should not happen: $type")
}
}

inline fun <E, T> UE<E>.foldTypes(
crossinline onLoading: (Type.Loading.UnitType) -> T,
crossinline onError: (Type.Error<E>) -> T
): T {
return when (val type = asLceType()) {
is Type.Loading.UnitType -> onLoading(type)
is Type.Error -> onError(type)
else -> throw IllegalStateException("this should not happen: $type")
}
}

inline fun <T> UT.foldTypes(
crossinline onLoading: (Type.Loading.UnitType) -> T,
crossinline onError: (Type.Error.ThrowableType) -> T
): T {
return when (val type = asLceType()) {
is Type.Loading.UnitType -> onLoading(type)
is Type.Error.ThrowableType -> onError(type)
else -> throw IllegalStateException("this should not happen: $type")
}
}
18 changes: 18 additions & 0 deletions lce/src/main/java/com/laimiux/lce/LE.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.laimiux.lce

/**
* LT stands for Loading / Error. A type that represents either loading of type [L]
* or error of type [E].
*
* Note: It's an intermediary type to enable certain APIs and should rarely be used.
*/
interface LE<out L, out E> {

fun isLoading(): Boolean
fun isError(): Boolean

fun loadingOrNull(): L?
fun errorOrNull(): E?

fun asLceType(): Type<L, Nothing, E>
}
18 changes: 18 additions & 0 deletions lce/src/main/java/com/laimiux/lce/LT.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.laimiux.lce

/**
* LT stands for Loading / Throwable. A type that represents either loading of type [L]
* or error of type [Throwable].
*
* Note: It's an intermediary type to enable certain APIs and should rarely be used.
*/
interface LT<out L> {

fun isLoading(): Boolean
fun isError(): Boolean

fun loadingOrNull(): L?
fun errorOrNull(): Throwable?

fun asLceType(): Type<L, Nothing, Throwable>
}
16 changes: 12 additions & 4 deletions lce/src/main/java/com/laimiux/lce/Type.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ sealed class Type<out L, out C, out E> : LCE<L, C, E> {
sealed class Loading<out L>() :
Type<L, Nothing, Nothing>(),
LCE<L, Nothing, Nothing>,
LC<L, Nothing> {
LC<L, Nothing>,
LE<L, Nothing>,
LT<L> {

companion object {
operator fun invoke() = UnitType
Expand Down Expand Up @@ -44,7 +46,9 @@ sealed class Type<out L, out C, out E> : LCE<L, C, E> {
LCE<Unit, Nothing, Nothing>,
UCE<Nothing, Nothing>,
UCT<Nothing>,
UC<Nothing> {
UC<Nothing>,
UE<Nothing>,
UT {

override val value: Unit = Unit
}
Expand All @@ -61,7 +65,9 @@ sealed class Type<out L, out C, out E> : LCE<L, C, E> {
Type<Nothing, Nothing, E>(),
LCE<Nothing, Nothing, E>,
UCE<Nothing, E>,
CE<Nothing, E> {
CE<Nothing, E>,
LE<Nothing, E>,
UE<E> {

companion object {
operator fun invoke(throwable: Throwable) = ThrowableType(throwable)
Expand Down Expand Up @@ -96,7 +102,9 @@ sealed class Type<out L, out C, out E> : LCE<L, C, E> {
override val value: Throwable
) : Error<Throwable>(),
UCT<Nothing>,
CT<Nothing> {
CT<Nothing>,
LT<Nothing>,
UT {

override fun errorOrNull(): Throwable = value
}
Expand Down
18 changes: 18 additions & 0 deletions lce/src/main/java/com/laimiux/lce/UE.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.laimiux.lce

/**
* UE stands for Unit / Error. A type that represents either loading of type [Unit]
* or error of type [E].
*
* Note: It's an intermediary type to enable certain APIs and should rarely be used.
*/
interface UE<out E> {

fun isLoading(): Boolean
fun isError(): Boolean

fun loadingOrNull(): Any?
fun errorOrNull(): E?

fun asLceType(): Type<Any?, Nothing, E>
}
18 changes: 18 additions & 0 deletions lce/src/main/java/com/laimiux/lce/UT.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.laimiux.lce

/**
* UT stands for Unit / Throwable. A type that represents either loading of type [Unit]
* or error of type [Throwable].
*
* Note: It's an intermediary type to enable certain APIs and should rarely be used.
*/
interface UT {

fun isLoading(): Boolean
fun isError(): Boolean

fun loadingOrNull(): Any?
fun errorOrNull(): Throwable?

fun asLceType(): Type<Any?, Nothing, Throwable>
}
27 changes: 27 additions & 0 deletions lce/src/test/java/com/laimiux/lce/FoldContentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,31 @@ class FoldContentTest {
)
assertThat(result).isEqualTo("success")
}

@Test fun `LCE foldContent provides LE in other to handle remaining cases`() {
val result = LCE.loading().foldContent(
onContent = { LCE.content(1) },
onOther = { it.asLCE() }
)

assertThat(result).isEqualTo(LCE.loading())
}

@Test fun `UCE foldContent provides UE in other to handle remaining cases`() {
val result = UCT.loading().foldContent(
onContent = { UCE.content(1) },
onOther = { it.asUCE() }
)

assertThat(result).isEqualTo(UCE.loading())
}

@Test fun `UCT foldContent provides UT in other to handle remaining cases`() {
val result = UCT.loading().foldContent(
onContent = { UCT.content(1) },
onOther = { it.asUCT() }
)

assertThat(result).isEqualTo(UCT.loading())
}
}

0 comments on commit 347f410

Please sign in to comment.