Search in sources :

Example 1 with UNION_FIELD

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();
}
Also used : PDescriptor(net.morimekta.providence.descriptor.PDescriptor) MessageMemberFormatter(net.morimekta.providence.generator.format.java.shared.MessageMemberFormatter) GeneratorException(net.morimekta.providence.generator.GeneratorException) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter) PContainer(net.morimekta.providence.descriptor.PContainer) JMessage(net.morimekta.providence.generator.format.java.utils.JMessage) ArrayList(java.util.ArrayList) PType(net.morimekta.providence.PType) UNION_FIELD(net.morimekta.providence.generator.format.java.messages.CoreOverridesFormatter.UNION_FIELD) JHelper(net.morimekta.providence.generator.format.java.utils.JHelper) Strings(net.morimekta.util.Strings) PMessageBuilder(net.morimekta.providence.PMessageBuilder) JAnnotation(net.morimekta.providence.generator.format.java.utils.JAnnotation) JField(net.morimekta.providence.generator.format.java.utils.JField) JField(net.morimekta.providence.generator.format.java.utils.JField)

Aggregations

ArrayList (java.util.ArrayList)1 PMessageBuilder (net.morimekta.providence.PMessageBuilder)1 PType (net.morimekta.providence.PType)1 PContainer (net.morimekta.providence.descriptor.PContainer)1 PDescriptor (net.morimekta.providence.descriptor.PDescriptor)1 GeneratorException (net.morimekta.providence.generator.GeneratorException)1 UNION_FIELD (net.morimekta.providence.generator.format.java.messages.CoreOverridesFormatter.UNION_FIELD)1 MessageMemberFormatter (net.morimekta.providence.generator.format.java.shared.MessageMemberFormatter)1 JAnnotation (net.morimekta.providence.generator.format.java.utils.JAnnotation)1 JField (net.morimekta.providence.generator.format.java.utils.JField)1 JHelper (net.morimekta.providence.generator.format.java.utils.JHelper)1 JMessage (net.morimekta.providence.generator.format.java.utils.JMessage)1 Strings (net.morimekta.util.Strings)1 IndentedPrintWriter (net.morimekta.util.io.IndentedPrintWriter)1