diff options
Diffstat (limited to 'src/jcgp/backend/modules/Module.java')
-rw-r--r-- | src/jcgp/backend/modules/Module.java | 67 |
1 files changed, 59 insertions, 8 deletions
diff --git a/src/jcgp/backend/modules/Module.java b/src/jcgp/backend/modules/Module.java index 7efbf3a..b53184e 100644 --- a/src/jcgp/backend/modules/Module.java +++ b/src/jcgp/backend/modules/Module.java @@ -1,23 +1,74 @@ package jcgp.backend.modules; -import jcgp.backend.resources.parameters.Parameter; +import java.util.ArrayList; + +import jcgp.backend.parameters.Parameter; /** - * This interface defines the expected behaviour of a module. Specifically, a module - * is expected to be able to return a list of local parameters. When a user interface - * is used, it is expected to display the parameters of each module and allow user - * interaction for parameters which are not monitors. + * This class defines the expected behaviour of a module. Generally, modules + * are entities which contain parameters; these can be retrieved using + * {@code getLocalParameters()}. GUIs should make use of this getter + * to display visual parameter controls to users. Subclasses don't have direct + * access to the list; instead they must use {@code registerParameter()} (ideally + * in the constructor) to make sure the parameters are returned. + * <br> + * In addition, implementations of {@code Module} should specify a module name + * in their constructor using {@code setName()}. If a name is not provided, + * the simple name of the class will be used. * * @see Parameter - * * @author Eduardo Pedroni * */ -public interface Module { +public abstract class Module { + private ArrayList<Parameter<?>> localParameters = new ArrayList<Parameter<?>>(); + private String name = getClass().getSimpleName(); + /** + * This method is used by the GUI in order to build visual + * representations of all parameters used by the module. + * Therefore, any parameters returned here will be displayed + * visually. + * * @return a list of generic parameters exposed by the module. */ - public abstract Parameter<?>[] getLocalParameters(); + public final ArrayList<Parameter<?>> getLocalParameters() { + return localParameters; + } + + /** + * Adds one or more parameters to the list of local parameters. + * The same parameter cannot be added twice. + * <br><br> + * Implementations of {@code Module} should use this module + * to register any parameters they wish to expose to the user + * if a GUI is in use. + * + * @param newParameters the parameter(s) to add to the list. + */ + protected final void registerParameters(Parameter<?>... newParameters) { + for (int i = 0; i < newParameters.length; i++) { + if (!localParameters.contains(newParameters[i])) { + localParameters.add(newParameters[i]); + } + } + } + + /** + * Sets the name of the module, for GUI display. + * If no name is set, the simple name of the class + * is be used instead. + * + * @param name the name to set. + */ + protected void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } } |