Convert some sealed classes to interfaces (#4347)
There is no non-abstract field in them, we can just fall back to interfaces.
This commit is contained in:
parent
e865ffafde
commit
1acae50845
8 changed files with 43 additions and 40 deletions
|
|
@ -21,27 +21,29 @@ package com.keylesspalace.tusky.util
|
|||
* Class to represent sum type/tagged union/variant/ADT e.t.c.
|
||||
* It is either Left or Right.
|
||||
*/
|
||||
sealed class Either<out L, out R> {
|
||||
data class Left<out L, out R>(val value: L) : Either<L, R>()
|
||||
data class Right<out L, out R>(val value: R) : Either<L, R>()
|
||||
sealed interface Either<out L, out R> {
|
||||
data class Left<out L, out R>(val value: L) : Either<L, R>
|
||||
data class Right<out L, out R>(val value: R) : Either<L, R>
|
||||
|
||||
fun isRight() = this is Right
|
||||
fun isRight(): Boolean = this is Right
|
||||
|
||||
fun isLeft() = this is Left
|
||||
fun isLeft(): Boolean = this is Left
|
||||
|
||||
fun asLeftOrNull() = (this as? Left<L, R>)?.value
|
||||
fun asLeftOrNull(): L? = (this as? Left<L, R>)?.value
|
||||
|
||||
fun asRightOrNull() = (this as? Right<L, R>)?.value
|
||||
fun asRightOrNull(): R? = (this as? Right<L, R>)?.value
|
||||
|
||||
fun asLeft(): L = (this as Left<L, R>).value
|
||||
|
||||
fun asRight(): R = (this as Right<L, R>).value
|
||||
|
||||
inline fun <N> map(crossinline mapper: (R) -> N): Either<L, N> {
|
||||
return if (this.isLeft()) {
|
||||
Left(this.asLeft())
|
||||
} else {
|
||||
Right(mapper(this.asRight()))
|
||||
companion object {
|
||||
inline fun <L, R, N> Either<L, R>.map(crossinline mapper: (R) -> N): Either<L, N> {
|
||||
return if (this.isLeft()) {
|
||||
Left(this.asLeft())
|
||||
} else {
|
||||
Right(mapper(this.asRight()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,16 @@
|
|||
package com.keylesspalace.tusky.util
|
||||
|
||||
sealed class Resource<T>(open val data: T?)
|
||||
sealed interface Resource<T> {
|
||||
val data: T?
|
||||
}
|
||||
|
||||
class Loading<T>(override val data: T? = null) : Resource<T>(data)
|
||||
class Loading<T>(override val data: T? = null) : Resource<T>
|
||||
|
||||
class Success<T>(override val data: T? = null) : Resource<T>(data)
|
||||
class Success<T>(override val data: T? = null) : Resource<T>
|
||||
|
||||
class Error<T>(
|
||||
override val data: T? = null,
|
||||
val errorMessage: String? = null,
|
||||
var consumed: Boolean = false,
|
||||
val cause: Throwable? = null
|
||||
) : Resource<T>(data)
|
||||
) : Resource<T>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue