2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.cluster;
|
2017-02-14 13:10:23 +02:00
|
|
|
|
|
|
|
|
import java.math.BigInteger;
|
2017-03-14 15:51:44 +05:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
2017-02-23 00:50:07 +05:00
|
|
|
import akka.actor.AbstractActor;
|
2017-03-14 15:51:44 +05:00
|
|
|
import static akka.pattern.PatternsCS.pipe;
|
2017-02-14 13:10:23 +02:00
|
|
|
|
|
|
|
|
//#backend
|
2017-02-23 00:50:07 +05:00
|
|
|
public class FactorialBackend extends AbstractActor {
|
2017-02-14 13:10:23 +02:00
|
|
|
|
|
|
|
|
@Override
|
2017-02-23 00:50:07 +05:00
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(Integer.class, n -> {
|
2017-03-14 15:51:44 +05:00
|
|
|
|
|
|
|
|
CompletableFuture<FactorialResult> result =
|
|
|
|
|
CompletableFuture.supplyAsync(() -> factorial(n))
|
|
|
|
|
.thenApply((factorial) -> new FactorialResult(n, factorial));
|
2017-02-14 13:10:23 +02:00
|
|
|
|
2017-03-17 10:18:15 +01:00
|
|
|
pipe(result, getContext().dispatcher()).to(getSender());
|
2017-02-14 13:10:23 +02:00
|
|
|
|
2017-02-23 00:50:07 +05:00
|
|
|
})
|
|
|
|
|
.build();
|
2017-02-14 13:10:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BigInteger factorial(int n) {
|
|
|
|
|
BigInteger acc = BigInteger.ONE;
|
|
|
|
|
for (int i = 1; i <= n; ++i) {
|
|
|
|
|
acc = acc.multiply(BigInteger.valueOf(i));
|
|
|
|
|
}
|
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#backend
|
|
|
|
|
|