This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GeneratorFactory interface to allow factory creation of Generators
- Loading branch information
Showing
13 changed files
with
163 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...e/src/main/java/uk/gov/justice/maven/generator/io/files/parser/core/GeneratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.core; | ||
|
||
public interface GeneratorFactory<T> { | ||
|
||
Generator<T> create(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 0 additions & 45 deletions
45
.../main/java/uk/gov/justice/maven/generator/io/files/parser/generator/GeneratorFactory.java
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
...n/java/uk/gov/justice/maven/generator/io/files/parser/generator/MojoGeneratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.generator; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
import uk.gov.justice.maven.generator.io.files.parser.core.Generator; | ||
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Factory for generators | ||
*/ | ||
public class MojoGeneratorFactory { | ||
|
||
private final Map<String, Generator> generators = new HashMap<>(); | ||
|
||
/** | ||
* Returns an instance of a generator. | ||
* | ||
* @param generatorName fully qualified classname that implements {@link Generator} | ||
* @return the generator | ||
*/ | ||
public Generator instanceOf(final String generatorName) { | ||
|
||
if (!generators.containsKey(generatorName)) { | ||
generators.put(generatorName, createGenerator(generatorName)); | ||
} | ||
|
||
return generators.get(generatorName); | ||
} | ||
|
||
private Generator createGenerator(final String generatorName) { | ||
|
||
try { | ||
final Class<?> generatorClass = Class.forName(generatorName); | ||
|
||
if (isGeneratorFactory(generatorClass)) { | ||
final GeneratorFactory generatorFactory = (GeneratorFactory) generatorClass.newInstance(); | ||
|
||
return generatorFactory.create(); | ||
} | ||
|
||
return (Generator) generatorClass.newInstance(); | ||
} catch (final ClassNotFoundException | IllegalAccessException | InstantiationException e) { | ||
throw new IllegalArgumentException(String.format("Could not instantiate generator %s", generatorName), e); | ||
} | ||
} | ||
|
||
private boolean isGeneratorFactory(final Class<?> generatorClass) { | ||
return asList(generatorClass.getInterfaces()).contains(GeneratorFactory.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ov/justice/maven/generator/io/files/parser/generator/generators/TestGeneratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.generator.generators; | ||
|
||
import uk.gov.justice.maven.generator.io.files.parser.core.Generator; | ||
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorFactory; | ||
|
||
public class TestGeneratorFactory implements GeneratorFactory { | ||
|
||
@Override | ||
public Generator create() { | ||
return new TestGenerator(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../test/java/uk/gov/justice/maven/generator/io/files/parser/json/test/FilePathProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.json.test; | ||
|
||
import static java.lang.String.format; | ||
|
||
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
public class FilePathProvider { | ||
|
||
public static final String FILE_NAME = "json-titles.txt"; | ||
|
||
public Path filePath(final GeneratorConfig generatorConfig) { | ||
return Paths.get(format("%s/%s", generatorConfig.getOutputDirectory().toString(), FILE_NAME)); | ||
} | ||
} |
Oops, something went wrong.