aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/scala/async/iterators/IteratorState.scala
blob: 5b8cf06263a734d504e607f960ef7b309d46a392 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package scala.async.iterators

import scala.util.{Try, Success, Failure}

trait IteratorState[T] {

  def apply(v: Try[Any]): Unit
  def apply: Unit
  def `result$async` : IteratorState[T]

  private[this] var _value: Option[T] = None
  private[this] var _exc: Throwable   = null

  def result_= (value: T) =
    _value = Some(value)

  def result: T = {
    if (_exc != null)
      throw _exc
    else
      _value.get
  }

  def exception_= (exc: Throwable) =
    _exc = exc

  def exception: Throwable =
    _exc

  def isFailed: Boolean =
    _exc != null

  def onComplete(cont: IteratorState[_]) = {
    // cont will always be `this`
    /* do nothing */
  }

  def next: T = {
    // continue iteration with next state
    this.apply(Success(result))
    // return current result
    result
  }

  def hasNext: Boolean = {
    println("invoking apply")
    apply
    _value.nonEmpty
  }
}