- Joined
- Jan 5, 2015
- Messages
- 17
- Thread Author
- #1
Code:
public class ModuleWrapper {
private LinkedHashMap<Condition, ArrayList<Module>> arrayListMap = new LinkedHashMap<>();
public ArrayList addModuleList(Condition condition) {
return arrayListMap.put(condition, new ArrayList<>());
}
private ArrayList<Module> getMapKey(int key) {
return arrayListMap.get(arrayListMap.keySet().toArray()[key]);
}
public void submit(final Module module, int key) {
if(getMapKey(key) !=null) {
getMapKey(key).add(module);
}
}
public ArrayList<Module> getValidList() {
ArrayList<Module> validList = new ArrayList<>();
arrayListMap.forEach((aBoolean, modules) -> {
if(aBoolean.check()) {
modules.forEach(validList::add);
}
});
return validList;
}
public Module getValidModule() {
for(Module m : getValidList()) {
if(m.validate())
return m;
}
return null;
}
}
Module Interface
Code:
public interface Module {
boolean validate();
void execute();
}
Condition Interface
Code:
public interface Condition {
boolean check();
}
Usage:
Code:
public boolean onStart() {
mw.addModuleList(Bank::isOpen);
mw.addModuleList(() -> !Bank.isOpen());
mw.submit(new Example(), 0);
mw.submit(new Example2(), 1);
return true;
}
You can add array lists and then add modules to those array lists by putting the index of the array list in the hashmap as a parameter in the submit.
Then getValidModule gets the valid array list and then iterates through that valid list for modules that are valid.
This is a really easy way to split up scripts into sections.