use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class BuilderCoreOverridesFormatter method appendOverrideResetter.
private void appendOverrideResetter(JMessage<?> message) {
writer.appendln(JAnnotation.NON_NULL).appendln("@Override").appendln("public _Builder clear(int key) {").begin().appendln("switch (key) {").begin();
for (JField field : message.numericalOrderFields()) {
writer.formatln("case %d: %s(); break;", field.id(), field.resetter());
}
writer.appendln("default: break;").end().appendln('}').appendln("return this;").end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class CommonMemberFormatter method appendCreateConstructor.
private void appendCreateConstructor(JMessage<?> message) throws GeneratorException {
if (message.isUnion()) {
for (JField field : message.declaredOrderFields()) {
BlockCommentBuilder block = new BlockCommentBuilder(writer);
if (field.hasComment()) {
block.comment(field.comment());
}
if (field.isVoid()) {
block.return_("The created union.").finish();
writer.formatln("public static %s %s() {", message.instanceType(), camelCase("with", field.name())).begin().formatln("return new _Builder().%s().build();", field.setter()).end().appendln('}').newline();
} else {
block.param_("value", "The union value").return_("The created union.").finish();
writer.formatln("public static %s %s(%s value) {", message.instanceType(), camelCase("with", field.name()), field.valueType()).begin().formatln("return new _Builder().%s(value).build();", field.setter()).end().appendln('}').newline();
if (field.type() == PType.MESSAGE) {
appendCreateConstructorBuilderOverload(message, field);
}
}
}
} else {
if (message.hasAnnotation(ThriftAnnotation.JAVA_PUBLIC_CONSTRUCTOR) || javaOptions.public_constructors) {
String spaces = message.instanceType().replaceAll("[\\S]", " ");
writer.formatln("public %s(", message.instanceType()).begin(" " + spaces);
boolean first = true;
for (JField field : message.declaredOrderFields()) {
if (field.isVoid()) {
// Void fields have no value.
continue;
}
if (first) {
first = false;
} else {
writer.append(',').appendln();
}
writer.format("%s %s", field.paramType(), field.param());
}
writer.end().append(") {").begin();
if (message.isException()) {
// If an exception class contains a field named 'message' (no caps), it will use that
// as the exception message unmodified.
Optional<JField> msg = message.exceptionMessageField();
if (msg.isPresent()) {
writer.formatln("super(%s);", msg.get().param()).newline();
} else {
writer.appendln("super(createMessage(").begin(" ");
first = true;
for (JField field : message.declaredOrderFields()) {
// Void fields have no value.
if (field.isVoid()) {
continue;
}
if (first) {
first = false;
} else {
writer.append(',').appendln();
}
writer.format("%s", field.param());
}
writer.append("));").end().newline();
}
}
for (JField field : message.declaredOrderFields()) {
// Void fields have no value.
if (field.isVoid()) {
continue;
}
switch(field.type()) {
case LIST:
{
writer.formatln("if (%s != null) {", field.param()).formatln(" %s = %s;", field.member(), field.fieldInstanceCopy(field.param())).appendln("} else {");
if (field.alwaysPresent()) {
writer.formatln(" %s = %s;", field.member(), field.kDefault());
} else {
writer.formatln(" %s = null;", field.member());
}
writer.appendln('}');
break;
}
case SET:
{
writer.formatln("if (%s != null) {", field.param()).formatln(" %s = %s;", field.member(), field.fieldInstanceCopy(field.param())).appendln("} else {");
if (field.alwaysPresent()) {
writer.formatln(" %s = %s;", field.member(), field.kDefault());
} else {
writer.formatln(" %s = null;", field.member());
}
writer.appendln('}');
break;
}
case MAP:
{
writer.formatln("if (%s != null) {", field.param()).formatln(" %s = %s;", field.member(), field.fieldInstanceCopy(field.param())).appendln("} else {");
if (field.alwaysPresent()) {
writer.formatln(" %s = %s;", field.member(), field.kDefault());
} else {
writer.formatln(" %s = null;", field.member());
}
writer.appendln('}');
break;
}
default:
{
if (field.alwaysPresent() && !(field.isRequired() && field.isPrimitiveJavaValue())) {
writer.formatln("if (%s != null) {", field.param()).formatln(" %s = %s;", field.member(), field.param()).appendln("} else {").formatln(" %s = %s;", field.member(), field.kDefault()).appendln('}');
} else {
writer.formatln("%s = %s;", field.member(), field.param());
}
break;
}
}
}
writer.end().appendln('}').newline();
}
}
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class CommonOverridesFormatter method appendCompareTo.
private void appendCompareTo(JMessage<?> message) {
writer.appendln("@Override").formatln("public int compareTo(%s other) {", message.instanceType()).begin();
if (message.isUnion()) {
writer.formatln("if (%s == null || other.%s == null) return Boolean.compare(%s != null, other.%s != null);", UNION_FIELD, UNION_FIELD, UNION_FIELD, UNION_FIELD).formatln("int c = %s.compareTo(other.%s);", UNION_FIELD, UNION_FIELD).appendln("if (c != 0) return c;").newline().formatln("switch (%s) {", UNION_FIELD).begin();
for (JField field : message.numericalOrderFields()) {
writer.formatln("case %s:", caseFieldConstant(field)).begin();
switch(field.type()) {
case BOOL:
writer.formatln("return Boolean.compare(%s, other.%s);", field.member(), field.member());
break;
case BYTE:
writer.formatln("return Byte.compare(%s, other.%s);", field.member(), field.member());
break;
case I16:
writer.formatln("return Short.compare(%s, other.%s);", field.member(), field.member());
break;
case I32:
writer.formatln("return Integer.compare(%s, other.%s);", field.member(), field.member());
break;
case I64:
writer.formatln("return Long.compare(%s, other.%s);", field.member(), field.member());
break;
case DOUBLE:
writer.formatln("return Double.compare(%s, other.%s);", field.member(), field.member());
break;
case STRING:
case BINARY:
case MESSAGE:
writer.formatln("return %s.compareTo(other.%s);", field.member(), field.member());
break;
case ENUM:
writer.formatln("return Integer.compare(%s.asInteger(), other.%s.asInteger());", field.member(), field.member());
break;
case SET:
case LIST:
case MAP:
// containers aren't really comparable, just make some consistent comparison.
writer.formatln("return Integer.compare(%s.hashCode(), other.%s.hashCode());", field.member(), field.member());
break;
default:
break;
}
writer.end();
}
writer.appendln("default: return 0;").end().appendln('}');
} else {
writer.appendln("int c;");
for (JField field : message.numericalOrderFields()) {
writer.newline();
if (!field.alwaysPresent()) {
writer.formatln("c = Boolean.compare(%s != null, other.%s != null);", field.member(), field.member()).appendln("if (c != 0) return c;").formatln("if (%s != null) {", field.member()).begin();
}
switch(field.type()) {
case BOOL:
writer.formatln("c = Boolean.compare(%s, other.%s);", field.member(), field.member());
break;
case BYTE:
writer.formatln("c = Byte.compare(%s, other.%s);", field.member(), field.member());
break;
case I16:
writer.formatln("c = Short.compare(%s, other.%s);", field.member(), field.member());
break;
case I32:
writer.formatln("c = Integer.compare(%s, other.%s);", field.member(), field.member());
break;
case I64:
writer.formatln("c = Long.compare(%s, other.%s);", field.member(), field.member());
break;
case DOUBLE:
writer.formatln("c = Double.compare(%s, other.%s);", field.member(), field.member());
break;
case STRING:
case BINARY:
case MESSAGE:
writer.formatln("c = %s.compareTo(other.%s);", field.member(), field.member());
break;
case ENUM:
writer.formatln("c = Integer.compare(%s.ordinal(), %s.ordinal());", field.member(), field.member());
break;
case SET:
case LIST:
case MAP:
writer.formatln("c = Integer.compare(%s.hashCode(), other.%s.hashCode());", field.member(), field.member());
break;
default:
break;
}
writer.appendln("if (c != 0) return c;");
if (!field.alwaysPresent()) {
writer.end().appendln('}');
}
}
writer.newline().appendln("return 0;");
}
writer.end().appendln("}").newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class CoreOverridesFormatter method appendJsonCompact.
private void appendJsonCompact(JMessage<?> message) {
if (!message.jsonCompactible()) {
return;
}
writer.appendln("@Override").appendln("public boolean jsonCompact() {").begin();
boolean hasCheck = false;
boolean hasMissingVar = false;
for (JField field : message.numericalOrderFields()) {
if (!field.alwaysPresent()) {
if (!hasMissingVar) {
writer.appendln("boolean missing = false;");
}
hasMissingVar = true;
hasCheck = true;
writer.formatln("if (%s()) {", field.presence()).appendln(" if (missing) return false;").appendln("} else {").appendln(" missing = true;").appendln('}');
} else if (hasCheck) {
writer.appendln("if (missing) return false;");
hasCheck = false;
}
}
writer.appendln("return true;").end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class CoreOverridesFormatter method appendGetter.
private void appendGetter(JMessage<?> message) {
writer.appendln("@Override").appendln("@SuppressWarnings(\"unchecked\")").appendln("public <T> T get(int key) {").begin().appendln("switch(key) {").begin();
for (JField field : message.numericalOrderFields()) {
if (field.isVoid()) {
// Void fields have no value.
writer.formatln("case %d: return %s() ? (T) Boolean.TRUE : null;", field.id(), field.presence());
} else if (field.isPrimitiveJavaValue()) {
if (field.alwaysPresent()) {
writer.formatln("case %d: return (T) (%s) %s;", field.id(), field.instanceType(), field.member());
} else {
writer.formatln("case %d: return (T) %s;", field.id(), field.member());
}
} else {
writer.formatln("case %d: return (T) %s;", field.id(), field.member());
}
}
writer.appendln("default: return null;").end().appendln('}').end().appendln('}').newline();
}
Aggregations