Refactor JDeffered (#2559)

This commit is contained in:
Grzegorz Piwowarek 2017-09-05 19:59:42 +02:00 committed by GitHub
parent 7b836d9211
commit cafdf51bd7
8 changed files with 42 additions and 68 deletions

View File

@ -4,25 +4,21 @@ import org.jdeferred.Deferred;
import org.jdeferred.Promise;
import org.jdeferred.impl.DeferredObject;
public class FilterDemo {
class FilterDemo {
static String modifiedMsg;
private static String modifiedMsg;
public static String filter(String msg) {
static String filter(String msg) {
Deferred<String, ?, ?> d = new DeferredObject<String, Object, Object>();
Deferred<String, ?, ?> d = new DeferredObject<>();
Promise<String, ?, ?> p = d.promise();
Promise<String, ?, ?> filtered = p.then((result) -> {
modifiedMsg = "Hello " + result;
});
filtered.done((result) -> {
System.out.println("filtering done");
});
filtered.done(r -> System.out.println("filtering done"));
d.resolve(msg);
return modifiedMsg;
}
}

View File

@ -5,35 +5,31 @@ import org.jdeferred.DonePipe;
import org.jdeferred.Promise;
import org.jdeferred.impl.DeferredObject;
public class PipeDemo {
class PipeDemo {
public static enum Result {
public enum Result {
SUCCESS, FAILURE
};
static Result status;
private static Result status;
public static Result validate(int num) {
Deferred<Integer, ?, ?> d = new DeferredObject<Integer, Object, Object>();
static Result validate(int num) {
Deferred<Integer, ?, ?> d = new DeferredObject<>();
Promise<Integer, ?, ?> p = d.promise();
p.then(new DonePipe<Integer, Integer, Exception, Void>() {
public Deferred<Integer, Exception, Void> pipeDone(Integer result) {
if (result < 90) {
return new DeferredObject<Integer, Exception, Void>().resolve(result);
} else {
return new DeferredObject<Integer, Exception, Void>().reject(new Exception("Unacceptable value"));
}
p.then((DonePipe<Integer, Integer, Exception, Void>) result -> {
if (result < 90) {
return new DeferredObject<Integer, Exception, Void>()
.resolve(result);
} else {
return new DeferredObject<Integer, Exception, Void>()
.reject(new Exception("Unacceptable value"));
}
}).done((result) -> {
status = Result.SUCCESS;
}).fail((result) -> {
status = Result.FAILURE;
});
}).done(r -> status = Result.SUCCESS)
.fail(r -> status = Result.FAILURE);
d.resolve(num);
return status;
}
}

View File

@ -4,22 +4,17 @@ import org.jdeferred.Deferred;
import org.jdeferred.Promise;
import org.jdeferred.impl.DeferredObject;
public class PromiseDemo {
class PromiseDemo {
public static void startJob(String jobName) {
static void startJob(String jobName) {
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
Deferred<String, String, String> deferred = new DeferredObject<>();
Promise<String, String, String> promise = deferred.promise();
promise.done((result) -> {
System.out.println("Job done");
}).fail((rejection) -> {
System.out.println("Job fail");
}).progress((progress) -> {
System.out.println("Job is in progress");
}).always((state, result, rejection) -> {
System.out.println("Job execution started");
});
promise.done(result -> System.out.println("Job done"))
.fail(rejection -> System.out.println("Job fail"))
.progress(progress -> System.out.println("Job is in progress"))
.always((state, result, rejection) -> System.out.println("Job execution started"));
deferred.resolve(jobName);
// deferred.notify("");

View File

@ -10,13 +10,11 @@ public class ThreadSafeDemo {
public static void task() {
DeferredManager dm = new DefaultDeferredManager();
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
Deferred<String, String, String> deferred = new DeferredObject<>();
Promise<String, String, String> p1 = deferred.promise();
Promise<String, String, String> p = dm.when(p1).done((result) -> {
System.out.println("done");
}).fail((result) -> {
System.out.println("fail");
});
Promise<String, String, String> p = dm.when(p1)
.done(r -> System.out.println("done"))
.fail(r -> System.out.println("fail"));
synchronized (p) {
while (p.isPending()) {

View File

@ -6,10 +6,10 @@ import org.jdeferred.Promise;
import org.jdeferred.impl.DefaultDeferredManager;
import org.jdeferred.impl.DeferredObject;
public class DeferredManagerDemo {
class DeferredManagerDemo {
public static void initiate() {
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
Deferred<String, String, String> deferred = new DeferredObject<>();
DeferredManager dm = new DefaultDeferredManager();
Promise<String, String, String> p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise();
dm.when(p1, p2, p3).done((result) -> {
@ -19,5 +19,4 @@ public class DeferredManagerDemo {
});
deferred.resolve("Hello Baeldung");
}
}

View File

@ -9,19 +9,16 @@ import org.jdeferred.Promise;
import org.jdeferred.impl.DefaultDeferredManager;
import org.jdeferred.impl.DeferredObject;
public class DeferredManagerWithExecutorDemo {
class DeferredManagerWithExecutorDemo {
public static void initiate() {
ExecutorService executor = Executors.newFixedThreadPool(10);
Deferred<String, String, String> deferred = new DeferredObject<String, String, String>();
Deferred<String, String, String> deferred = new DeferredObject<>();
DeferredManager dm = new DefaultDeferredManager(executor);
Promise<String, String, String> p1 = deferred.promise(), p2 = deferred.promise(), p3 = deferred.promise();
dm.when(p1, p2, p3).done((result) -> {
System.out.println("done");
}).fail((result) -> {
System.out.println("fail");
});
dm.when(p1, p2, p3)
.done(r -> System.out.println("done"))
.fail(r -> System.out.println("fail"));
deferred.resolve("done");
}
}

View File

@ -3,17 +3,12 @@ package com.baeldung.jdeffered.manager;
import org.jdeferred.DeferredManager;
import org.jdeferred.impl.DefaultDeferredManager;
public class SimpleDeferredManagerDemo {
class SimpleDeferredManagerDemo {
public static void initiate() {
DeferredManager dm = new DefaultDeferredManager();
dm.when(() -> {
return 1;
}).done((result) -> {
System.out.println("done");
}).fail((e) -> {
e.printStackTrace();
});
dm.when(() -> 1)
.done(r -> System.out.println("done"))
.fail(Throwable::printStackTrace);
}
}

View File

@ -1,8 +1,7 @@
package com.baeldung.jdeffered;
import org.junit.Test;
import com.baeldung.jdeffered.PipeDemo.Result;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ -24,5 +23,4 @@ public class AppTest {
Result result = PipeDemo.validate(80);
assertEquals(result, Result.SUCCESS);
}
}