Skip to content

Commit

Permalink
EntityInterface refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyknic committed Mar 9, 2015
1 parent c7b98e9 commit 1e11816
Show file tree
Hide file tree
Showing 16 changed files with 729 additions and 62 deletions.
Expand Up @@ -46,10 +46,10 @@ public abstract class DefaultJavaClassTranslator<T extends ConfigEntity> impleme


public static final String GETTER_METHOD_PREFIX = "get"; public static final String GETTER_METHOD_PREFIX = "get";
public static final String SETTER_METHOD_PREFIX = "set"; public static final String SETTER_METHOD_PREFIX = "set";
public static final String BUILDER_METHOD_PREFIX = "with"; public static final String BUILDER_METHOD_PREFIX = SETTER_METHOD_PREFIX;


public static final String GENERATED_JAVADOC_MESSAGE = "\n<p>\nThis Class or Interface has been automatically generated by Speedment.\n" public static final String GENERATED_JAVADOC_MESSAGE = "\n<p>\nThis Class or Interface has been automatically generated by Speedment.\n"
+ "Any changes made to this Class or Interface will be overwritten."; + "Any changes made to this Class or Interface will be overwritten.";


private final T configEntity; private final T configEntity;


Expand Down
Expand Up @@ -23,6 +23,8 @@
import com.speedment.orm.code.model.Translator; import com.speedment.orm.code.model.Translator;
import com.speedment.orm.code.model.java.entity.EntityBeanImplTranslator; import com.speedment.orm.code.model.java.entity.EntityBeanImplTranslator;
import com.speedment.orm.code.model.java.entity.EntityBuilderImplTranslator; import com.speedment.orm.code.model.java.entity.EntityBuilderImplTranslator;
import com.speedment.orm.code.model.java.entity.EntityConfigTranslator;
import com.speedment.orm.code.model.java.entity.EntityImplTranslator_OLD;
import com.speedment.orm.code.model.java.entity.EntityImplTranslator; import com.speedment.orm.code.model.java.entity.EntityImplTranslator;
import com.speedment.orm.code.model.java.entity.EntityTranslator; import com.speedment.orm.code.model.java.entity.EntityTranslator;
import com.speedment.orm.config.model.Project; import com.speedment.orm.config.model.Project;
Expand Down Expand Up @@ -57,8 +59,10 @@ public void accept(Project project) {
project.traversalOf(Table.class).forEach(table -> { project.traversalOf(Table.class).forEach(table -> {
translators.add(new EntityTranslator(cg, table)); translators.add(new EntityTranslator(cg, table));
translators.add(new EntityImplTranslator(cg, table)); translators.add(new EntityImplTranslator(cg, table));
translators.add(new EntityBeanImplTranslator(cg, table)); translators.add(new EntityConfigTranslator(cg, table));
translators.add(new EntityBuilderImplTranslator(cg, table)); // translators.add(new EntityImplTranslator(cg, table));
// translators.add(new EntityBeanImplTranslator(cg, table));
// translators.add(new EntityBuilderImplTranslator(cg, table));
}); });


Formatting.tab(" "); Formatting.tab(" ");
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/ */
package com.speedment.orm.code.model.java.entity; package com.speedment.orm.code.model.java.entity;


import com.speedment.codegen.Formatting;
import com.speedment.codegen.base.CodeGenerator; import com.speedment.codegen.base.CodeGenerator;
import com.speedment.codegen.lang.controller.AutoImports; import com.speedment.codegen.lang.controller.AutoImports;
import com.speedment.codegen.lang.controller.AutoJavadoc; import com.speedment.codegen.lang.controller.AutoJavadoc;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class ClassType {


private ClassType(String typeName, String implTypeName) { private ClassType(String typeName, String implTypeName) {
this.type = Type.of(fullyQualifiedTypeName() + typeName); this.type = Type.of(fullyQualifiedTypeName() + typeName);
this.implType = Type.of(fullyQualifiedTypeName("impl") + implTypeName); this.implType = Type.of(fullyQualifiedTypeName("impl") + typeName + implTypeName);
} }


private final Type type; private final Type type;
Expand All @@ -57,11 +58,20 @@ public Type getImplType() {
return implType; return implType;
} }


public String getName() {
return Formatting.shortName(type.getName());
}

public String getImplName() {
return Formatting.shortName(implType.getName());
}
} }


public final ClassType INTERFACE = new ClassType("", "Impl"); public final ClassType INTERFACE = new ClassType("", "Impl");
public final ClassType BEAN = new ClassType(".Bean", "BeanImpl"); public final ClassType BEAN = new ClassType("Bean", "Impl");
public final ClassType BUILDER = new ClassType(".Builder", "BuilderImpl"); public final ClassType BUILDER = new ClassType(".Builder", "Impl");
public final ClassType PERSISTER = new ClassType(".Persister", "Impl");
public final ClassType CONFIG = new ClassType("Config", "Impl");


public BaseEntityTranslator(CodeGenerator cg, Table configEntity) { public BaseEntityTranslator(CodeGenerator cg, Table configEntity) {
super(configEntity); super(configEntity);
Expand Down
@@ -0,0 +1,78 @@
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.orm.code.model.java.entity;

import com.speedment.codegen.Formatting;
import static com.speedment.codegen.Formatting.block;
import com.speedment.codegen.base.CodeGenerator;
import com.speedment.codegen.lang.models.Enum;
import com.speedment.codegen.lang.models.EnumConstant;
import com.speedment.codegen.lang.models.Field;
import com.speedment.codegen.lang.models.File;
import com.speedment.codegen.lang.models.Import;
import com.speedment.codegen.lang.models.Method;
import com.speedment.codegen.lang.models.Type;
import static com.speedment.codegen.lang.models.constants.DefaultType.BOOLEAN_PRIMITIVE;
import static com.speedment.codegen.lang.models.constants.DefaultType.VOID;
import com.speedment.codegen.lang.models.values.BooleanValue;
import com.speedment.orm.config.model.Table;
import com.speedment.orm.platform.Speedment;
import java.util.function.Supplier;

/**
*
* @author pemi
*/
public class EntityConfigTranslator extends BaseEntityTranslator<Enum> {

public EntityConfigTranslator(CodeGenerator cg, Table configEntity) {
super(cg, configEntity);
}

@Override
protected Enum make(File file) {
final Supplier<Field> speedment = () -> Field.of("speedment", Type.of(Speedment.class));

return Enum.of(Formatting.shortName(CONFIG.getType().getName()))
.add(EnumConstant.of("INSTANCE"))
.add(Field.of("running", BOOLEAN_PRIMITIVE).set(new BooleanValue(false)))
.add(speedment.get())
.add(Method.of("setSpeedment", VOID)
.public_()
.add(speedment.get())
.add("if (running) " + block(
"throw new IllegalStateException(\"Can't change Speedment instance while running!\");"
)
).add("INSTANCE.speedment = speedment;")
)
.add(Method.of("setRunning", VOID)
.public_()
.add("running = true;"))
.call(e -> file.add(Import.of(Type.of(IllegalStateException.class))));
}

@Override
protected String getJavadocRepresentText() {
return "A configuration";
}

@Override
protected String getFileName() {
return Formatting.shortName(CONFIG.getType().getName());
}

}
Expand Up @@ -18,10 +18,12 @@


import com.speedment.codegen.Formatting; import com.speedment.codegen.Formatting;
import com.speedment.codegen.base.CodeGenerator; import com.speedment.codegen.base.CodeGenerator;
import com.speedment.codegen.lang.models.Method;
import com.speedment.codegen.lang.models.Type; import com.speedment.codegen.lang.models.Type;
import com.speedment.codegen.lang.models.Class; import com.speedment.codegen.lang.models.Class;
import com.speedment.codegen.lang.models.File; import com.speedment.codegen.lang.models.File;
import com.speedment.codegen.lang.models.Method; import com.speedment.codegen.lang.models.Generic;
import com.speedment.codegen.lang.models.Import;
import static com.speedment.codegen.lang.models.constants.DefaultAnnotationUsage.OVERRIDE; import static com.speedment.codegen.lang.models.constants.DefaultAnnotationUsage.OVERRIDE;
import com.speedment.orm.config.model.Table; import com.speedment.orm.config.model.Table;


Expand All @@ -37,31 +39,51 @@ public EntityImplTranslator(CodeGenerator cg, Table configEntity) {


@Override @Override
protected Class make(File file) { protected Class make(File file) {
return new ClassBuilder(INTERFACE.getImplType().getName())
return new ClassBuilder(INTERFACE.getImplName())
.addColumnConsumer((cl, c) -> { .addColumnConsumer((cl, c) -> {
cl cl
.add(fieldFor(c).private_().final_()) .add(fieldFor(c).private_())
.add(Method.of(GETTER_METHOD_PREFIX + typeName(c), Type.of(c.getMapping())) .add(Method.of(GETTER_METHOD_PREFIX + typeName(c), Type.of(c.getMapping()))
.public_()
.add(OVERRIDE) .add(OVERRIDE)
.add("return " + variableName(c) + ";"))
.add(Method.of(BUILDER_METHOD_PREFIX + typeName(c), INTERFACE.getImplType())
.public_() .public_()
.add("return " + variableName(c) + ";")); .add(OVERRIDE)
.add(fieldFor(c))
.add("this." + variableName(c) + " = " + variableName(c) + ";")
.add("return this;"));
}) })
.build() .build()
.public_() .public_()
.add(INTERFACE.getType()) .add(PERSISTER.getType())
//.add(emptyConstructor()) .add(BUILDER.getType())
.add(copyConstructor(INTERFACE.getType(), CopyConstructorMode.FIELD)); .add(emptyConstructor())
.add(copyConstructor(INTERFACE.getType(), CopyConstructorMode.BUILDER))
.add(Method.of("build", INTERFACE.getType())
.add(OVERRIDE)
.public_()
.call(m -> file.add(Import.of(INTERFACE.getImplType())))
.add("return new " + INTERFACE.getImplName() + "(this);"))
.add(Method.of("persist", INTERFACE.getType())
.add(OVERRIDE)
.public_()
.add("return this;"))
.add(Method.of("remove", INTERFACE.getType())
.add(OVERRIDE)
.public_()
.add("return this;"));
} }


@Override @Override

protected String getJavadocRepresentText() { protected String getJavadocRepresentText() {
return "An immutable implementation"; return "An implementation ";
} }


@Override @Override
protected String getFileName() { protected String getFileName() {
return Formatting.shortName(INTERFACE.getImplType().getName()); return INTERFACE.getImplName();
} }


@Override @Override
Expand Down
@@ -0,0 +1,72 @@
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.orm.code.model.java.entity;

import com.speedment.codegen.Formatting;
import com.speedment.codegen.base.CodeGenerator;
import com.speedment.codegen.lang.models.Type;
import com.speedment.codegen.lang.models.Class;
import com.speedment.codegen.lang.models.File;
import com.speedment.codegen.lang.models.Method;
import static com.speedment.codegen.lang.models.constants.DefaultAnnotationUsage.OVERRIDE;
import com.speedment.orm.config.model.Table;

/**
*
* @author pemi
*/
public class EntityImplTranslator_OLD extends BaseEntityTranslator<Class> {

public EntityImplTranslator_OLD(CodeGenerator cg, Table configEntity) {
super(cg, configEntity);
}

@Override
protected Class make(File file) {
return new ClassBuilder(INTERFACE.getImplType().getName())
.addColumnConsumer((cl, c) -> {
cl
.add(fieldFor(c).private_().final_())
.add(Method.of(GETTER_METHOD_PREFIX + typeName(c), Type.of(c.getMapping()))
.add(OVERRIDE)
.public_()
.add("return " + variableName(c) + ";"));
})
.build()
.public_()
.add(INTERFACE.getType())
//.add(emptyConstructor())
.add(copyConstructor(INTERFACE.getType(), CopyConstructorMode.FIELD));
}

@Override

protected String getJavadocRepresentText() {
return "An immutable implementation";
}

@Override
protected String getFileName() {
return Formatting.shortName(INTERFACE.getImplType().getName());
}

@Override
protected boolean isInImplPackage() {
return true;
}

}

0 comments on commit 1e11816

Please sign in to comment.