List of usage examples for java.util.concurrent CompletableFuture thenCompose
public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)
From source file:ru.histone.v2.evaluator.Evaluator.java
private CompletableFuture<EvalNode> processCall(ExpAstNode expNode, Context context) { final ExpAstNode node = expNode.getNode(0); final boolean valueNodeExists = node.size() > 1; final List<AstNode> paramsAstNodes = expNode.getNodes().subList(1, expNode.getNodes().size()); final CompletableFuture<EvalNode> functionNameFuture = evaluateNode(node.getNode(valueNodeExists ? 1 : 0), context);/*from www. j ava2 s.co m*/ CompletableFuture<List<EvalNode>> argsFuture = sequence( paramsAstNodes.stream().map(x -> evaluateNode(x, context)).collect(Collectors.toList())); return argsFuture.thenCompose(args -> functionNameFuture.thenCompose(functionNameNode -> { if (functionNameNode instanceof StringEvalNode && !valueNodeExists) { return context.call((String) functionNameNode.getValue(), args); } else { return processMethod(node, context, args); } })); }
From source file:ru.histone.v2.evaluator.Evaluator.java
private CompletableFuture<EvalNode> processForNode(ExpAstNode expNode, Context context) { CompletableFuture<EvalNode> objToIterateFuture = evaluateNode(expNode.getNode(2), context); return objToIterateFuture.thenCompose(objToIterate -> { if (!(objToIterate instanceof MapEvalNode)) { return processNonMapValue(expNode, context); } else {/*from w ww . ja v a 2 s. c om*/ return processMapValue(expNode, context, (MapEvalNode) objToIterate); } }); }
From source file:ru.histone.v2.evaluator.Evaluator.java
private CompletableFuture<EvalNode> processMapValue(ExpAstNode expNode, Context context, MapEvalNode objToIterate) {/* www. jav a 2s. com*/ CompletableFuture<EvalNode> keyVarName = evaluateNode(expNode.getNode(0), context); CompletableFuture<EvalNode> valueVarName = evaluateNode(expNode.getNode(1), context); CompletableFuture<List<EvalNode>> leftRightDone = sequence(keyVarName, valueVarName); return leftRightDone.thenCompose(keyValueNames -> { CompletableFuture<List<EvalNode>> res = iterate(expNode, context, objToIterate, keyValueNames.get(0), keyValueNames.get(1)); return getEvaluatedString(context, res); }); }
From source file:ru.histone.v2.evaluator.Evaluator.java
private CompletableFuture<EvalNode> processAddNode(ExpAstNode node, Context context) { CompletableFuture<List<EvalNode>> leftRight = evalAllNodesOfCurrent(node, context); return leftRight.thenCompose(lr -> { EvalNode left = lr.get(0);// w w w. j a v a 2s.c o m EvalNode right = lr.get(1); if (!(left instanceof StringEvalNode || right instanceof StringEvalNode)) { if (isNumberNode(left) && isNumberNode(right)) { Float res = getValue(left).orElse(null) + getValue(right).orElse(null); if (res % 1 == 0 && res <= Long.MAX_VALUE) { return EvalUtils.getValue(res.longValue()); } else { return EvalUtils.getValue(res); } } if (left instanceof MapEvalNode && right instanceof MapEvalNode) { throw new NotImplementedException(); } } CompletableFuture<List<EvalNode>> lrFutures = sequence( context.call(TO_STRING_FUNC_NAME, Collections.singletonList(left)), context.call(TO_STRING_FUNC_NAME, Collections.singletonList(right))); return lrFutures.thenCompose(futures -> { StringEvalNode l = (StringEvalNode) futures.get(0); StringEvalNode r = (StringEvalNode) futures.get(1); return EvalUtils.getValue(l.getValue() + r.getValue()); }); }); }
From source file:ru.histone.v2.evaluator.Evaluator.java
private CompletableFuture<EvalNode> processReferenceNode(ExpAstNode node, Context context) { StringAstNode valueNode = node.getNode(0); CompletableFuture<EvalNode> value = getValueFromParentContext(context, valueNode.getValue()); return value.thenCompose(v -> { if (v != null) { return CompletableFuture.completedFuture(v); } else {/*from ww w . ja v a 2 s.c o m*/ return CompletableFuture.completedFuture(EmptyEvalNode.INSTANCE); } }); }
From source file:ru.histone.v2.evaluator.Evaluator.java
private CompletableFuture<EvalNode> processIfNode(ExpAstNode node, Context context) { CompletableFuture<EvalNode> conditionFuture = evaluateNode(node.getNode(1), context); return conditionFuture.thenCompose(condNode -> { Context current = context.createNew(); CompletableFuture<EvalNode> result; if (nodeAsBoolean(condNode)) { result = evaluateNode(node.getNode(0), current); } else {// w w w. ja v a 2 s . c o m result = evaluateNode(node.getNode(2), current); } current.release(); return result; }); }