
edu.stanford.protege.webprotege.dispatch.impl.ActionHandlerRegistryImpl Maven / Gradle / Ivy
The newest version!
package edu.stanford.protege.webprotege.dispatch.impl;
import edu.stanford.protege.webprotege.common.Request;
import edu.stanford.protege.webprotege.common.Response;
import edu.stanford.protege.webprotege.dispatch.*;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Author: Matthew Horridge
* Stanford University
* Bio-Medical Informatics Research Group
* Date: 20/01/2013
*/
public abstract class ActionHandlerRegistryImpl implements ActionHandlerRegistry {
// NOT a concurrent map. This is only written to in the constructor. At runtime it's essentially immutable and the
// basic maps are safe for multiple readers
private final Map, ActionHandler, ?>> registry = new HashMap<>();
public ActionHandlerRegistryImpl(Set extends ActionHandler> handlers) {
for(ActionHandler, ?> actionHandler : handlers) {
register(actionHandler);
}
}
private , R extends Response> void register(ActionHandler handler) {
registry.put(handler.getActionClass(), handler);
}
@Nonnull
@Override
@SuppressWarnings("unchecked")
public , R extends Response> ActionHandler getActionHandler(A action) {
checkNotNull(action, "action must not be null");
ActionHandler handler = (ActionHandler) registry.get(action.getClass());
if(handler == null) {
handler = (ActionHandler) registry.get(action.getClass().getSuperclass());
if (handler == null) {
throw new ActionHandlerNotFoundException(action);
}
}
return handler;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy