Example usage for java.util.concurrent CompletableFuture completedFuture

List of usage examples for java.util.concurrent CompletableFuture completedFuture

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture completedFuture.

Prototype

public static <U> CompletableFuture<U> completedFuture(U value) 

Source Link

Document

Returns a new CompletableFuture that is already completed with the given value.

Usage

From source file:org.venice.beachfront.bfapi.services.JobServiceTests.java

@Test
public void testCreateJob() throws UserException {
    // Mock Scene and Algorithm
    String serviceId = "service123";
    String creatorId = "tester123";
    Algorithm mockAlgorithm = new Algorithm("Description", "Interface", 10, "Name", serviceId, "1.0.0");
    Mockito.doReturn(mockAlgorithm).when(algorithmService).getAlgorithm(Mockito.eq(serviceId));
    Scene mockScene = new Scene("scene123", new DateTime(), 10, null, 10, "Sensor", "URI");
    Mockito.doReturn(mockScene).when(sceneService).getScene(Mockito.eq("scene123"), Mockito.anyString(),
            Mockito.anyBoolean());//from  w  w  w  .  java 2s  . com
    Mockito.doReturn(CompletableFuture.completedFuture(mockScene)).when(sceneService)
            .asyncGetActiveScene(Mockito.eq("scene123"), Mockito.anyString(), Mockito.anyBoolean());
    Mockito.doReturn(mockScene).when(sceneService).getSceneFromLocalDatabase(Mockito.eq("scene123"));
    // No redundant jobs
    Mockito.doReturn(new ArrayList<Job>()).when(jobDao)
            .findBySceneIdAndAlgorithmIdAndAlgorithmVersionAndComputeMaskAndStatusAndSeedJobIdIsNull(
                    Mockito.eq(mockScene.getSceneId()), Mockito.eq(serviceId),
                    Mockito.eq(mockAlgorithm.getVersion()), Mockito.any(), Mockito.any());

    // Test
    String jobName = "Test Job";
    Job job = jobService.createJob(jobName, creatorId, mockScene.getSceneId(), serviceId, null, true, null);

    // Verify
    assertNotNull(job);
    assertEquals(job.getAlgorithmId(), serviceId);
    assertEquals(job.getAlgorithmName(), mockAlgorithm.getName());
    assertEquals(job.getAlgorithmVersion(), mockAlgorithm.getVersion());
    assertEquals(job.getCreatedByUserId(), creatorId);
    assertTrue(Seconds.secondsBetween(new DateTime(), job.getCreatedOn()).getSeconds() <= 5);
    assertEquals(job.getJobName(), jobName);
    assertEquals(job.getSceneId(), mockScene.getSceneId());
    assertEquals(job.getStatus(), Job.STATUS_ACTIVATING);
}

From source file:org.venice.beachfront.bfapi.services.PiazzaServiceTests.java

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);

    ReflectionTestUtils.setField(piazzaService, "PIAZZA_URL", "https://piazza.com");
    ReflectionTestUtils.setField(piazzaService, "PIAZZA_API_KEY", "piazzaKey");

    mockScene.setSceneId("test");
    mockScene.setSensorName("rapideye");
    sceneFuture = CompletableFuture.completedFuture(mockScene);

    Mockito.when(sceneService.getSceneInputFileNames(Mockito.isA(Scene.class)))
            .thenReturn(new ArrayList<String>());
    Mockito.when(sceneService.getSceneInputURLs(Mockito.isA(Scene.class))).thenReturn(new ArrayList<String>());

    callback = new JobStatusCallback() {
        @Override/*  w w  w. j ava 2  s  .c  o  m*/
        public void updateStatus(String jobId, String status, String errorMessage) {
            return;
        }
    };
}

From source file:ru.histone.v2.evaluator.Evaluator.java

public CompletableFuture<EvalNode> evaluateNode(AstNode node, Context context) {
    if (node == null) {
        return CompletableFuture.completedFuture(EmptyEvalNode.INSTANCE);
    }//from   ww  w  .  j  a  v a 2  s.  c  o m

    if (node.hasValue()) {
        return getValueNode(node);
    }

    ExpAstNode expNode = (ExpAstNode) node;
    switch (node.getType()) {
    case AST_ARRAY:
        return processArrayNode(expNode, context);
    case AST_REGEXP:
        return processRegExp(expNode);
    case AST_THIS:
        return processThisNode(expNode, context);
    case AST_GLOBAL:
        return processGlobalNode(expNode, context);
    case AST_NOT:
        return processNotNode(expNode, context);
    case AST_AND:
        return processAndNode(expNode, context);
    case AST_OR:
        return processOrNode(expNode, context);
    case AST_TERNARY:
        return processTernary(expNode, context);
    case AST_ADD:
        return processAddNode(expNode, context);
    case AST_SUB:
    case AST_MUL:
    case AST_DIV:
    case AST_MOD:
        return processArithmetical(expNode, context);
    case AST_USUB:
        return processUnaryMinus(expNode, context);
    case AST_LT:
    case AST_GT:
    case AST_LE:
    case AST_GE:
        return processRelation(expNode, context);
    case AST_EQ:
        return processEqNode(expNode, context, true);
    case AST_NEQ:
        return processEqNode(expNode, context, false);
    case AST_REF:
        return processReferenceNode(expNode, context);
    case AST_METHOD:
        return processMethod(expNode, context);
    case AST_PROP:
        return processPropertyNode(expNode, context);
    case AST_CALL:
        return processCall(expNode, context);
    case AST_VAR:
        return processVarNode(expNode, context);
    case AST_IF:
        return processIfNode(expNode, context);
    case AST_FOR:
        return processForNode(expNode, context);
    case AST_MACRO:
        return processMacroNode(expNode, context);
    case AST_RETURN:
        return processReturnNode(expNode, context);
    case AST_NODES:
        return processNodeList(expNode, context, true);
    case AST_NODELIST:
        return processNodeList(expNode, context, false);
    case AST_BOR:
        return processBorNode(expNode, context);
    case AST_BXOR:
        return processBxorNode(expNode, context);
    case AST_BAND:
        return processBandNode(expNode, context);
    case AST_SUPRESS:
        break;
    case AST_LISTEN:
        break;
    case AST_TRIGGER:
        break;
    }
    throw new HistoneException("WTF!?!?!? " + node.getType());

}

From source file:ru.histone.v2.evaluator.Evaluator.java

private CompletableFuture<EvalNode> processGlobalNode(ExpAstNode expNode, Context context) {
    return CompletableFuture.completedFuture(new GlobalEvalNode());
}

From source file:ru.histone.v2.evaluator.Evaluator.java

/**
 * [AST_ID, MACRO_BODY, NUM_OF_VARS, VARS...]
 *///from  w  w  w. j av  a2  s.co  m
private CompletableFuture<EvalNode> processMacroNode(ExpAstNode node, Context context) {
    final int bodyIndex = 0;
    final int startVarIndex = 2;
    final CompletableFuture<List<AstNode>> astArgsFuture = CompletableFuture
            .completedFuture(node.size() < startVarIndex ? Collections.<AstNode>emptyList()
                    : node.getNodes().subList(startVarIndex, node.size()));
    final CompletableFuture<List<String>> argsFuture = astArgsFuture
            .thenApply(astNodes -> astNodes.stream().map(x -> {
                final StringAstNode nameNode = ((ExpAstNode) x).getNode(0);
                return nameNode.getValue();
            }).collect(Collectors.toList()));
    return argsFuture.thenApply(args -> {
        final AstNode body = node.getNode(bodyIndex);
        return new MacroEvalNode(new HistoneMacro(args, body, context, Evaluator.this));
    });
}

From source file:ru.histone.v2.evaluator.Evaluator.java

