aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/Module.java
blob: b53184ea2d88ed30e025802e6bb52baf7f961f28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package jcgp.backend.modules;

import java.util.ArrayList;

import jcgp.backend.parameters.Parameter;

/**
 * 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 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 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;
	}
	
}