Created
November 20, 2019 19:05
-
-
Save jeffypooo/7eebc98ba6568b374e55c6935ef2dca0 to your computer and use it in GitHub Desktop.
Usecase base classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.reactivex.Observable | |
import io.reactivex.Single | |
/** | |
* Something that can be executed. | |
* @param ArgsType The argument data type. | |
* @param ReturnType The return data type for the execution | |
*/ | |
interface AbstractExecutable<ArgsType : Any, ReturnType : Any> { | |
fun execute(data: ArgsType): ReturnType | |
} | |
/** | |
* Convenience extension for [AbstractExecutable]s with [Unit] argument types. | |
*/ | |
fun AbstractExecutable<Unit, *>.execute() = execute(data = Unit) | |
/** | |
* Base use-case types for the application. | |
*/ | |
sealed class UseCase<A : Any, R : Any> : AbstractExecutable<A, R> { | |
/** | |
* Synchronous use-case. Executes and returns the result immediately. | |
*/ | |
abstract class Synchronous<A : Any, R : Any> : UseCase<A, R>() | |
/** | |
* Reactive use-case. Returns an Observable stream of results. | |
* @param R the element type for the [Observable]. | |
*/ | |
abstract class RxObservable<A : Any, R : Any> : UseCase<A, Observable<R>>() | |
/** | |
* Reactive use-case. Returns a Single that emits the result type. | |
* @param R the element type for the [Single]. | |
*/ | |
abstract class RxSingle<A: Any, R: Any>: UseCase<A, Single<R>>() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment