use of com.google.api.generator.engine.ast.LogicalOperationExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createEqualsMethod.
private static MethodDefinition createEqualsMethod(ResourceName resourceName, List<List<String>> tokenHierarchies, TypeStore typeStore) {
// Create method definition variables.
Variable oVariable = Variable.builder().setType(TypeNode.withReference(javaObjectReference)).setName("o").build();
VariableExpr argVarExpr = VariableExpr.builder().setIsDecl(false).setVariable(oVariable).build();
TypeNode thisClassType = typeStore.get(getThisClassName(resourceName));
ValueExpr thisValueExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
ValueExpr trueValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build());
// Create first if statement's return expression
ReturnExpr returnTrueExpr = ReturnExpr.withExpr(trueValueExpr);
// Create second if statement's condition expression
RelationalOperationExpr oEqualsThisExpr = RelationalOperationExpr.equalToWithExprs(argVarExpr, thisValueExpr);
RelationalOperationExpr oNotEqualsNullExpr = RelationalOperationExpr.notEqualToWithExprs(argVarExpr, ValueExpr.createNullExpr());
MethodInvocationExpr getClassMethodInvocationExpr = MethodInvocationExpr.builder().setMethodName("getClass").build();
RelationalOperationExpr getClassEqualsExpr = RelationalOperationExpr.equalToWithExprs(getClassMethodInvocationExpr, getClassMethodInvocationExpr.toBuilder().setExprReferenceExpr(argVarExpr).build());
LogicalOperationExpr orLogicalExpr = LogicalOperationExpr.logicalOrWithExprs(oNotEqualsNullExpr, getClassEqualsExpr);
// Create second if statement's body assignment expression.
Variable thatVariable = Variable.builder().setName("that").setType(thisClassType).build();
VariableExpr thatVariableExpr = VariableExpr.builder().setIsDecl(false).setVariable(thatVariable).build();
CastExpr oCastExpr = CastExpr.builder().setExpr(argVarExpr).setType(thisClassType).build();
AssignmentExpr thatAssignmentExpr = AssignmentExpr.builder().setVariableExpr(thatVariableExpr.toBuilder().setIsDecl(true).build()).setValueExpr(oCastExpr).build();
// PubSub special-case handling - exclude _deleted-topic_.
List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(ts -> !ts.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
// Create return expression in the second if statement's body.
Set<String> tokenSet = getTokenSet(processedTokenHierarchies);
Iterator<String> itToken = tokenSet.iterator();
Expr curTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
while (itToken.hasNext()) {
Expr nextTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
curTokenExpr = LogicalOperationExpr.logicalAndWithExprs(curTokenExpr, nextTokenExpr);
}
ReturnExpr secondIfReturnExpr = ReturnExpr.withExpr(curTokenExpr);
// Code: if (o == this) { return true;}
IfStatement firstIfStatement = IfStatement.builder().setConditionExpr(oEqualsThisExpr).setBody(Arrays.asList(ExprStatement.withExpr(returnTrueExpr))).build();
// Code: if (o != null || getClass() == o.getClass()) { FoobarName that = ((FoobarName) o);
// return ..}
IfStatement secondIfStatement = IfStatement.builder().setConditionExpr(orLogicalExpr).setBody(Arrays.asList(ExprStatement.withExpr(thatAssignmentExpr), ExprStatement.withExpr(secondIfReturnExpr))).build();
// Create method's return expression.
ValueExpr falseValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("false").build());
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setArguments(argVarExpr.toBuilder().setIsDecl(true).build()).setReturnType(TypeNode.BOOLEAN).setName("equals").setReturnExpr(falseValueExpr).setBody(Arrays.asList(firstIfStatement, secondIfStatement)).build();
}
use of com.google.api.generator.engine.ast.LogicalOperationExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeLogicalOperationExprImports.
@Test
public void writeLogicalOperationExprImports() {
MethodInvocationExpr lhsExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(UnaryOperationExpr.class))).setMethodName("isValid").setReturnType(TypeNode.BOOLEAN).build();
VariableExpr rhsExpr = VariableExpr.builder().setVariable(createVariable("isGood", TypeNode.BOOLEAN)).build();
LogicalOperationExpr logicalOperationExpr = LogicalOperationExpr.logicalAndWithExprs(lhsExpr, rhsExpr);
logicalOperationExpr.accept(writerVisitor);
assertEquals("import com.google.api.generator.engine.ast.UnaryOperationExpr;\n\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.LogicalOperationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLogicalOperationExpr_logicalAnd.
@Test
public void writeLogicalOperationExpr_logicalAnd() {
VariableExpr lhsExpr = VariableExpr.withVariable(createVariable("isEmpty", TypeNode.BOOLEAN));
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
MethodInvocationExpr rhsExpr = MethodInvocationExpr.builder().setMethodName("isValid").setExprReferenceExpr(ValueExpr.withValue(ThisObjectValue.withType(classType))).setReturnType(TypeNode.BOOLEAN).build();
LogicalOperationExpr logicalOperationExpr = LogicalOperationExpr.logicalAndWithExprs(lhsExpr, rhsExpr);
logicalOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("isEmpty && this.isValid()");
}
use of com.google.api.generator.engine.ast.LogicalOperationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLogicalOperationExpr_logicalOr.
@Test
public void writeLogicalOperationExpr_logicalOr() {
VariableExpr lhsExpr = VariableExpr.withVariable(createVariable("isGood", TypeNode.BOOLEAN));
MethodInvocationExpr rhsExpr = MethodInvocationExpr.builder().setMethodName("isValid").setReturnType(TypeNode.BOOLEAN).build();
LogicalOperationExpr logicalOperationExpr = LogicalOperationExpr.logicalOrWithExprs(lhsExpr, rhsExpr);
logicalOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("isGood || isValid()");
}
Aggregations