Skip to content

Instantly share code, notes, and snippets.

@jeffypooo
Created November 20, 2019 19:05
Show Gist options
  • Select an option

  • Save jeffypooo/7eebc98ba6568b374e55c6935ef2dca0 to your computer and use it in GitHub Desktop.

Select an option

Save jeffypooo/7eebc98ba6568b374e55c6935ef2dca0 to your computer and use it in GitHub Desktop.

Revisions

  1. jeffypooo created this gist Nov 20, 2019.
    44 changes: 44 additions & 0 deletions UseCases.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    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>>()

    }