CommonCodegenUtils { public static ExpressionStmt getFilteredKiePMMLNameValueExpression(final String kiePMMLNameValueListParam, final String fieldNameToRef, boolean stringLiteralComparison) { MethodCallExpr argumentBodyExpressionArgument2 = new MethodCallExpr("getName"); argumentBodyExpressionArgument2.setScope(new NameExpr(LAMBDA_PARAMETER_NAME)); MethodCallExpr argumentBodyExpression = new MethodCallExpr("equals"); Expression equalsComparisonExpression; if (stringLiteralComparison) { equalsComparisonExpression = new StringLiteralExpr(fieldNameToRef); } else { equalsComparisonExpression = new NameExpr(fieldNameToRef); } argumentBodyExpression.setArguments(NodeList.nodeList(equalsComparisonExpression, argumentBodyExpressionArgument2)); argumentBodyExpression.setScope(new NameExpr(Objects.class.getName())); ExpressionStmt argumentBody = new ExpressionStmt(argumentBodyExpression); Parameter argumentParameter = new Parameter(parseClassOrInterfaceType(KiePMMLNameValue.class.getName()), LAMBDA_PARAMETER_NAME); LambdaExpr argument = new LambdaExpr(); argument.setEnclosingParameters(true).setParameters(NodeList.nodeList(argumentParameter)); argument.setBody(argumentBody); MethodCallExpr initializerScopeScope = new MethodCallExpr("stream"); initializerScopeScope.setScope(new NameExpr(kiePMMLNameValueListParam)); MethodCallExpr initializerScope = new MethodCallExpr("filter"); initializerScope.setScope(initializerScopeScope); initializerScope.setArguments(NodeList.nodeList(argument)); MethodCallExpr initializer = new MethodCallExpr( "findFirst"); initializer.setScope(initializerScope); VariableDeclarator variableDeclarator = new VariableDeclarator(getTypedClassOrInterfaceType(Optional.class.getName(), Collections.singletonList(KiePMMLNameValue.class.getName())), OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME); variableDeclarator.setInitializer(initializer); VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(NodeList.nodeList(variableDeclarator)); ExpressionStmt toReturn = new ExpressionStmt(); toReturn.setExpression(variableDeclarationExpr); return toReturn; } private  CommonCodegenUtils(); static void populateMethodDeclarations(final ClassOrInterfaceDeclaration toPopulate,
                                                  final Collection<MethodDeclaration> methodDeclarations); static ExpressionStmt getFilteredKiePMMLNameValueExpression(final String kiePMMLNameValueListParam,
                                                                       final String fieldNameToRef,
                                                                       boolean stringLiteralComparison); static void addMapPopulation(final Map<String, MethodDeclaration> toAdd,
                                        final BlockStmt body,
                                        final String mapName); static MethodDeclaration getMethodDeclaration(final String methodName,
                                                         final Map<String, ClassOrInterfaceType> parameterNameTypeMap); static MethodDeclaration getMethodDeclaration(final String methodName); static ReturnStmt getReturnStmt(final String returnedVariableName); static ClassOrInterfaceType getTypedClassOrInterfaceType(final String className,
                                                                    final List<String> typesName ); static void setAssignExpressionValue( final BlockStmt body, final String assignExpressionName, final Expression value); static Optional<AssignExpr> getAssignExpression(final BlockStmt body, final String assignExpressionName); static Optional<ExplicitConstructorInvocationStmt> getExplicitConstructorInvocationStmt(final BlockStmt body); static void setExplicitConstructorInvocationArgument(final ExplicitConstructorInvocationStmt constructorInvocationStmt, final String parameterName, final String value); static Optional<NameExpr> getExplicitConstructorInvocationParameter(final ExplicitConstructorInvocationStmt constructorInvocationStmt, final String parameterName); static Optional<MethodDeclaration> getMethodDeclaration(final ClassOrInterfaceDeclaration classOrInterfaceDeclaration, final String methodName); static MethodDeclaration addMethod(final MethodDeclaration methodTemplate,
                                                 final ClassOrInterfaceDeclaration tableTemplate,
                                                 final String methodName); static Optional<VariableDeclarator> getVariableDeclarator(final BlockStmt body, final String variableName); static String OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME; }
@Test public void getFilteredKiePMMLNameValueExpression() { String kiePMMLNameValueListParam = "KIEPMMLNAMEVALUELISTPARAM"; String fieldNameToRef = "FIELDNAMETOREF"; ExpressionStmt retrieved = CommonCodegenUtils.getFilteredKiePMMLNameValueExpression(kiePMMLNameValueListParam, fieldNameToRef, true); assertNotNull(retrieved); String expected = String.format("%1$s<%2$s> %3$s = %4$s.stream().filter((%2$s %5$s) -> %6$s.equals(\"%7$s\", %5$s.getName())).findFirst();", Optional.class.getName(), KiePMMLNameValue.class.getName(), OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME, kiePMMLNameValueListParam, LAMBDA_PARAMETER_NAME, Objects.class.getName(), fieldNameToRef); String retrievedString = retrieved.toString(); assertEquals(expected, retrievedString); BlockStmt body = new BlockStmt(); body.addStatement(retrieved); Parameter listParameter = new Parameter(CommonCodegenUtils.getTypedClassOrInterfaceType(List.class.getName(), Collections.singletonList(KiePMMLNameValue.class.getName())), kiePMMLNameValueListParam); Parameter fieldRefParameter = new Parameter(parseClassOrInterfaceType(String.class.getName()), fieldNameToRef); commonValidateCompilation(body, Arrays.asList(listParameter, fieldRefParameter)); retrieved = CommonCodegenUtils.getFilteredKiePMMLNameValueExpression(kiePMMLNameValueListParam, fieldNameToRef, false); assertNotNull(retrieved); expected = String.format("%1$s<%2$s> %3$s = %4$s.stream().filter((%2$s %5$s) -> %6$s.equals(%7$s, %5$s.getName())).findFirst();", Optional.class.getName(), KiePMMLNameValue.class.getName(), OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME, kiePMMLNameValueListParam, LAMBDA_PARAMETER_NAME, Objects.class.getName(), fieldNameToRef); retrievedString = retrieved.toString(); assertEquals(expected, retrievedString); body = new BlockStmt(); body.addStatement(retrieved); listParameter = new Parameter(CommonCodegenUtils.getTypedClassOrInterfaceType(List.class.getName(), Collections.singletonList(KiePMMLNameValue.class.getName())), kiePMMLNameValueListParam); fieldRefParameter = new Parameter(parseClassOrInterfaceType(String.class.getName()), fieldNameToRef); commonValidateCompilation(body, Arrays.asList(listParameter, fieldRefParameter)); }