Skip to content

Commit

Permalink
Make Translator generic towards created entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Feb 2, 2016
1 parent fb070a9 commit cfa8f10
Show file tree
Hide file tree
Showing 20 changed files with 280 additions and 290 deletions.
23 changes: 14 additions & 9 deletions src/main/java/com/speedment/code/StandardTranslatorKey.java
Expand Up @@ -20,6 +20,9 @@
import com.speedment.config.db.Project; import com.speedment.config.db.Project;
import com.speedment.config.db.Table; import com.speedment.config.db.Table;
import com.speedment.internal.core.code.TranslatorKeyImpl; import com.speedment.internal.core.code.TranslatorKeyImpl;
import com.speedment.internal.codegen.lang.models.Class;
import com.speedment.internal.codegen.lang.models.ClassOrInterface;
import com.speedment.internal.codegen.lang.models.Interface;
import java.util.stream.Stream; import java.util.stream.Stream;


/** /**
Expand All @@ -30,20 +33,22 @@
@Api(version = "2.3") @Api(version = "2.3")
public final class StandardTranslatorKey { public final class StandardTranslatorKey {


public final static TranslatorKey<Project> public final static TranslatorKey<Project, Class>
SPEEDMENT_APPLICATION = new TranslatorKeyImpl<>("SpeedmentApplication"), SPEEDMENT_APPLICATION = new TranslatorKeyImpl<>("SpeedmentApplication", Class.class),
SPEEDMENT_APPLICATION_METADATA = new TranslatorKeyImpl<>("SpeedmentApplicationMetadata"); SPEEDMENT_APPLICATION_METADATA = new TranslatorKeyImpl<>("SpeedmentApplicationMetadata", Class.class);


public final static TranslatorKey<Table> public final static TranslatorKey<Table, Interface>
ENTITY = new TranslatorKeyImpl<>("Entity"), ENTITY = new TranslatorKeyImpl<>("Entity", Interface.class);
ENTITY_IMPL = new TranslatorKeyImpl<>("EntityImpl"),
MANAGER_IMPL = new TranslatorKeyImpl<>("ManagerImpl"); public final static TranslatorKey<Table, Class>
ENTITY_IMPL = new TranslatorKeyImpl<>("EntityImpl", Class.class),
MANAGER_IMPL = new TranslatorKeyImpl<>("ManagerImpl", Class.class);


public static Stream<TranslatorKey<Project>> projectTranslatorKeys() { public static Stream<TranslatorKey<Project, Class>> projectTranslatorKeys() {
return Stream.of(SPEEDMENT_APPLICATION, SPEEDMENT_APPLICATION_METADATA); return Stream.of(SPEEDMENT_APPLICATION, SPEEDMENT_APPLICATION_METADATA);
} }


public static Stream<TranslatorKey<Table>> tableTranslatorKeys() { public static Stream<TranslatorKey<Table, ? extends ClassOrInterface<?>>> tableTranslatorKeys() {
return Stream.of(ENTITY, ENTITY_IMPL, MANAGER_IMPL); return Stream.of(ENTITY, ENTITY_IMPL, MANAGER_IMPL);
} }


Expand Down
34 changes: 17 additions & 17 deletions src/main/java/com/speedment/code/Translator.java
Expand Up @@ -37,30 +37,30 @@
import com.speedment.internal.codegen.lang.models.ClassOrInterface; import com.speedment.internal.codegen.lang.models.ClassOrInterface;
import com.speedment.internal.codegen.lang.models.File; import com.speedment.internal.codegen.lang.models.File;
import java.util.Map; import java.util.Map;
import static java.util.Objects.requireNonNull;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import static java.util.Objects.requireNonNull;


/** /**
* A component that can translate a {@link Document} into something else. This * A component that can translate a {@link Document} into something else. This
* interface is implemented to generate more files from the same database * interface is implemented to generate more files from the same database
* structure. * structure.
* *
* @author pemi * @author pemi
* @param <T> the Document type to use * @param <DOC> the Document type to use
* @param <R> the type to translate into * @param <T> the codegen type to make (Class, Interface or Enum)
* @see Document * @see Document
* @since 2.3 * @since 2.3
*/ */
@Api(version = "2.3") @Api(version = "2.3")
public interface Translator<T extends Document & HasMainInterface, R> extends Supplier<R> { public interface Translator<DOC extends Document & HasMainInterface, T extends ClassOrInterface<T>> extends Supplier<File> {


/** /**
* The document being translated. * The document being translated.
* *
* @return the document * @return the document
*/ */
T getDocument(); DOC getDocument();


/** /**
* The document being translated wrapped in a {@link HasAlias}. * The document being translated wrapped in a {@link HasAlias}.
Expand Down Expand Up @@ -198,7 +198,7 @@ default <E extends Document> E getDocument(Class<E> clazz) {
)); ));
} }


default Meta<R, String> generate() { default Meta<File, String> generate() {
return getCodeGenerator().metaOn(get()).findFirst().orElseThrow(() -> new SpeedmentException("Unable to generate Java code")); return getCodeGenerator().metaOn(get()).findFirst().orElseThrow(() -> new SpeedmentException("Unable to generate Java code"));
} }


Expand All @@ -208,16 +208,14 @@ default String toCode() {


Generator getCodeGenerator(); Generator getCodeGenerator();


void onClass(BiConsumer<File, Builder<com.speedment.internal.codegen.lang.models.Class>> action); void onMake(BiConsumer<File, Builder<T>> action);
void onInterface(BiConsumer<File, Builder<com.speedment.internal.codegen.lang.models.Interface>> action);
void onEnum(BiConsumer<File, Builder<com.speedment.internal.codegen.lang.models.Enum>> action);


Stream<BiConsumer<File, Builder<com.speedment.internal.codegen.lang.models.Class>>> classListeners(); Stream<BiConsumer<File, Builder<T>>> listeners();
Stream<BiConsumer<File, Builder<com.speedment.internal.codegen.lang.models.Interface>>> interfaceListeners();
Stream<BiConsumer<File, Builder<com.speedment.internal.codegen.lang.models.Enum>>> enumListeners();


interface Builder<T extends ClassOrInterface<T>> { interface Builder<T extends ClassOrInterface<T>> {
<P extends Document, D extends Document> Builder<T> addConsumer(String key, BiFunction<P, Map<String, Object>, D> constructor, BiConsumer<T, D> consumer); <P extends Document, DOC extends Document> Builder<T>
addConsumer(String key, BiFunction<P, Map<String, Object>, DOC> constructor, BiConsumer<T, DOC> consumer);

Builder<T> addProjectConsumer(BiConsumer<T, Project> consumer); Builder<T> addProjectConsumer(BiConsumer<T, Project> consumer);
Builder<T> addDbmsConsumer(BiConsumer<T, Dbms> consumer); Builder<T> addDbmsConsumer(BiConsumer<T, Dbms> consumer);
Builder<T> addSchemaConsumer(BiConsumer<T, Schema> consumer); Builder<T> addSchemaConsumer(BiConsumer<T, Schema> consumer);
Expand All @@ -226,5 +224,7 @@ interface Builder<T extends ClassOrInterface<T>> {
Builder<T> addIndexConsumer(BiConsumer<T, Index> consumer); Builder<T> addIndexConsumer(BiConsumer<T, Index> consumer);
Builder<T> addForeignKeyConsumer(BiConsumer<T, ForeignKey> consumer); Builder<T> addForeignKeyConsumer(BiConsumer<T, ForeignKey> consumer);
Builder<T> addForeignKeyReferencesThisTableConsumer(BiConsumer<T, ForeignKey> consumer); Builder<T> addForeignKeyReferencesThisTableConsumer(BiConsumer<T, ForeignKey> consumer);

T build();
} }
} }
13 changes: 7 additions & 6 deletions src/main/java/com/speedment/code/TranslatorConstructor.java
Expand Up @@ -20,16 +20,17 @@
import com.speedment.annotation.Api; import com.speedment.annotation.Api;
import com.speedment.config.db.trait.HasMainInterface; import com.speedment.config.db.trait.HasMainInterface;
import com.speedment.internal.codegen.base.Generator; import com.speedment.internal.codegen.base.Generator;
import com.speedment.internal.codegen.lang.models.File; import com.speedment.internal.codegen.lang.models.ClassOrInterface;


/** /**
* *
* @author Per Minborg * @author Emil Forslund
* @param <T> document type * @param <DOC> document type
* @since 2.3 * @param <T> codegen model type
* @since 2.3
*/ */
@Api(version = "2.3") @Api(version = "2.3")
@FunctionalInterface @FunctionalInterface
public interface TranslatorConstructor<T extends HasMainInterface> { public interface TranslatorConstructor<DOC extends HasMainInterface, T extends ClassOrInterface<T>> {
Translator<T, File> apply(Speedment speedment, Generator gen, T document); Translator<DOC, T> apply(Speedment speedment, Generator gen, DOC document);
} }
12 changes: 7 additions & 5 deletions src/main/java/com/speedment/code/TranslatorDecorator.java
Expand Up @@ -19,16 +19,18 @@
import com.speedment.annotation.Api; import com.speedment.annotation.Api;
import com.speedment.config.db.trait.HasMainInterface; import com.speedment.config.db.trait.HasMainInterface;
import com.speedment.config.db.trait.HasName; import com.speedment.config.db.trait.HasName;
import com.speedment.internal.codegen.lang.models.ClassOrInterface;
import com.speedment.internal.core.code.JavaClassTranslator; import com.speedment.internal.core.code.JavaClassTranslator;


/** /**
* *
* @author Emil Forslund * @author Emil Forslund
* @param <T> the document type * @param <DOC> the document type
* @since 2.3 * @param <T> the codegen model type
* @since 2.3
*/ */
@Api(version = "2.3") @Api(version = "2.3")
@FunctionalInterface @FunctionalInterface
public interface TranslatorDecorator<T extends HasName & HasMainInterface> { public interface TranslatorDecorator<DOC extends HasName & HasMainInterface, T extends ClassOrInterface<T>> {
void apply(JavaClassTranslator<T> translator); void apply(JavaClassTranslator<DOC, T> translator);
} }
22 changes: 17 additions & 5 deletions src/main/java/com/speedment/code/TranslatorKey.java
Expand Up @@ -18,17 +18,29 @@


import com.speedment.annotation.Api; import com.speedment.annotation.Api;
import com.speedment.config.db.trait.HasMainInterface; import com.speedment.config.db.trait.HasMainInterface;
import com.speedment.internal.codegen.lang.models.ClassOrInterface;


/** /**
* *
* @author Per Minborg * @author Per Minborg
* @param <T> Document type * @param <DOC> Document type
* @since 2.3 * @param <T> CodeGen main model
* @since 2.3
*/ */
@Api(version = "2.3") @Api(version = "2.3")
public interface TranslatorKey<T extends HasMainInterface> { public interface TranslatorKey<DOC extends HasMainInterface, T extends ClassOrInterface<T>> {


/**
* Returns the key that is used to identify this category of translators.
*
* @return the key
*/
String getKey(); String getKey();



/**
* Returns the CodeGen type that the translator will operate on.
*
* @return codegen main model type
*/
Class<T> getTranslatedType();
} }

0 comments on commit cfa8f10

Please sign in to comment.