Commit fe5e39ce authored by Toni Beier's avatar Toni Beier

ASiSTModuleManager uses synchronized linked map to store module instances

The map key is the class instance if the module.
Map ist linked hash map to keep insertion order.
ASiSTModuleManager never return a modifiable instance of the map.
parent 5a767825
package de.bps.asist.core.manager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import de.bps.asist.module.AbstractAsistModule;
public class ASiSTModuleManager {
private static final ASiSTModuleManager INSTANCE = new ASiSTModuleManager();
private List<AbstractAsistModule> modules = new ArrayList<>();
private ASiSTModuleManager(){
}
public static ASiSTModuleManager getInstance() {
return INSTANCE;
}
public void resetModuleList(){
modules = new ArrayList<>();
}
public void register(AbstractAsistModule module){
modules.add(module);
}
public <T extends AbstractAsistModule> T getModule( Class<T> moduleClass, T defaultValue ) {
T resultModule = defaultValue;
for ( AbstractAsistModule registeredModule: this.modules ) {
if( moduleClass.isInstance( registeredModule ) ) {
defaultValue = (T)registeredModule;
break;
}
}
return resultModule;
}
public <T extends AbstractAsistModule> T getModule( Class<T> moduleClass) {
return this.getModule(moduleClass, null);
}
public List<AbstractAsistModule> getModules() {
return modules;
}
}
private static final ASiSTModuleManager INSTANCE = new ASiSTModuleManager();
Map<Class<? extends AbstractAsistModule>, AbstractAsistModule> modules = createEmptyModuleMap();
private ASiSTModuleManager() {
}
public static ASiSTModuleManager getInstance() {
return INSTANCE;
}
public void resetModuleList() {
modules = createEmptyModuleMap();
}
public void register(AbstractAsistModule module) {
modules.put(module.getClass(), module);
}
public <T extends AbstractAsistModule> T getModule(Class<T> moduleClass, T defaultValue) {
return modules.containsKey(moduleClass) ? (T) modules.get(moduleClass) : defaultValue;
}
public <T extends AbstractAsistModule> T getModule(Class<T> moduleClass) {
return (T) modules.get(moduleClass);
}
public List<AbstractAsistModule> getModules() {
return new ArrayList<>(modules.values());
}
protected Map<Class<? extends AbstractAsistModule>, AbstractAsistModule> createEmptyModuleMap() {
return Collections.synchronizedMap(new LinkedHashMap<Class<? extends AbstractAsistModule>, AbstractAsistModule>());
}
}
\ 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