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 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow plugins to have a factory method for instantiation
- Loading branch information
amckenzie
committed
Sep 22, 2017
1 parent
1510143
commit bf61d06
Showing
32 changed files
with
1,596 additions
and
427 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
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
18 changes: 18 additions & 0 deletions
18
pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/plugin/FactoryMethod.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,18 @@ | ||
package uk.gov.justice.generation.pojo.plugin; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Marks a public static method on a plugin as a factory method for creating an instance | ||
* of the plugin | ||
*/ | ||
@Documented | ||
@Target(METHOD) | ||
@Retention(RUNTIME) | ||
public @interface FactoryMethod { | ||
} |
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
30 changes: 30 additions & 0 deletions
30
...ore/src/main/java/uk/gov/justice/generation/pojo/plugin/PluginProviderFactoryFactory.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,30 @@ | ||
package uk.gov.justice.generation.pojo.plugin; | ||
|
||
import uk.gov.justice.generation.pojo.plugin.factory.AllPluginsInstantiator; | ||
import uk.gov.justice.generation.pojo.plugin.factory.ClassModifyingPluginsSelector; | ||
import uk.gov.justice.generation.pojo.plugin.factory.DefaultPluginsProvider; | ||
import uk.gov.justice.generation.pojo.plugin.factory.ExcludeDefaultPluginsSwitch; | ||
import uk.gov.justice.generation.pojo.plugin.factory.Instantiator; | ||
import uk.gov.justice.generation.pojo.plugin.factory.NameGeneratingPluginFactory; | ||
import uk.gov.justice.generation.pojo.plugin.factory.PluginInstantiator; | ||
import uk.gov.justice.generation.pojo.plugin.factory.PluginTypeSorter; | ||
import uk.gov.justice.generation.pojo.plugin.factory.PluginsFromClassnameListFactory; | ||
import uk.gov.justice.generation.pojo.plugin.factory.TypeModifyingPluginsSelector; | ||
|
||
public class PluginProviderFactoryFactory { | ||
|
||
public PluginProviderFactory create() { | ||
|
||
final ExcludeDefaultPluginsSwitch excludeDefaultPluginsSwitch = new ExcludeDefaultPluginsSwitch(); | ||
final DefaultPluginsProvider defaultPluginsProvider = new DefaultPluginsProvider(); | ||
|
||
return new PluginProviderFactory( | ||
new NameGeneratingPluginFactory(), | ||
new ClassModifyingPluginsSelector(excludeDefaultPluginsSwitch, defaultPluginsProvider), | ||
new TypeModifyingPluginsSelector(), | ||
new PluginTypeSorter(), | ||
new AllPluginsInstantiator(new PluginInstantiator(new Instantiator())), | ||
new PluginsFromClassnameListFactory() | ||
); | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
...e/src/main/java/uk/gov/justice/generation/pojo/plugin/factory/AllPluginsInstantiator.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,21 @@ | ||
package uk.gov.justice.generation.pojo.plugin.factory; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.util.List; | ||
|
||
public class AllPluginsInstantiator { | ||
|
||
private final PluginInstantiator pluginInstantiator; | ||
|
||
public AllPluginsInstantiator(final PluginInstantiator pluginInstantiator) { | ||
this.pluginInstantiator = pluginInstantiator; | ||
} | ||
|
||
public List<Object> instantiate(final List<String> pluginNames) { | ||
return pluginNames | ||
.stream() | ||
.map(pluginInstantiator::instantiate) | ||
.collect(toList()); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ain/java/uk/gov/justice/generation/pojo/plugin/factory/ClassModifyingPluginsSelector.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,50 @@ | ||
package uk.gov.justice.generation.pojo.plugin.factory; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static java.util.stream.Stream.concat; | ||
|
||
import uk.gov.justice.generation.pojo.plugin.classmodifying.ClassModifyingPlugin; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
public class ClassModifyingPluginsSelector { | ||
|
||
private static final Class<ClassModifyingPlugin> CLASS_MODIFYING_PLUGIN = ClassModifyingPlugin.class; | ||
|
||
private final ExcludeDefaultPluginsSwitch excludeDefaultPluginsSwitch; | ||
private final DefaultPluginsProvider defaultPluginsProvider; | ||
|
||
public ClassModifyingPluginsSelector( | ||
final ExcludeDefaultPluginsSwitch excludeDefaultPluginsSwitch, | ||
final DefaultPluginsProvider defaultPluginsProvider) { | ||
this.excludeDefaultPluginsSwitch = excludeDefaultPluginsSwitch; | ||
this.defaultPluginsProvider = defaultPluginsProvider; | ||
} | ||
|
||
public List<ClassModifyingPlugin> selectFrom(final Map<Class<?>, List<Object>> pluginTypes, final Map<String, String> generatorProperties) { | ||
return concat( | ||
defaultClassModifyingPlugins(generatorProperties), | ||
userDefinedClassModifyingPlugins(pluginTypes) | ||
).collect(toList()); | ||
} | ||
|
||
private Stream<ClassModifyingPlugin> userDefinedClassModifyingPlugins(final Map<Class<?>, List<Object>> pluginTypes) { | ||
if (pluginTypes.containsKey(CLASS_MODIFYING_PLUGIN)) { | ||
return pluginTypes.get(CLASS_MODIFYING_PLUGIN) | ||
.stream() | ||
.map(plugin -> (ClassModifyingPlugin) plugin); | ||
} | ||
|
||
return Stream.empty(); | ||
} | ||
|
||
private Stream<ClassModifyingPlugin> defaultClassModifyingPlugins(final Map<String, String> generatorProperties) { | ||
if (excludeDefaultPluginsSwitch.shouldExcludeDefaultPlugins(generatorProperties)) { | ||
return Stream.empty(); | ||
} | ||
|
||
return defaultPluginsProvider.getDefaultPlugins().stream(); | ||
} | ||
} |
Oops, something went wrong.