use of net.morimekta.providence.generator.format.java.messages.CoreOverridesFormatter.UNION_FIELD in project providence by morimekta.
the class BuilderCoreOverridesFormatter method appendOverrideIsValid.
private void appendOverrideIsValid(JMessage<?> message) {
writer.appendln("@Override").appendln("public boolean valid() {").begin();
if (message.isUnion()) {
writer.formatln("if (%s == null) {", UNION_FIELD).appendln(" return false;").appendln('}').newline().formatln("switch (%s) {", UNION_FIELD).begin();
message.numericalOrderFields().stream().filter(field -> !field.alwaysPresent()).forEachOrdered(field -> {
if (field.isVoid()) {
// Void fields have no value.
writer.formatln("case %s: return true;", field.fieldEnum());
} else if (field.type() == PType.MESSAGE) {
writer.formatln("case %s: return %s != null || %s_builder != null;", field.fieldEnum(), field.member(), field.member());
} else {
writer.formatln("case %s: return %s != null;", field.fieldEnum(), field.member());
}
});
writer.appendln("default: return true;").end().appendln('}');
} else {
writer.appendln("return ").begin(" ");
boolean first = true;
for (JField field : message.declaredOrderFields()) {
if (field.isRequired()) {
if (first) {
first = false;
} else {
writer.append(" &&").appendln("");
}
writer.format("optionals.get(%d)", field.index());
}
}
if (first) {
writer.append("true");
}
// alignment indent
writer.end().append(';');
}
writer.end().appendln('}').newline();
}
Aggregations