private CompletableFuture<EvalNode> processTernary(ExpAstNode expNode, Context context) {
    CompletableFuture<EvalNode> condition = evaluateNode(expNode.getNode(0), context);
    return condition.thenCompose(conditionNode -> {
        if (nodeAsBoolean(conditionNode)) {
            return evaluateNode(expNode.getNode(1), context);
        } else if (expNode.getNode(2) != null) {
            return evaluateNode(expNode.getNode(2), context);
        }/*from  w  ww  .  jav a 2 s  .co m*/
        return CompletableFuture.completedFuture(EmptyEvalNode.INSTANCE);
    });
}

From source file:ru.histone.v2.evaluator.Evaluator.java

private CompletionStage<EvalNode> processNonMapValue(ExpAstNode expNode, Context context) {
    if (expNode.size() == 4) {
        return CompletableFuture.completedFuture(EmptyEvalNode.INSTANCE);
    }// w ww .j  a va2  s  .  co m
    int i = 0;
    ExpAstNode expressionNode = expNode.getNode(i + 4);
    ExpAstNode bodyNode = expNode.getNode(i + 5);
    while (bodyNode != null) {
        CompletableFuture<EvalNode> conditionFuture = evaluateNode(expressionNode, context);
        EvalNode conditionNode = conditionFuture.join();
        if (nodeAsBoolean(conditionNode)) {
            return evaluateNode(bodyNode, context);
        }
        i++;
        expressionNode = expNode.getNode(i + 4);
        bodyNode = expNode.getNode(i + 5);
    }
    if (expressionNode != null) {
        return evaluateNode(expressionNode, context);
    }

    return CompletableFuture.completedFuture(EmptyEvalNode.INSTANCE);
}

From source file:ru.histone.v2.evaluator.Evaluator.java

private CompletableFuture<EvalNode> processArrayNode(ExpAstNode node, Context context) {
    if (CollectionUtils.isEmpty(node.getNodes())) {
        return CompletableFuture.completedFuture(new MapEvalNode(Collections.emptyMap()));
    }//from   w w  w.  ja v a 2 s  . c o  m
    if (node.getNode(0).getType() == AstType.AST_VAR) {
        return evalAllNodesOfCurrent(node, context).thenApply(evalNodes -> EmptyEvalNode.INSTANCE);
    } else {
        if (node.size() > 0) {
            CompletableFuture<List<EvalNode>> futures = evalAllNodesOfCurrent(node, context);
            return futures.thenApply(nodes -> {
                Map<String, Object> map = new LinkedHashMap<>();
                for (int i = 0; i < nodes.size() / 2; i++) {
                    EvalNode key = nodes.get(i * 2);
                    EvalNode value = nodes.get(i * 2 + 1);
                    map.put(key.getValue() + "", value.getValue());
                }
                return new MapEvalNode(map);
            });
        } else {
            return CompletableFuture.completedFuture(new MapEvalNode(new LinkedHashMap<>()));
        }
    }
}

From source file:ru.histone.v2.evaluator.Evaluator.java

private CompletableFuture<EvalNode> getValueNode(AstNode node) {
    ValueNode valueNode = (ValueNode) node;
    if (valueNode.getValue() == null) {
        return CompletableFuture.completedFuture(NullEvalNode.INSTANCE);
    }// w  ww  .  j a  va 2 s. c  om

    Object val = valueNode.getValue();
    if (val instanceof Boolean) {
        return CompletableFuture.completedFuture(new BooleanEvalNode((Boolean) val));
    } else if (val instanceof Long) {
        return CompletableFuture.completedFuture(new LongEvalNode((Long) val));
    } else if (val instanceof Float) {
        return CompletableFuture.completedFuture(new FloatEvalNode((Float) val));
    }
    return CompletableFuture.completedFuture(new StringEvalNode(val + ""));
}

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  w ww .j a v a2s. c o  m*/
            return CompletableFuture.completedFuture(EmptyEvalNode.INSTANCE);
        }
    });
}