Commit d10e4958 authored by Toni Beier's avatar Toni Beier

ASiSTModuleManager class use map to store modules instances

Module map is a LinkedHashMap to keep insertion order.
Keys are the Class instance of the modules and the values are the module instances.
parent 6f601fbd
package de.bps.asist.core.manager; package de.bps.asist.core.manager;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import de.bps.asist.module.AbstractAsistModule; import de.bps.asist.module.AbstractAsistModule;
...@@ -9,7 +11,7 @@ public class ASiSTModuleManager { ...@@ -9,7 +11,7 @@ public class ASiSTModuleManager {
private static final ASiSTModuleManager INSTANCE = new ASiSTModuleManager(); private static final ASiSTModuleManager INSTANCE = new ASiSTModuleManager();
private List<AbstractAsistModule> modules = new ArrayList<>(); private Map<Class<? extends AbstractAsistModule>, AbstractAsistModule> modules = new LinkedHashMap<>();
private ASiSTModuleManager(){ private ASiSTModuleManager(){
} }
...@@ -19,22 +21,19 @@ public class ASiSTModuleManager { ...@@ -19,22 +21,19 @@ public class ASiSTModuleManager {
} }
public void resetModuleList(){ public void resetModuleList(){
modules = new ArrayList<>(); this.modules.clear();
} }
public void register(AbstractAsistModule module){ public void register(AbstractAsistModule module){
modules.add(module); this.modules.put(module.getClass(), module);
} }
public <T extends AbstractAsistModule> T getModule( Class<T> moduleClass, T defaultValue ) { public <T extends AbstractAsistModule> T getModule(Class<T> moduleClass, T defaultValue ) {
T resultModule = defaultValue; T module = (T)this.modules.get(moduleClass);
for ( AbstractAsistModule registeredModule: this.modules ) { if(module == null) {
if( moduleClass.isInstance( registeredModule ) ) { module = defaultValue;
defaultValue = (T)registeredModule;
break;
}
} }
return resultModule; return module;
} }
public <T extends AbstractAsistModule> T getModule( Class<T> moduleClass) { public <T extends AbstractAsistModule> T getModule( Class<T> moduleClass) {
...@@ -42,7 +41,6 @@ public class ASiSTModuleManager { ...@@ -42,7 +41,6 @@ public class ASiSTModuleManager {
} }
public List<AbstractAsistModule> getModules() { public List<AbstractAsistModule> getModules() {
return modules; return new ArrayList<>(modules.values());
} }
}
} \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment