Skip to content

Commit

Permalink
minor refactoring, javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
felixz92 committed Jun 18, 2016
1 parent e1a2ede commit e2094da
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 24 deletions.
23 changes: 16 additions & 7 deletions contrib-dsl/src/main/java/de/fz/contrib/script/GroovySupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,37 @@
import groovy.lang.GroovyShell;

/**
* Plugin providing support for groovy-scripts with a custom DSL for artemis-odb. <br>
* Plugin providing support for groovy-scripts with a custom DSL for artemis-odb.
* Register with:
* <code>{@link WorldConfigurationBuilder#with(ArtemisPlugin...)}</code>
* <p>
* For more information on available keywords, see {@link ArtemisScript}
*
* <p>For more information on available keywords, see {@link ArtemisScript}
*
* @author felixz
*/
public class GroovySupport implements ArtemisPlugin {

/**
*
*/
private final GroovyShell shell;

/**
*
*/
public GroovySupport() {
this(new GroovyShell());
}

public GroovySupport(GroovyShell shell) {
this.shell = shell;
/**
* @param groovyShell the GroovyShell to use
*/
public GroovySupport(final GroovyShell groovyShell) {
this.shell = groovyShell;
}

@Override
public void setup(WorldConfigurationBuilder b) {
b.with(new ScriptManager(shell));
public final void setup(final WorldConfigurationBuilder builder) {
builder.with(new ScriptManager(shell));
}
}
29 changes: 29 additions & 0 deletions contrib-dsl/src/main/java/de/fz/contrib/script/ScriptAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,46 @@
*/
public interface ScriptAdapter {

/**
* Initializes the script. Gets called upon registration in {@link ScriptManager}.
*
* @see com.artemis.BaseSystem#initialize()
*/
void init();

/**
* Returns the name of the script or null if not set.
*
* @return the script's name
*/
String getName();

/**
* Enables or disables the script. It will only be processed if enabled is true
*
* @param enabled whether to enable or disable the script
*/
void setEnabled(boolean enabled);

/**
* @return the enabled flag within a system
* @see com.artemis.BaseSystem#isEnabled()
*/
boolean isEnabled();

/**
* Processes the script.
*/
void process();

/**
* @return the world this script runs on
*/
World getWorld();

/**
*
* @param world the world this script should run on
*/
void setWorld(World world);
}
58 changes: 43 additions & 15 deletions contrib-dsl/src/main/java/de/fz/contrib/script/ScriptManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,36 @@
/**
* Register and retrieve your scripts with this manager.
* <p>
* If you don't call <code>setEnabled false</code> in a script, its process method will be called here.
* If you don't call <code>setEnabled false</code> in a script, <br>
* its process method will be called here.
*
* @author felixz
*/
public class ScriptManager extends BaseSystem {

/**
* This systems tag.
*/
public static final String TAG = ScriptManager.class.getSimpleName();

/**
* The registered scripts mapped by their classes.
*/
private final Map<Class<? extends ScriptAdapter>, ScriptAdapter> scriptsByClass;

/**
* The registered scripts mapped by their names.
*/
private final Map<String, ScriptAdapter> scriptsByName;

/**
* The GroovyShell to use.
*/
private GroovyShell groovyShell;

/**
* Constructs a new ScriptManager.
*/
public ScriptManager() {
this(null);
}
Expand All @@ -31,9 +49,9 @@ public ScriptManager() {
* Constructs a new ScriptManager.
* Can be registered with a {@link com.artemis.World} directly or via {@link GroovySupport}
*
* @param shell
* @param shell the {@link GroovyShell} to use
*/
public ScriptManager(GroovyShell shell) {
public ScriptManager(final GroovyShell shell) {
this.groovyShell = shell != null ? shell : new GroovyShell();
this.scriptsByClass = new HashMap<>();
this.scriptsByName = new HashMap<>();
Expand All @@ -43,43 +61,50 @@ public ScriptManager(GroovyShell shell) {
* Register a {@link ArtemisScript}.
*
* @param file the file containing a ArtemisScript
* @param <T> the explicit script type
* @return an instance of the parsed script
*/
@SuppressWarnings("unchecked")
public <T extends ScriptAdapter> T registerScript(File file) {
public final <T extends ScriptAdapter> T registerScript(final File file) {
try {
T script = (T) groovyShell.parse(file);
script.setWorld(this.world);
script.init();
this.scriptsByClass.put(script.getClass(), script);
if (script.getName() != null) this.scriptsByName.put(script.getName(), script);
if (script.getName() != null) {
this.scriptsByName.put(script.getName(), script);
}
return script;
} catch (IOException e) {
e.printStackTrace();
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
return null;
}

/**
* Register an instance of {@link ScriptAdapter} directly
* Register an instance of {@link ScriptAdapter} directly.
*
* @param script the script to register
* @param <T> the explicit script type
*/
public <T extends ScriptAdapter> void registerScript(T script) {
public final <T extends ScriptAdapter> void registerScript(final T script) {
if (!this.scriptsByClass.containsKey(script.getClass())) {
this.scriptsByClass.put(script.getClass(), script);
if (script.getName() != null) this.scriptsByName.put(script.getName(), script);
if (script.getName() != null) {
this.scriptsByName.put(script.getName(), script);
}
}
}

/**
* Retrieve a registered {@link ScriptAdapter} by its class
* Retrieve a registered {@link ScriptAdapter} by its class.
*
* @param scriptClass the class of the script to look up
* @param <T> the explicit script type
* @return the registered script instance
*/
@SuppressWarnings("unchecked")
public <T extends ScriptAdapter> T getScript(Class<T> scriptClass) {
public final <T extends ScriptAdapter> T getScript(final Class<T> scriptClass) {
return (T) this.scriptsByClass.get(scriptClass);
}

Expand All @@ -88,17 +113,20 @@ public <T extends ScriptAdapter> T getScript(Class<T> scriptClass) {
* Only possible if <code>name {scriptname}</code> is called within a script
*
* @param name the scripts name
* @param <T> the explicit script type
* @return the registered script instance
*/
@SuppressWarnings("unchecked")
public <T extends ScriptAdapter> T getScript(String name) {
public final <T extends ScriptAdapter> T getScript(final String name) {
return (T) this.scriptsByName.get(name);
}

@Override
protected void processSystem() {
protected final void processSystem() {
for (ScriptAdapter script : this.scriptsByClass.values()) {
if (script.isEnabled()) script.process();
if (script.isEnabled()) {
script.process();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/**
* Created by felixz on 18.06.16.
* The java-side of the artemis-dsl-plugin.
*
* @author felixz
*/
package de.fz.contrib.script;
package de.fz.contrib.script;

0 comments on commit e2094da

Please sign in to comment.