/**
 * ﻿Copyright 2015-2017 Valery Silaev (http://vsilaev.com)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:

 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.

 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.tascalate.async.core;

import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;

import net.tascalate.async.api.Scheduler;
import net.tascalate.async.api.YieldReply;
import net.tascalate.async.api.suspendable;
import net.tascalate.async.api.Generator;

abstract public class AsyncGenerator<T> extends AsyncMethod {
    public final LazyGenerator<T> generator;
    
    protected AsyncGenerator(Scheduler scheduler) {
        super(scheduler);
        this.generator = new LazyGenerator<>(this);
    }
    
    @Override
    protected final @suspendable void internalRun() {
        boolean success = false;
        try {
    	    generator.begin();
    	    doRun();
    	    success = true;
        } catch (Throwable ex) {
            generator.end(ex);
        } finally {
            if (success) {
                generator.end(null);
            }
        }
    }
    
    abstract protected @suspendable void doRun() throws Throwable;
    
    boolean checkDone() {
        if (future.isDone()) {
            // If we have synchronous error in generator method
            // (as opposed to asynchronous that is managed by consumerLock
            if (!future.isCancelled() && future.isCompletedExceptionally()) {
                try {
                    future.get();
                } catch (final CancellationException | InterruptedException ex) {
                    // Should not happen -- completed exceptionally already checked
                    throw new IllegalStateException(ex);
                } catch (final ExecutionException ex) {
                    Exceptions.sneakyThrow(Exceptions.unrollExecutionException(ex));
                }
            }
            return true;
        } else {
            return false;
        }
    }

    protected Generator<T> yield() {
        return generator;
    }
    
    protected @suspendable YieldReply<T> yield(T readyValue) {
        return generator.produce(Generator.of(readyValue));
    }

    protected @suspendable YieldReply<T> yield(CompletionStage<T> pendingValue) {
        return generator.produce(Generator.of(pendingValue));
    }

    protected @suspendable YieldReply<T> yield(Generator<T> values) {
        return generator.produce(values);
    }
}

@@ -1478,21 +1478,25 @@
 mpletion
-Stage
+Exception
 ;%0Aimport
@@ -1517,34 +1517,31 @@
 current.
-ExecutionException
+CompletionStage
 ;%0A%0Aimpor
@@ -2728,11 +2728,12 @@
 ure.
-get
+join
 ();%0A
@@ -2789,31 +2789,8 @@
 ion 
-%7C InterruptedException 
 ex) 
@@ -2963,21 +2963,22 @@
  (final 
-Execu
+Comple
 tionExce
@@ -3053,13 +3053,14 @@
 roll
-Execu
+Comple
 tion

Simplify exception handling in AsyncGenerator.checkDone