项目作者: just-4-fun

项目描述 :
Kotlin utilites: Result wrapper, AsyncTask, SuspendTask, etc
高级语言: Kotlin
项目地址: git://github.com/just-4-fun/kotlinkit.git
创建时间: 2017-10-01T08:58:38Z
项目社区:https://github.com/just-4-fun/kotlinkit

开源协议:Apache License 2.0

下载


kotlinkit

Kotlin utilities: Result wrapper, AsyncResult, AsyncTask, SuspendTask, etc

Result

The Result<T> class represents a result of some method call and provides the caller with a requested value of type T or an exception, in case if something’s gone awry.
Returning a Result from a method that can fail, replaces the usual approach to throw an exception or return the null in case of an execution failure.

  • Construction:

    1. // from execution of a code block
    2. val result1 = Result { 10 / 10 } // Result<Int>(1)
    3. val result2 = Result { 10 / 0 } // Result<Int>(ArithmeticException)
    4. // from constructor
    5. fun devide(a: Int, b: Int): Result<Int> = if (b != 0) Result(a / b) else Result(ArithmeticException())
    6. val result31: Result<Int> = devide(10, 0) // Result<Int>(ArithmeticException)
    7. val result32: Result<Int> = devide(10, 10) // Result<Int>(1)
    8. // from companion's functions
    9. val result4: Result<String> = Result.Success("ok") // Result<String>("ok")
    10. val result7: Result<Int> = Result.Failure(ArithmeticException()) // Result<Int>(ArithmeticException)
    11. val result5: Result<Exception> = Result.Success(Exception()) // successful Result<Exception>(Exception)
  • Destructuring:

    1. val (value, exception) = Result("ok") // value = "ok"; exception = null
    2. Result { 10 / 0 }.let { (value, exception) -> println("v=$value; e=$exception") } // v=null; e=ArithmeticException
    • Simple usage

      1. val okay = Result { 10 / 10 } //Result<Int>(1)
      2. val oops = Result { 10 / 0 } // Result<Int>(ArithmeticException)
      3. val s0 = okay.value ?: 0 + 1 // = 2
      4. val f0 = oops.value ?: 0 + 1 // = 1
      5. val s1 = okay.valueOrThrow + 1 // = 2
      6. // val f1 = oops.valueOrThrow + 1 // throws ArithmeticException
      7. val s2 = okay.value!! + 1 // = 2
      8. // val f2 = oops.value!! + 1 // throws NullPointerException
      9. val s3 = okay.valueOr(0) + 1 // = 2
      10. val f3 = oops.valueOr(0) + 1 // = 1
      11. val s4 = okay.valueOr { if (it is ArithmeticException) 0 else 1 } + 1 // = 2
      12. val f4 = oops.valueOr { if (it is ArithmeticException) 0 else 1 } + 1 // = 1
      13. val e0 = okay.exception // is null
      14. val e1 = oops.exception // is ArithmeticException
      15. val b0 = if (okay.isSuccess) 1 else 0 // = 1
      16. val b1 = if (oops.isFailure) 1 else 0 // = 1
    • Hooks

      1. okay.onSuccess { println("success") }.onFailure { println("failure 1") } // prints "success"
      2. oops.onSuccess { println("success") }.onFailure { println("failure 2") } // prints "failure 2"
      3. oops.onFailureOf<ArithmeticException> { println("failure 3") } // prints "failure 3"
      4. oops.onFailureOfNot<ArithmeticException> { println("failure 4") } // prints nothing
      5. oops.onFailureOf<Exception> { println("failure 5") } // prints "failure 5"
      6. oops.onFailureOfNot<NullPointerException> { println("failure 6") } // prints "failure 6"
    • Transformations

      1. val w0 = oops.wrapFailure { IllegalStateException() } // exception is IllegalStateException with cause: ArithmeticException
      2. /* Value transformation */
      3. val t0 = okay.mapFailure { 0 } // Result(1)
      4. val t1 = oops.mapFailure { 0 } // Result(0)
      5. val t11 = oops.mapFailureOf<ArithmeticException> { 0 } // Result(0)
      6. val t12 = oops.mapFailureOfNot<NullPointerException> { 0 } // Result(0)
      7. // Chaining
      8. val t2 = okay.mapSuccess { "ok" }.mapFailure { "oops" } // Result("ok")
      9. val t3 = oops.mapSuccess { "ok" }.mapFailure { "oops" } // Result("oops")
      10. /* Result transformation */
      11. val t4 = okay.flatMapSuccess {
      12. if (it == 0) Result("ok") else Result(Exception("oops"))
      13. }
      14. // t4 is Result<String>(Exception)
      15. val t5 = okay.flatMapSuccess {
      16. if (it == 1) Result("ok") else Result(Exception("oops"))
      17. }
      18. // t5 is Result("ok")
      19. // Chaining
      20. val t6 = oops.flatMapSuccess { Result("ok") }.flatMapFailure {
      21. if (it is ArithmeticException) Result("wrong") else Result(Exception("oops"))
      22. }
      23. // t6 is Result("wrong")
      24. val t7 = oops.flatMapSuccess { Result("ok") }.flatMapFailure {
      25. if (it is ArithmeticException) Result(Exception("oops")) else Result("wrong")
      26. }
      27. // t6 is Result<String>(Exception)

AsyncResult

The AsyncResult is an interface designed to represent a Result that may not be currently available but can be obtained on completion of some async execution via theonComplete callback.

The following are implementations:

  • AsyncTask: asynchronously executes code block (see description below);
  • SuspendTask: executes suspension code block (see description below);
  • ThreadTask: asynchronously executes code block in separate thread;
  • ReadyAsyncResult: already complete result.

Usage example:

  1. ThreadTask { Thread.sleep(1000); "ok" }.onComplete { println("v=${it.value}; e=${it.exception}") }

AsyncTask

An AsyncResult of the code block execution.

  • Can be scheduled by some delay.
  • Can run in an executor thread if one is specified. Otherwise runs in the AsyncTask.sharedContext.

Usage example:

  1. AsyncTask(1000) {"ok"}.onComplete { println("v=${it.value}; e=${it.exception}") }

SuspendTask

An AsyncResult of suspension code block execution which is performed in a context of the coroutine.

  • Can specify CoroutineContext
  • Can specify code block receiver

Usage example:

  1. suspend fun longRun(): String { Thread.sleep(1000); return "ok" }
  2. // async execution
  3. val result: AsyncResult<String> = SuspendTask.async { longRun() }
  4. result.onComplete { println("v=${it.value}; e=${it.exception}") }
  5. // suspended execution
  6. val result: Result<String> = SuspendTask { longRun() }
  7. val value = result.valueOr("oops") // = "ok"

Installation

Gradle dependency:

  1. compile 'com.github.just-4-fun:kotlinkit:0.4'

Maven dependency:

  1. <dependency>
  2. <groupId>com.github.just-4-fun</groupId>
  3. <artifactId>kotlinkit</artifactId>
  4. <version>0.4</version>
  5. <type>pom</type>
  6. </dependency>