/** * Adds an {@link Override}-annotation to this model. * * @return this instance */ default Method override() { return add(DefaultAnnotationUsage.OVERRIDE); } }
private Method generateApplyResultSet( TranslatorSupport<Table> support, File file, Supplier<Stream<? extends Column>> columnsSupplier) { return Method.of("apply", support.entityType()) .protected_() .add(SQLException.class) .add(Field.of("resultSet", ResultSet.class)) .add(Field.of(OFFSET_PARAMETER_NAME, int.class)) .add(generateApplyResultSetBody( this::readFromResultSet, support, file, columnsSupplier )); }
/** * 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;") ); }
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; }
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; }
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)); }
.add(RETURN.setText("True if the objects are equal.")) ).public_() .add(OVERRIDE) .add(Field.of("other", Object.class)) .add("return Optional.ofNullable(other)") .call(m -> { if (HasSupertype.class.isAssignableFrom(model.getClass())) { final Optional<Type> supertype = ((HasSupertype<?>) model).getSupertype(); if (supertype.isPresent()) { m.add(tab() + ".filter(o -> super.equals(o))"); .add(tab() + ".filter(o -> getClass().equals(o.getClass()))") .add(tab() + ".map(o -> (" + model.getName() + ") o)") .add(tab() + model.getFields().stream().map(this::compare).collect( Collectors.joining(nl() + tab()) )) .add(tab() + ".isPresent();") );
final Method getMetadata = Method.of("getMetadata", DefaultType.optional(String.class)) .protected_() .add(OVERRIDE); int lineCnt = 0; for (String line : seg) { subMethod.add( indent("\"" + line.replace("\\", "\\\\") .replace("\"", "\\\"") }).forEach(subMethod -> subMethod.add( ").forEachOrdered(" + STRING_BUILDER_NAME + "::append);" )); initializer.add("final StringBuilder " + STRING_BUILDER_NAME + " = new StringBuilder();"); subInitializers.stream().forEachOrdered(si -> { initializer.add(si.getName() + "(" + STRING_BUILDER_NAME + ");"); }); initializer.add("return " + STRING_BUILDER_NAME + ".toString();"); getMetadata.add("return Optional.of(METADATA);");
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; }
.add(Field.of(DATABASE_NAME, String.class)) .add("if (" + DATABASE_NAME + " == null) return null;") .add("switch (" + DATABASE_NAME + ") {"); .add(Field.of(DATABASE_ORDINAL, Integer.class)) .add("if (" + DATABASE_ORDINAL + " == null) return null;") .add("switch (" + DATABASE_ORDINAL + ") {"); fromDatabase.add(indent( "case \"" + constant + "\" : return " + namer.javaStaticFieldName(constant) + ";" )); fromDatabaseOrdinal.add(indent( "case " + ordinal + " : return " + namer.javaStaticFieldName(constant) + ";" .add(indent("default : throw new UnsupportedOperationException(")) .add(indent("\"Unknown enum constant '\" + " + DATABASE_NAME + " + \"'.\"", 2)) .add(indent(");")) .add("}"); .add(indent("default : throw new UnsupportedOperationException(")) .add(indent("\"Unknown enum ordinal '\" + " + DATABASE_ORDINAL + " + \"'.\"", 2)) .add(indent(");")) .add("}"); .public_().add("return " + DATABASE_NAME + ";") );
private Method generateCreateEntity(File file) { final Type entityImplType = getSupport().entityImplType(); file.add(Import.of(entityImplType)); return Method.of("createEntity", entityImplType).protected_() .add("return new " + getSupport().entityImplName() + "();"); }
.add(JavadocTag.of("return", "a reference to this object")) ).public_() .add(param) .add("this." + f.getName() + ".add(" + param.getName() + ");") .add("return this;"); set.add(Field.of(f.getName(), paramType.getActualTypeArguments()[0])) .add("this." + f.getName() + " = Optional.of(" + f.getName() + ");") .add("return this;"); } else { set.add(Field.of(f.getName(), f.getType())) .add("this." + f.getName() + " = " + f.getName() + ";") .add("return this;"); .add(JavadocTag.of("return", "the " + f.getName())) ).public_() .add("return this." + f.getName() + ";");
.forEveryTable((clazz, table) -> { final Method createHelpers = Method.of(CREATE_HELPERS_METHOD_NAME, void.class) .add(withExecuteBefore(file)) .add(Field.of("projectComponent", ProjectComponent.class)) .add("final Project project = projectComponent.getProject();"); Method.of("identifier", tableIdentifierType) .public_() .add(OVERRIDE) .add("return tableIdentifier;") .add(OVERRIDE) .add("return entityMapper(0);") .add(OVERRIDE) .add(Field.of(OFFSET_PARAMETER_NAME, int.class)) .add("return rs -> apply(rs, offset);") .add(tmsName + " = " + SqlTypeMapperHelper.class.getSimpleName() + ".create(project, " + getSupport().entityName() + "." + getSupport().namer().javaStaticFieldName(col.getJavaName())
.add(fieldFor(col).private_()) .add(Method.of(GETTER_METHOD_PREFIX + getSupport().typeName(col), retType) .public_().add(OVERRIDE) .add("return " + getter + ";")); }) .add(Method.of(SETTER_METHOD_PREFIX + getSupport().typeName(col), getSupport().entityType()) .public_() .add(OVERRIDE) .add(fieldFor(col)) .add("this." + getSupport().variableName(col) + " = " + getSupport().variableName(col) + ";") .add("return this;")) : fuSupport.entityType()) .public_() .add(DefaultAnnotationUsage.OVERRIDE) .add(Field.of("foreignManager", SimpleParameterizedType.create( Manager.class, fuSupport.entityType() ))) .add( col.isNullable() ? "if (" + GETTER_METHOD_PREFIX + getSupport().namer().javaTypeName(col.getJavaName()) + "()" + isPresentName + ") " + block(
.add(OVERRIDE) .add(returnString(getSupport().dbmsOrThrow().getId())) .add(OVERRIDE) .add(returnString(getSupport().schemaOrThrow().getId())) .add(OVERRIDE) .add(returnString(getSupport().tableOrThrow().getId())) .add(OVERRIDE) .add("return this.columnId;") .add(OVERRIDE) .add("return this.tableIdentifier;") ); .add(Field.of(getSupport().variableName(col), typeMappers.get(col).getJavaType(col))) .set(Javadoc.of( "Sets the " + getSupport().variableName(col) .add(DefaultJavadocTag.RETURN.setText("the foreign entity referenced")) .add(Field.of("foreignManager", SimpleParameterizedType.create( Manager.class, fuSupport.entityType() )))
/** * Adds an {@link Override}-annotation to this model. * * @return this instance */ default Method override() { return add(DefaultAnnotationUsage.OVERRIDE); } }