Example usage for org.apache.commons.javaflow.api Continuation suspend

List of usage examples for org.apache.commons.javaflow.api Continuation suspend

Introduction

In this page you can find the example usage for org.apache.commons.javaflow.api Continuation suspend.

Prototype

public static Object suspend(final Object value) 

Source Link

Document

Stops the running continuation.

Usage

From source file:org.apache.commons.javaflow.examples.cancel.Execution.java

@Override
public @continuable void run() {
    final Object[] array = new String[] { "A", "C", "B" };
    try {// www.  ja  va 2  s  .  c o m
        int i = 1;
        System.out.println("Before suspend");
        Continuation.suspend("XYZ");
        System.out.println("After suspend #" + (i++) + ", should not mutate!!!");
        Continuation.cancel();
        // The line below will never be called -- 
        // first we are canceling continuation
        // then we are destroying it
        array[1] = "CHANGED";
    } finally {
        // This will be called only after cc.destroy() from outer code
        System.out.println("Finally is called, array value is " + Arrays.asList(array));
    }
}

From source file:org.apache.commons.javaflow.examples.cdi.owb.TargetClass.java

@LoggableMethod
protected @continuable void executeNested(final String prefix) {
    System.out.println("In target BEFORE suspend");
    final Object value = Continuation.suspend(this + " @ " + prefix);
    System.out.println("In target AFTER suspend: " + value);
}

From source file:org.apache.commons.javaflow.examples.inheritance.Execution.java

public @continuable void run() {
    // Single abstract method of anonymous class inside @Continuable method
    // Direct invocation over concrete type
    new Runnable() {
        @Override/*w  w w  .j  a v  a 2 s . co m*/
        public @continuable void run() {
            System.out.println("Anonymous class -- before");
            Continuation.suspend("SAM Interface");
            System.out.println("Anonymous class -- after");
        }
    }.run();

    // Invocation over non-continuable supertype/interface
    // Notice that you MUST annotate variable
    @ccs
    Runnable x = new Runnable() {
        @Override
        @continuable
        public void run() {
            System.out.println("Ref Anonymous class -- before");
            Continuation.suspend("Ref SAM Interface");
            System.out.println("Ref Anonymous class -- after");
        }
    };
    x.run();

    @ccs
    IDemo d1 = createDemo();
    d1.call(11);

    DemoConcrete d2 = (DemoConcrete) d1;
    d2.call(77);

    try {
        Integer.valueOf("XYZ");
    } catch (NumberFormatException ex) {
        String message1 = "In ";
        String message2 = "exception ";
        String message3 = "handler";
        System.out.println(message1 + message2 + message3);
        x.run();
    }

}

From source file:org.apache.commons.javaflow.examples.inheritance.Execution.java

@continuable
void suspend(final int i) {
    System.out.println("Exe before suspend");
    Continuation.suspend(i);
    System.out.println("Exe after suspend");
}

From source file:org.apache.commons.javaflow.examples.inner_outer.Execution.java

private @continuable void outerMethod(int i) {
    System.out.println("Exe before suspend");
    Object fromCaller = Continuation.suspend(i);
    System.out.println("Exe after suspend: " + fromCaller);
}

From source file:org.apache.commons.javaflow.examples.interceptor.TargetClass.java

@Override
public @continuable void execute(final String prefix) {
    System.out.println("In target BEFORE suspend");
    final Object value = Continuation.suspend("Target @ " + prefix);
    System.out.println("In target AFTER suspend: " + value);
}

From source file:org.apache.commons.javaflow.examples.invokedynamic.SimpleDynamicLinkage.java

@continuable
static void sayHello() {
    System.out.println("There we go!");
    for (char c = 'A'; c < 'G'; c++)
        Continuation.suspend("Data" + c);
    System.out.println("CallSite continuation finished");
}

From source file:org.apache.commons.javaflow.examples.lambdas.LambdasExample.java

private @continuable void executeAll() {

    //  @Continuable method references and @Continuable SAM interfaces are supported
    ContinuableRunnable r1 = this::lambdaDemo;
    r1.run();//from ww  w .ja  v a2  s  . c  o m

    ContinuableRunnable r2 = () -> {
        System.out.println("ContinuableRunnable by arrow function -- before");
        Continuation.suspend(" ** ContinuableRunnable by arrow function");
        System.out.println("ContinuableRunnable by arrow function -- after");
    };
    r2.run();

    // Lambda reference MUST have annotated CallSite if SAM interface is not @continuable
    // Notice that we still MUST create right interface (ContinuableRunnable in this case)
    @ccs
    Runnable closure = exec(() -> {
        System.out.println("Plain Runnable Lambda by arrow function -- before");
        Continuation.suspend(" ** Plain Runnable Lambda by arrow function" + ref);
        System.out.println("Plain Runnable Lambda by arrow function -- after");
    });
    closure.run();

    // We can't use stream.forEach as is, but we can provide good enough helpers
    // See org.apache.commons.javaflow.examples.lambdas.ContinuableAdapters for
    // definition of "accept" & "forEach"
    List<String> listOfStrings = Arrays.asList("A", null, "B", null, "C");
    Continuations.forEach(listOfStrings.stream().filter(s -> s != null).map(s -> s + s),
            accept(this::yieldString1).andThen(this::yieldString2));

}

From source file:org.apache.commons.javaflow.examples.lambdas.LambdasExample.java

@continuable
void lambdaDemo() {
    System.out.println("Lambda by method reference -- before");
    Continuation.suspend(" ** Lambda by method reference** ");
    System.out.println("Lambda by method reference -- after");
}

From source file:org.apache.commons.javaflow.examples.lambdas.LambdasExample.java

@continuable
void yieldString1(String s) {
    System.out.println("Before yield I " + s);
    Continuation.suspend("yield I " + s);
    System.out.println("After yield I " + s);
}