Skip to content

Commit

Permalink
Preparing release
Browse files Browse the repository at this point in the history
  • Loading branch information
vsilaev committed Nov 8, 2018
1 parent 215e604 commit 4f4a8b6
Show file tree
Hide file tree
Showing 31 changed files with 290 additions and 287 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Maven Central](https://img.shields.io/maven-central/v/net.tascalate.async/net.tascalate.async.parent.svg)](https://search.maven.org/artifact/net.tascalate.async/net.tascalate.async.parent/2.3.2/pom) [![GitHub release](https://img.shields.io/github/release/vsilaev/tascalate-async-await.svg)](https://github.com/vsilaev/tascalate-async-await/releases/tag/2.3.2) [![license](https://img.shields.io/github/license/vsilaev/tascalate-async-await.svg)](https://github.com/vsilaev/tascalate-async-await/blob/master/LICENSE)
# Why async-await?
Asynchronous programming has long been a useful way to perform operations that don’t necessarily need to hold up the flow or responsiveness of an application. Generally, these are either compute-bound operations or I/O bound operations. Compute-bound operations are those where computations can be done on a separate thread, leaving the main thread to continue its own processing, while I/O bound operations involve work that takes place externally and may not need to block a thread while such work takes place. Common examples of I/O bound operations are file and network operations.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import net.tascalate.async.Generator;
import net.tascalate.async.AsyncGenerator;
import net.tascalate.async.YieldReply;
import net.tascalate.async.async;
import net.tascalate.concurrent.CompletableTask;
Expand All @@ -54,7 +54,7 @@ public static void main(String[] args) throws Exception {
}

@async static Promise<String> consumer() {
try (Generator<Object> g = producer()) {
try (AsyncGenerator<Object> g = producer()) {
CompletionStage<Object> f = null;
while (null != (f = g.next())) {
System.out.println("Consumed future: " + f);
Expand All @@ -72,7 +72,7 @@ public static void main(String[] args) throws Exception {
return async("Done");
}

@async static Generator<Object> producer() {
@async static AsyncGenerator<Object> producer() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
YieldReply<String> reply = yield(waitString("VALUE " + i, 100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Stream;

import net.tascalate.async.Generator;
import net.tascalate.async.AsyncGenerator;
import net.tascalate.async.Sequence;
import net.tascalate.async.YieldReply;
import net.tascalate.async.async;
Expand Down Expand Up @@ -77,7 +76,7 @@ public static void main(String[] args) {
@async
CompletableFuture<String> mergeStrings(String delimeter) {
StringJoiner joiner = new StringJoiner(", ");
try (Generator<String> generator = produceStrings()) {
try (AsyncGenerator<String> generator = produceStrings()) {
System.out.println("%%MergeStrings - before iterations");
String param = "GO!";
int i = 0;
Expand Down Expand Up @@ -137,7 +136,7 @@ void asyncOperation() {
}

@async
Generator<String> produceStrings() {
AsyncGenerator<String> produceStrings() {

System.out.println("%%ProduceStrings - starting + ");
YieldReply<String> o;
Expand All @@ -148,7 +147,7 @@ Generator<String> produceStrings() {
o = yield(waitString("ABC"));
System.out.println("Processed: " + o + ", " + new Date());

o = yield(Sequence.readyFirst(Stream.of(waitString("PV-1", 2000L), waitString("PV-2", 1500L), waitString("PV-3", 1000L)), 2));
o = yield( AsyncGenerator.readyFirst(waitString("PV-1", 2000L), waitString("PV-2", 1500L), waitString("PV-3", 1000L)) );
System.out.println("AFTER LIST PENDING: " + o);

String s = await(waitString("InternalAsync"));
Expand All @@ -157,7 +156,7 @@ Generator<String> produceStrings() {
o = yield(Sequence.empty());
System.out.println("AFTER EMPTY: " + o);

o = yield(Sequence.from("RV-1", "RV-2", "RV-3"));
o = yield(AsyncGenerator.from("RV-1", "RV-2", "RV-3"));
System.out.println("AFTER LIST READY: " + o);

System.out.println("Is generator interrupted: " + interrupted());
Expand All @@ -170,7 +169,7 @@ Generator<String> produceStrings() {

yield(chainedGenerator());

try (Generator<String> nested = moreStrings()) {
try (AsyncGenerator<String> nested = moreStrings()) {
CompletionStage<String> singleResult;
while (null != (singleResult = nested.next())) {
String v = await(singleResult);
Expand Down Expand Up @@ -198,7 +197,7 @@ Generator<String> produceStrings() {

// Private to ensure that generated accessor methods work
@async
private Generator<String> moreStrings() {
private AsyncGenerator<String> moreStrings() {
yield(waitString("111"));
yield(waitString("222"));
yield("333");
Expand All @@ -208,7 +207,7 @@ private Generator<String> moreStrings() {
}

@async
private Generator<String> moreStringsEx() throws FileNotFoundException {
private AsyncGenerator<String> moreStringsEx() throws FileNotFoundException {
yield(waitString("111"));
yield(waitString("222"));
yield("333");
Expand All @@ -220,7 +219,7 @@ private Generator<String> moreStringsEx() throws FileNotFoundException {
}

@async
Generator<String> chainedGenerator() {
AsyncGenerator<String> chainedGenerator() {
yield(waitString("CHAINED-1"));
yield(waitString("CHAINED-2"));
yield("CHAINED-3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import org.apache.commons.javaflow.core.StackRecorder;

import net.tascalate.async.Generator;
import net.tascalate.async.AsyncGenerator;
import net.tascalate.async.Sequence;
import net.tascalate.async.async;

Expand All @@ -57,7 +57,7 @@ public static void main(String[] args) throws Exception {
return async("Done");
}

@async static Generator<Object> producer() {
@async static AsyncGenerator<Object> producer() {
System.out.println( StackRecorder.get().getRunnable().toString() );
for (int i = 0; i < 10_000_000; i++) {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.tascalate.async.Scheduler;
import net.tascalate.async.SchedulerProvider;
import net.tascalate.async.Sequence;
import net.tascalate.async.AsyncGenerator;
import net.tascalate.async.async;
import net.tascalate.async.xpi.TaskScheduler;
import net.tascalate.concurrent.Promise;
Expand Down Expand Up @@ -53,7 +54,7 @@ public static void main(String[] args) {
@async
static Promise<String> mergeStrings(String delimeter, @SchedulerProvider Scheduler scheduler, int zz) {
StringJoiner joiner = new StringJoiner(delimeter);
try (Sequence<String, Promise<String>> generator = Sequence.from("ABC", "XYZ").stream().map(Promises::from).convert(Sequence.fromStream())) {
try (Sequence<Promise<String>> generator = AsyncGenerator.from("ABC", "XYZ").stream().map(Promises::from).convert(Sequence.fromStream())) {
System.out.println("%%MergeStrings - before iterations");
CompletionStage<String> singleResult;
while (null != (singleResult = generator.next())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import net.tascalate.async.Generator;
import net.tascalate.async.AsyncGenerator;
import net.tascalate.async.Sequence;
import net.tascalate.async.YieldReply;
import net.tascalate.async.async;
Expand Down Expand Up @@ -116,7 +116,7 @@ public CompletionStage<String> asyncOperation(int outerDiv) {
);
}

@async Generator<String> produceMergedStrings() {
@async AsyncGenerator<String> produceMergedStrings() {
SuspendableStream<CompletionStage<String>> alphas =
produceAlphaStrings()
.stream()
Expand All @@ -138,7 +138,7 @@ public CompletionStage<String> asyncOperation(int outerDiv) {

// Private to ensure that generated accessor methods work
@async
private Generator<String> produceNumericStrings() {
private AsyncGenerator<String> produceNumericStrings() {
try {
yield(Sequence.empty());
yield(waitString("111"));
Expand All @@ -152,7 +152,7 @@ private Generator<String> produceNumericStrings() {
}

@async
Generator<String> produceAlphaStrings() {
AsyncGenerator<String> produceAlphaStrings() {
try {
for (String s : Arrays.asList("AAA", "BBB", "CCC", "DDD")) {
yield( waitString(s, 400) );
Expand All @@ -164,7 +164,7 @@ Generator<String> produceAlphaStrings() {
}

@async
Generator<String> producePrefixedStrings(CompletionStage<String> prefix) {
AsyncGenerator<String> producePrefixedStrings(CompletionStage<String> prefix) {
try {
for (int i = 1; i <= 9; i++) {
YieldReply<String> r = yield( prefix.thenCombine(waitString(String.valueOf(i)), (px,v) -> px + v) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@

import java.util.concurrent.CompletionStage;

import net.tascalate.async.Generator;
import net.tascalate.async.AsyncGenerator;
import net.tascalate.concurrent.Promise;
import net.tascalate.concurrent.Promises;

public class DefaultPromisesGenerator<T> extends DefaultPromisesSequence<T> implements PromisesGenerator<T> {

public DefaultPromisesGenerator(Generator<T> delegate) {
public DefaultPromisesGenerator(AsyncGenerator<T> delegate) {
super(delegate);
}

@Override
public Promise<T> next(Object producerParam) {
@SuppressWarnings("unchecked")
Generator<T> _delegate = (Generator<T>)delegate;
AsyncGenerator<T> _delegate = (AsyncGenerator<T>)delegate;
CompletionStage<T> original = _delegate.next(producerParam);
return null == original ? null : Promises.from(original);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
import java.util.concurrent.CompletionStage;

import net.tascalate.async.Sequence;

import net.tascalate.concurrent.Promise;
import net.tascalate.concurrent.Promises;

public class DefaultPromisesSequence<T> implements PromisesSequence<T> {

protected final Sequence<T, ? extends CompletionStage<T>> delegate;
protected final Sequence<? extends CompletionStage<T>> delegate;

public DefaultPromisesSequence(Sequence<T, ? extends CompletionStage<T>> delegate) {
public DefaultPromisesSequence(Sequence<? extends CompletionStage<T>> delegate) {
this.delegate = delegate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import static net.tascalate.async.CallContext.yield;

import java.time.Duration;

import java.util.Iterator;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Stream;

import net.tascalate.async.Generator;
import net.tascalate.async.AsyncGenerator;
import net.tascalate.async.Scheduler;
import net.tascalate.async.Sequence;
import net.tascalate.async.async;

import net.tascalate.async.spi.CurrentCallContext;

import net.tascalate.concurrent.CompletableTask;
Expand All @@ -23,45 +25,45 @@ public class Generators {
private Generators() {}

@SafeVarargs
public static <T, F extends CompletionStage<T>> Generator<T> concat(Sequence<T, F>... sequences) {
public static <T> AsyncGenerator<T> concat(Sequence<? extends CompletionStage<T>>... sequences) {
return concat(Stream.of(sequences));
}

public static <T, F extends CompletionStage<T>> Generator<T> concat(Iterable<? extends Sequence<T, F>> sequences) {
public static <T> AsyncGenerator<T> concat(Iterable<? extends Sequence<? extends CompletionStage<T>>> sequences) {
return concat(sequences.iterator());
}

public static <T, F extends CompletionStage<T>> Generator<T> concat(Stream<? extends Sequence<T, F>> sequences) {
public static <T> AsyncGenerator<T> concat(Stream<? extends Sequence<? extends CompletionStage<T>>> sequences) {
return concat(sequences.iterator());
}

private static @async <T, F extends CompletionStage<T>> Generator<T> concat(Iterator<? extends Sequence<T, F>> sequences) {
private static @async <T> AsyncGenerator<T> concat(Iterator<? extends Sequence<? extends CompletionStage<T>>> sequences) {
while (sequences.hasNext()) {
yield( sequences.next() );
}
return yield();
}

public static @async Generator<Duration> delays(Duration duration) {
public static @async AsyncGenerator<Duration> delays(Duration duration) {
Executor executor = new CurrentSchedulerExecutor(CurrentCallContext.scheduler());
while (true) {
yield( CompletableTask.delay(duration, executor) );
}
}

public static @async Generator<Duration> delays(long timeout, TimeUnit timeUnit) {
public static @async AsyncGenerator<Duration> delays(long timeout, TimeUnit timeUnit) {
Executor executor = new CurrentSchedulerExecutor(CurrentCallContext.scheduler());
while (true) {
yield( CompletableTask.delay(timeout, timeUnit, executor) );
}
}


public static <T> Function<Sequence<T, ? extends CompletionStage<T>>, PromisesSequence<T>> promisesSequence() {
public static <T> Function<Sequence<? extends CompletionStage<T>>, PromisesSequence<T>> promisesSequence() {
return DefaultPromisesSequence::new;
}

public static <T> Function<Generator<T>, PromisesGenerator<T>> promisesGenerator() {
public static <T> Function<AsyncGenerator<T>, PromisesGenerator<T>> promisesGenerator() {
return DefaultPromisesGenerator::new;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
import net.tascalate.async.InteractiveSequence;
import net.tascalate.concurrent.Promise;

public interface PromisesGenerator<T> extends PromisesSequence<T>, InteractiveSequence<T, Promise<T>> {
public interface PromisesGenerator<T> extends PromisesSequence<T>, InteractiveSequence<Promise<T>> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
import net.tascalate.async.Sequence;
import net.tascalate.concurrent.Promise;

public interface PromisesSequence<T> extends Sequence<T, Promise<T>> {
public interface PromisesSequence<T> extends Sequence<Promise<T>> {

}
Loading

0 comments on commit 4f4a8b6

Please sign in to comment.