use of java.util.function.Function in project android by JetBrains.
the class InstallTaskTest method runCallbacks.
@Test
public void runCallbacks() throws Exception {
Runnable prepareComplete = Mockito.mock(Runnable.class);
myInstallTask.setPrepareCompleteCallback(prepareComplete);
Function<List<RepoPackage>, Void> complete = (Function<List<RepoPackage>, Void>) Mockito.mock(Function.class);
myInstallTask.setCompleteCallback(complete);
myInstallTask.run(new StudioProgressIndicatorAdapter(myProgressIndicator, new EmptyProgressIndicator()));
InOrder callbackCalls = Mockito.inOrder(myInstaller, prepareComplete, complete);
callbackCalls.verify(myInstaller).prepare(myProgressIndicator);
callbackCalls.verify(prepareComplete).run();
callbackCalls.verify(myInstaller).complete(myProgressIndicator);
callbackCalls.verify(complete).apply(new ArrayList<>());
}
use of java.util.function.Function in project midpoint by Evolveum.
the class WfExpressionEvaluationHelper method evaluateExpression.
@SuppressWarnings("unchecked")
@NotNull
public <T> List<T> evaluateExpression(ExpressionType expressionType, ExpressionVariables variables, String contextDescription, Class<T> clazz, QName typeName, Function<Object, Object> additionalConvertor, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
ExpressionFactory expressionFactory = getExpressionFactory();
PrismContext prismContext = expressionFactory.getPrismContext();
PrismPropertyDefinition<String> resultDef = new PrismPropertyDefinitionImpl<>(new QName(SchemaConstants.NS_C, "result"), typeName, prismContext);
Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(expressionType, resultDef, contextDescription, task, result);
ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, contextDescription, task, result);
context.setAdditionalConvertor(additionalConvertor);
PrismValueDeltaSetTriple<PrismPropertyValue<String>> exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, context, task, result);
return exprResultTriple.getZeroSet().stream().map(ppv -> (T) ppv.getRealValue()).collect(Collectors.toList());
}
use of java.util.function.Function in project symja_android_library by axkr.
the class Replace method replaceExpr.
private static IExpr replaceExpr(final IAST ast, IExpr arg1, IExpr rules, final EvalEngine engine) {
if (rules.isListOfLists()) {
IAST result = F.List();
for (IExpr list : (IAST) rules) {
IAST subList = (IAST) list;
IExpr temp = F.NIL;
for (IExpr element : subList) {
if (element.isRuleAST()) {
IAST rule = (IAST) element;
Function<IExpr, IExpr> function = Functors.rules(rule);
temp = function.apply(arg1);
if (temp.isPresent()) {
break;
}
} else {
WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
throw wat;
}
}
if (temp.isPresent()) {
result.append(temp);
} else {
result.append(arg1);
}
}
return result;
} else if (rules.isList()) {
for (IExpr element : (IAST) rules) {
if (element.isRuleAST()) {
IAST rule = (IAST) element;
Function<IExpr, IExpr> function = Functors.rules(rule);
IExpr temp = function.apply(arg1);
if (temp.isPresent()) {
return temp;
}
} else {
WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
throw wat;
}
}
return arg1;
}
if (rules.isRuleAST()) {
return replaceRule(arg1, (IAST) rules);
} else {
WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
engine.printMessage(wat.getMessage());
}
return F.NIL;
}
use of java.util.function.Function in project gatk by broadinstitute.
the class AdaptiveMetropolisSamplerUnitTest method testBeta.
@Test
public void testBeta() {
final RandomGenerator rng = RandomGeneratorFactory.createRandomGenerator(new Random(RANDOM_SEED));
for (final double a : Arrays.asList(10, 20, 30)) {
for (final double b : Arrays.asList(10, 20, 30)) {
final double theoreticalMean = a / (a + b);
final double theoreticalVariance = a * b / ((a + b) * (a + b) * (a + b + 1));
//Note: this is the theoretical standard deviation of the sample mean given uncorrelated
//samples. The sample mean will have a greater variance here because samples are correlated.
final double standardDeviationOfMean = Math.sqrt(theoreticalVariance / NUM_SAMPLES);
final Function<Double, Double> logPDF = x -> (a - 1) * Math.log(x) + (b - 1) * Math.log(1 - x);
final AdaptiveMetropolisSampler sampler = new AdaptiveMetropolisSampler(INITIAL_BETA_GUESS, INITIAL_STEP_SIZE, 0, 1);
final List<Double> samples = sampler.sample(rng, logPDF, NUM_SAMPLES, NUM_BURN_IN_STEPS);
final double sampleMean = samples.stream().mapToDouble(x -> x).average().getAsDouble();
final double sampleMeanSquare = samples.stream().mapToDouble(x -> x * x).average().getAsDouble();
final double sampleVariance = (sampleMeanSquare - sampleMean * sampleMean) * NUM_SAMPLES / (NUM_SAMPLES - 1);
Assert.assertEquals(sampleMean, theoreticalMean, 10 * standardDeviationOfMean);
Assert.assertEquals(sampleVariance, theoreticalVariance, 10e-4);
}
}
}
use of java.util.function.Function in project gatk by broadinstitute.
the class AdaptiveMetropolisSamplerUnitTest method testGaussian.
@Test
public void testGaussian() {
final RandomGenerator rng = RandomGeneratorFactory.createRandomGenerator(new Random(RANDOM_SEED));
for (final double theoreticalMean : Arrays.asList(0)) {
for (final double precision : Arrays.asList(1.0)) {
final double variance = 1 / precision;
//Note: this is the theoretical standard deviation of the sample mean given uncorrelated
//samples. The sample mean will have a greater variance here because samples are correlated.
final double standardDeviationOfMean = Math.sqrt(variance / NUM_SAMPLES);
final Function<Double, Double> logPDF = x -> -(precision / 2) * (x - theoreticalMean) * (x - theoreticalMean);
final AdaptiveMetropolisSampler sampler = new AdaptiveMetropolisSampler(INITIAL_GAUSSIAN_GUESS, INITIAL_STEP_SIZE, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
final List<Double> samples = sampler.sample(rng, logPDF, NUM_SAMPLES, NUM_BURN_IN_STEPS);
final double sampleMean = samples.stream().mapToDouble(x -> x).average().getAsDouble();
final double sampleMeanSquare = samples.stream().mapToDouble(x -> x * x).average().getAsDouble();
final double sampleVariance = (sampleMeanSquare - sampleMean * sampleMean) * NUM_SAMPLES / (NUM_SAMPLES - 1);
Assert.assertEquals(sampleMean, theoreticalMean, 6 * standardDeviationOfMean);
Assert.assertEquals(sampleVariance, variance, variance / 10);
}
}
}
Aggregations