private Method hashCodeMethod(File file) { final Method method = Method.of("hashCode", int.class) .public_() .add(OVERRIDE) .add("int hash = 7;"); columns().forEachOrdered(c -> { final StringBuilder str = new StringBuilder(); str.append("hash = 31 * hash + "); final Type type = typeMappers.get(c).getJavaType(c); if (!usesOptional(c) && DefaultType.isPrimitive(type)) { str.append(DefaultType.wrapperFor(type).getSimpleName()); } else { str.append("Objects"); } str.append(".hashCode("); if (usesOptional(c)) { file.add(Import.of(OptionalUtil.class)); str.append("OptionalUtil.unwrap("); } str.append("get").append(getSupport().typeName(c)).append("()"); if (usesOptional(c)) str.append(')'); str.append(");"); method.add(str.toString()); }); method.add("return hash;"); return method; }
/** * The <code>hashCode()</code>-part of the <code>accept</code> method. * * @param model the model */ protected void acceptHashcode(T model) { requireNonNull(model); model.add(Method.of(HASHCODE, int.class) .set( Javadoc.of( "Generates a hashCode for this object. If any field is " + "changed to another value, the hashCode may be different. " + "Two objects with the same values are guaranteed to have " + "the same hashCode. Two objects with the same hashCode are " + "not guaranteed to have the same hashCode." ) .add(RETURN.setText("The hash code.")) ).public_() .add(OVERRIDE) .add("int hash = 7;") .add(model.getFields().stream() .map(this::hash) .collect(Collectors.joining(nl())) ) .add("return hash;") ); }
.public_().add(OVERRIDE) .add(Field.of("injector", Injector.class)) .add("return injector.getOrThrow(" + getSupport().typeName(getSupport().projectOrThrow()) + "Application.class);");
public static Method generateFields( TranslatorSupport<Table> support, File file, String methodName, Supplier<Stream<? extends Column>> columnsSupplier) { return Method.of(methodName, DefaultType.stream( SimpleParameterizedType.create( com.speedment.runtime.field.Field.class, support.entityType() ) ) ) .public_().add(OVERRIDE) .add(generateFieldsBody(support, file, columnsSupplier)); }
).public_() .add(OVERRIDE) .add(Field.of("other", Object.class))
.public_().static_() .add(Field.of(DATABASE_NAME, String.class)) .add("if (" + DATABASE_NAME + " == null) return null;") .public_().static_() .add(Field.of(DATABASE_ORDINAL, Integer.class)) .add("if (" + DATABASE_ORDINAL + " == null) return null;") .public_().add("return " + DATABASE_NAME + ";") ); .public_().add("return " + DATABASE_ORDINAL + ";") ); .add(enumType) .add(Method.of("getLabel", String.class) .public_().add(OVERRIDE) .add("return \"" + shortName(dbType.getTypeName()) + " to " + enumShortName + " Mapper\";") .public_().add(OVERRIDE) .add("return " + TypeMapper.Ordering.class.getSimpleName() + "." + TypeMapper.Ordering.RETAIN.name() + ";") .public_().add(OVERRIDE) .add(Field.of("column", Column.class)) .add("return " + TypeMapper.Category.class.getSimpleName() + .public_().add(OVERRIDE)
.add(JavadocTag.of("param", param.getName(), "the new value")) .add(JavadocTag.of("return", "a reference to this object")) ).public_() .add(param) .add("this." + f.getName() + ".add(" + param.getName() + ");") .add(JavadocTag.of("param", f.getName(), "the new value")) .add(JavadocTag.of("return", "a reference to this object")) ).public_(); .setText("Gets the " + f.getName() + " of this " + shortName(model.getName()) + ".") .add(JavadocTag.of("return", "the " + f.getName())) ).public_() .add("return this." + f.getName() + ";");
protected Method toStringMethod(File file) { file.add(Import.of(StringJoiner.class)); file.add(Import.of(Objects.class)); final Method m = Method.of("toString", String.class) .public_() .add(OVERRIDE) .add("final StringJoiner sj = new StringJoiner(\", \", \"{ \", \" }\");"); columns().forEachOrdered(col -> { final String getter; if (usesOptional(col)) { file.add(Import.of(OptionalUtil.class)); getter = "OptionalUtil.unwrap(get" + getSupport().typeName(col) + "())"; } else { getter = "get" + getSupport().typeName(col) + "()"; } m.add("sj.add(\"" + getSupport().variableName(col) + " = \" \t+ Objects.toString(" + getter + "));"); }); m.add("return \"" + getSupport().entityImplName() + " \" + sj.toString();"); return m; }
private Method equalsMethod() { final String thatName = "that"; final String thatCastedName = thatName + getSupport().entityName(); final Method method = Method.of("equals", boolean.class) .public_() .add(OVERRIDE) .add(Field.of(thatName, Object.class)) .add("if (this == that) { return true; }") .add("if (!(" + thatName + " instanceof " + getSupport().entityName() + ")) { return false; }") .add("final " + getSupport().entityName() + " " + thatCastedName + " = (" + getSupport().entityName() + ")" + thatName + ";"); columns().forEachOrdered(c -> { final String getter = "get" + getSupport().typeName(c); final Type type = typeMappers.get(c).getJavaType(c); if (usesOptional(c) || !DefaultType.isPrimitive(type)) { method.add("if (!Objects.equals(this." + getter + "(), " + thatCastedName + "." + getter + "())) { return false; }"); } else { method.add("if (this." + getter + "() != " + thatCastedName + "." + getter + "()) { return false; }"); } }); method.add("return true;"); return method; }
.add(fieldFor(col).private_()) .add(Method.of(GETTER_METHOD_PREFIX + getSupport().typeName(col), retType) .public_().add(OVERRIDE) .add("return " + getter + ";")); }) clazz .add(Method.of(SETTER_METHOD_PREFIX + getSupport().typeName(col), getSupport().entityType()) .public_() .add(OVERRIDE) .add(fieldFor(col)) ? DefaultType.optional(fuSupport.entityType()) : fuSupport.entityType()) .public_() .add(DefaultAnnotationUsage.OVERRIDE) .add(Field.of("foreignManager", SimpleParameterizedType.create(
) + ");") .add(Method.of("getDbmsId", String.class).public_() .add(OVERRIDE) .add(returnString(getSupport().dbmsOrThrow().getId())) .add(Method.of("getSchemaId", String.class).public_() .add(OVERRIDE) .add(returnString(getSupport().schemaOrThrow().getId())) .add(Method.of("getTableId", String.class).public_() .add(OVERRIDE) .add(returnString(getSupport().tableOrThrow().getId())) .add(Method.of("getColumnId", String.class).public_() .add(OVERRIDE) .add("return this.columnId;") .add(Method.of("asTableIdentifier", tableIdentifierType).public_() .add(OVERRIDE) .add("return this.tableIdentifier;")
private Method hashCodeMethod(File file) { final Method method = Method.of("hashCode", int.class) .public_() .add(OVERRIDE) .add("int hash = 7;"); columns().forEachOrdered(c -> { final StringBuilder str = new StringBuilder(); str.append("hash = 31 * hash + "); final Type type = typeMappers.get(c).getJavaType(c); if (!usesOptional(c) && DefaultType.isPrimitive(type)) { str.append(DefaultType.wrapperFor(type).getSimpleName()); } else { str.append("Objects"); } str.append(".hashCode("); if (usesOptional(c)) { file.add(Import.of(OptionalUtil.class)); str.append("OptionalUtil.unwrap("); } str.append("get").append(getSupport().typeName(c)).append("()"); if (usesOptional(c)) str.append(')'); str.append(");"); method.add(str.toString()); }); method.add("return hash;"); return method; }
/** * The <code>hashCode()</code>-part of the <code>accept</code> method. * * @param model the model */ protected void acceptHashcode(T model) { requireNonNull(model); model.add(Method.of(HASHCODE, int.class) .set( Javadoc.of( "Generates a hashCode for this object. If any field is " + "changed to another value, the hashCode may be different. " + "Two objects with the same values are guaranteed to have " + "the same hashCode. Two objects with the same hashCode are " + "not guaranteed to have the same hashCode." ) .add(RETURN.setText("The hash code.")) ).public_() .add(OVERRIDE) .add("int hash = 7;") .add(model.getFields().stream() .map(this::hash) .collect(Collectors.joining(nl())) ) .add("return hash;") ); }
/** * The <code>hashCode()</code>-part of the <code>accept</code> method. * * @param model the model */ protected void acceptHashcode(T model) { requireNonNull(model); model.add(Method.of(HASHCODE, int.class) .set( Javadoc.of( "Generates a hashCode for this object. If any field is " + "changed to another value, the hashCode may be different. " + "Two objects with the same values are guaranteed to have " + "the same hashCode. Two objects with the same hashCode are " + "not guaranteed to have the same hashCode." ) .add(RETURN.setText("The hash code.")) ).public_() .add(OVERRIDE) .add("int hash = 7;") .add(model.getFields().stream() .map(this::hash) .collect(Collectors.joining(nl())) ) .add("return hash;") ); }
public static Method generateFields( TranslatorSupport<Table> support, File file, String methodName, Supplier<Stream<? extends Column>> columnsSupplier) { return Method.of(methodName, DefaultType.stream( SimpleParameterizedType.create( com.speedment.runtime.field.Field.class, support.entityType() ) ) ) .public_().add(OVERRIDE) .add(generateFieldsBody(support, file, columnsSupplier)); }
).public_() .add(OVERRIDE) .add(Field.of("other", Object.class))
protected Method toStringMethod(File file) { file.add(Import.of(StringJoiner.class)); file.add(Import.of(Objects.class)); final Method m = Method.of("toString", String.class) .public_() .add(OVERRIDE) .add("final StringJoiner sj = new StringJoiner(\", \", \"{ \", \" }\");"); columns().forEachOrdered(col -> { final String getter; if (usesOptional(col)) { file.add(Import.of(OptionalUtil.class)); getter = "OptionalUtil.unwrap(get" + getSupport().typeName(col) + "())"; } else { getter = "get" + getSupport().typeName(col) + "()"; } m.add("sj.add(\"" + getSupport().variableName(col) + " = \" \t+ Objects.toString(" + getter + "));"); }); m.add("return \"" + getSupport().entityImplName() + " \" + sj.toString();"); return m; }
private Method equalsMethod() { final String thatName = "that"; final String thatCastedName = thatName + getSupport().entityName(); final Method method = Method.of("equals", boolean.class) .public_() .add(OVERRIDE) .add(Field.of(thatName, Object.class)) .add("if (this == that) { return true; }") .add("if (!(" + thatName + " instanceof " + getSupport().entityName() + ")) { return false; }") .add("final " + getSupport().entityName() + " " + thatCastedName + " = (" + getSupport().entityName() + ")" + thatName + ";"); columns().forEachOrdered(c -> { final String getter = "get" + getSupport().typeName(c); final Type type = typeMappers.get(c).getJavaType(c); if (usesOptional(c) || !DefaultType.isPrimitive(type)) { method.add("if (!Objects.equals(this." + getter + "(), " + thatCastedName + "." + getter + "())) { return false; }"); } else { method.add("if (this." + getter + "() != " + thatCastedName + "." + getter + "()) { return false; }"); } }); method.add("return true;"); return method; }