Skip to content

Commit

Permalink
Move resp of copy to manager, Fix #197
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Apr 28, 2016
1 parent 8395c93 commit fa86204
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/speedment/SpeedmentVersion.java
Expand Up @@ -32,7 +32,7 @@ public final class SpeedmentVersion {


private static final String IMPLEMENTATION_TITLE = "Speedment", private static final String IMPLEMENTATION_TITLE = "Speedment",
IMPLEMENTATION_VENDOR = "Speedment Inc.", // "Speedment, Inc." difficult to enter into POM because of ',' IMPLEMENTATION_VENDOR = "Speedment Inc.", // "Speedment, Inc." difficult to enter into POM because of ','
IMPLEMENTATION_VERSION = "2.3.2", IMPLEMENTATION_VERSION = "2.3.3",
SPECIFICATION_VERSION = "2.3"; SPECIFICATION_VERSION = "2.3";


private static final boolean PRODUCTION_MODE = true; private static final boolean PRODUCTION_MODE = true;
Expand Down
Expand Up @@ -81,6 +81,11 @@ public ENTITY remove(Consumer<MetaResult<ENTITY>> consumer) throws SpeedmentExce
return manager_().remove(selfAsEntity(), consumer); return manager_().remove(selfAsEntity(), consumer);
} }


@Override
public ENTITY copy() {
return manager_().newCopyOf(selfAsEntity());
}

protected abstract Class<ENTITY> entityClass(); protected abstract Class<ENTITY> entityClass();


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -67,7 +67,7 @@ public GeneratedEntityImplTranslator(Speedment speedment, Generator gen, Table t
@Override @Override
protected Class makeCodeGenModel(File file) { protected Class makeCodeGenModel(File file) {
requireNonNull(file); requireNonNull(file);

file.add(Import.of(Type.of(Speedment.class))); file.add(Import.of(Type.of(Speedment.class)));


final Map<Table, List<String>> fkStreamers = new HashMap<>(); final Map<Table, List<String>> fkStreamers = new HashMap<>();
Expand Down Expand Up @@ -114,9 +114,7 @@ protected Class makeCodeGenModel(File file) {
+ EntityTranslatorSupport.pluralis(fu.getTable(), getNamer()) + EntityTranslatorSupport.pluralis(fu.getTable(), getNamer())
+ "By" + getSupport().typeName(fu.getColumn()); + "By" + getSupport().typeName(fu.getColumn());
fkStreamers.computeIfAbsent(fu.getTable(), t -> new ArrayList<>()).add(methodName); fkStreamers.computeIfAbsent(fu.getTable(), t -> new ArrayList<>()).add(methodName);




final Type returnType = Type.of(Stream.class).add(Generic.of().add(fu.getEmt().getSupport().entityType())); final Type returnType = Type.of(Stream.class).add(Generic.of().add(fu.getEmt().getSupport().entityType()));
final Method method = Method.of(methodName, returnType).public_().add(OVERRIDE) final Method method = Method.of(methodName, returnType).public_().add(OVERRIDE)
.add("return " + MANAGER_OF_METHOD + "(" + getSupport().typeName(fu.getTable()) + ".class)") .add("return " + MANAGER_OF_METHOD + "(" + getSupport().typeName(fu.getTable()) + ".class)")
Expand All @@ -128,7 +126,7 @@ protected Class makeCodeGenModel(File file) {
*/ */
.forEveryForeignKey((clazz, fk) -> { .forEveryForeignKey((clazz, fk) -> {
final FkHolder fu = new FkHolder(getSpeedment(), getCodeGenerator(), fk); final FkHolder fu = new FkHolder(getSpeedment(), getCodeGenerator(), fk);

final Type returnType; final Type returnType;
if (fu.getColumn().isNullable()) { if (fu.getColumn().isNullable()) {
file.add(Import.of(OPTIONAL)); file.add(Import.of(OPTIONAL));
Expand Down Expand Up @@ -160,19 +158,18 @@ protected Class makeCodeGenModel(File file) {
} }
clazz.add(method); clazz.add(method);
}) })

/** /**
* Class details * Class details
*/ */
.forEveryTable(Phase.POST_MAKE, (clazz, table) -> { .forEveryTable(Phase.POST_MAKE, (clazz, table) -> {
clazz.add(copy(file)) clazz
.add(toString(file)) .add(toString(file))
.add(equalsMethod()) .add(equalsMethod())
.add(hashCodeMethod()) .add(hashCodeMethod())
.add(Method.of("entityClass", Type.of(java.lang.Class.class).add(Generic.of().add(getSupport().entityType()))).public_().add(OVERRIDE) .add(Method.of("entityClass", Type.of(java.lang.Class.class).add(Generic.of().add(getSupport().entityType()))).public_().add(OVERRIDE)
.add("return " + getSupport().entityName() + ".class;") .add("return " + getSupport().entityName() + ".class;")
); );

/** /**
* Create aggregate streaming functions, if any * Create aggregate streaming functions, if any
*/ */
Expand All @@ -195,7 +192,6 @@ protected Class makeCodeGenModel(File file) {
} }
}); });
}) })

.build() .build()
.public_() .public_()
.abstract_() .abstract_()
Expand All @@ -205,42 +201,6 @@ protected Class makeCodeGenModel(File file) {


} }


private Method copy(File file) {
file.add(Import.of(getSupport().entityImplType()));

final JavaLanguageNamer namer = getSpeedment().getCodeGenerationComponent().javaLanguageNamer();
final String entityName = namer.javaVariableName(getSupport().entityName());
final Method result = Method.of("copy", getSupport().entityType()).public_().add(OVERRIDE)
.add(
"final " + getSupport().entityName() + " " + entityName + " = new " + getSupport().entityImplName() + "() {", indent(
"@Override",
"protected final " + Speedment.class.getSimpleName() + " speedment() {", indent(
"return " + getSupport().generatedEntityImplName() + ".this.speedment();"
), "}"
), "};",
""
);

columns().forEachOrdered(c -> {
if (c.isNullable()) {
result.add(
GETTER_METHOD_PREFIX + getSupport().typeName(c) +
"().ifPresent(" + entityName + "::" +
SETTER_METHOD_PREFIX + getSupport().typeName(c) +
");"
);
} else {
result.add(
entityName + "." + SETTER_METHOD_PREFIX +
getSupport().typeName(c) + "(get" +
getSupport().typeName(c) + "());"
);
}
});

return result.add("", "return " + entityName + ";");
}

protected Method toString(File file) { protected Method toString(File file) {
file.add(Import.of(Type.of(StringJoiner.class))); file.add(Import.of(Type.of(StringJoiner.class)));
file.add(Import.of(Type.of(Objects.class))); file.add(Import.of(Type.of(Objects.class)));
Expand Down

0 comments on commit fa86204

Please sign in to comment.