use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class BuilderCommonMemberFormatter method appendMutateConstructor.
private void appendMutateConstructor(JMessage<?> message) {
BlockCommentBuilder comment = new BlockCommentBuilder(writer);
comment.comment("Make a mutating builder off a base " + message.descriptor().getQualifiedName() + ".").newline().param_("base", "The base " + message.descriptor().getName()).finish();
writer.formatln("public _Builder(%s base) {", message.instanceType()).begin().appendln("this();").newline();
if (message.isUnion()) {
writer.formatln("%s = base.%s;", UNION_FIELD, UNION_FIELD).newline();
}
for (JField field : message.declaredOrderFields()) {
boolean checkPresence = message.isUnion() ? field.container() : !field.alwaysPresent();
if (checkPresence) {
writer.formatln("if (base.%s()) {", field.presence()).begin();
}
if (!message.isUnion()) {
writer.formatln("optionals.set(%d);", field.index());
}
if (field.type() != PType.VOID) {
writer.formatln("%s = base.%s;", field.member(), field.member());
}
if (checkPresence) {
writer.end().appendln('}');
}
}
writer.end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class BuilderCommonMemberFormatter method appendEquals.
private void appendEquals(JMessage<?> message) {
writer.appendln("@Override").appendln("public boolean equals(Object o) {").begin().appendln("if (o == this) return true;").appendln("if (o == null || !o.getClass().equals(getClass())) return false;");
if (message.numericalOrderFields().size() > 0) {
writer.formatln("%s._Builder other = (%s._Builder) o;", message.instanceType(), message.instanceType()).appendln("return ");
if (message.isUnion()) {
writer.format("%s.equals(%s, other.%s)", Objects.class.getName(), UNION_FIELD, UNION_FIELD);
} else {
writer.format("%s.equals(optionals, other.optionals)", Objects.class.getName());
}
for (JField field : message.declaredOrderFields()) {
if (field.isVoid())
continue;
writer.append(" &&").appendln(" ");
if (field.type() == MESSAGE) {
writer.format("%s.equals(%s(), other.%s())", Objects.class.getName(), field.getter(), field.getter());
} else {
writer.format("%s.equals(%s, other.%s)", Objects.class.getName(), field.member(), field.member());
}
}
writer.append(';');
} else {
writer.appendln("return true;");
}
writer.end().appendln("}").newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class BuilderCommonMemberFormatter method appendMethods.
@Override
public void appendMethods(JMessage<?> message) throws GeneratorException {
for (JField field : message.declaredOrderFields()) {
appendSetter(message, field);
if (field.container()) {
appendAdder(message, field);
}
appendIsSet(message, field);
if (!message.isUnion()) {
appendIsModified(message, field);
}
appendResetter(message, field);
appendMutableGetters(message, field);
}
if (message.isUnion()) {
appendIsUnionModified(message);
}
if (message.isException()) {
appendInitCause(message);
}
appendEquals(message);
appendHashCode(message);
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class BuilderCoreOverridesFormatter method appendMerge.
private void appendMerge(JMessage<?> message) throws GeneratorException {
writer.appendln(JAnnotation.NON_NULL).appendln("@Override").formatln("public _Builder merge(%s from) {", message.instanceType()).begin();
if (message.isUnion()) {
writer.appendln("if (!from.unionFieldIsSet()) {").appendln(" return this;").appendln("}").newline().appendln("switch (from.unionField()) {").begin();
for (JField field : message.declaredOrderFields()) {
writer.formatln("case %s: {", field.fieldEnum()).begin();
switch(field.type()) {
case VOID:
// Void fields have no value.
writer.formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
break;
case MESSAGE:
writer.formatln("if (%s == _Field.%s && %s != null) {", UNION_FIELD, field.fieldEnum(), field.member()).formatln(" %s = %s.mutate().merge(from.%s()).build();", field.member(), field.member(), field.getter()).appendln("} else {").formatln(" %s(from.%s());", field.setter(), field.getter()).appendln('}');
break;
case SET:
writer.formatln("if (%s == _Field.%s) {", UNION_FIELD, field.fieldEnum()).formatln(" %s().addAll(from.%s());", field.mutable(), field.getter()).appendln("} else {").formatln(" %s(from.%s());", field.setter(), field.getter()).appendln('}');
break;
case MAP:
writer.formatln("if (%s == _Field.%s) {", UNION_FIELD, field.fieldEnum()).formatln(" %s().putAll(from.%s());", field.mutable(), field.getter()).appendln("} else {").formatln(" %s(from.%s());", field.setter(), field.getter()).appendln('}');
break;
default:
writer.formatln("%s(from.%s());", field.setter(), field.getter());
break;
}
writer.appendln("break;").end().appendln('}');
}
writer.end().appendln('}');
} else {
boolean first = true;
for (JField field : message.declaredOrderFields()) {
if (first) {
first = false;
} else {
writer.newline();
}
if (!field.alwaysPresent()) {
writer.formatln("if (from.%s()) {", field.presence()).begin();
}
writer.formatln("optionals.set(%d);", field.index());
writer.formatln("modified.set(%d);", field.index());
switch(field.type()) {
case MESSAGE:
// Message fields are merged.
writer.formatln("if (%s_builder != null) {", field.member()).formatln(" %s_builder.merge(from.%s());", field.member(), field.getter()).formatln("} else if (%s != null) {", field.member()).formatln(" %s_builder = %s.mutate().merge(from.%s());", field.member(), field.member(), field.getter()).formatln(" %s = null;", field.member()).appendln("} else {").formatln(" %s = from.%s();", field.member(), field.getter()).appendln('}');
break;
case SET:
writer.formatln("%s().addAll(from.%s());", field.mutable(), field.getter());
break;
case MAP:
writer.formatln("%s().putAll(from.%s());", field.mutable(), field.getter());
break;
default:
writer.formatln("%s = from.%s();", field.member(), field.getter());
break;
}
if (!field.alwaysPresent()) {
writer.end().appendln('}');
}
}
}
writer.appendln("return this;").end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField 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