Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @SuppressWarnings("rawtypes")
- public abstract class HandleGenericType<T> implements IHandler<T> {
- @SuppressWarnings("unchecked")
- protected final List<IHandler>[] subHandlers = (List<IHandler>[]) new List[getRequiredParameters()];
- protected HandleGenericType(Type... parameterTypes) throws NoHandlerFoundException {
- if (parameterTypes.length == 0) {
- return;
- }
- if (parameterTypes.length != getRequiredParameters()) {
- throw new IllegalArgumentException("Mismatch of parameter count. Required: " + getRequiredParameters() + " Found: " + parameterTypes.length);
- }
- for (int i = 0; i < getRequiredParameters(); i++) {
- Type type = parameterTypes[i];
- try {
- List<IHandler> handlers = Registry.GLOBAL_REGISTRY.findHandlers(type);
- if (handlers.isEmpty()) {
- throw new NoHandlerFoundException(type, "Unknown");
- }
- subHandlers[i] = handlers;
- } catch (IllegalAccessException | InstantiationException e) {
- throw new NoHandlerFoundException(type, "Unknown", e);
- }
- }
- }
- @Override
- public abstract Class<?> getRootType();
- protected abstract IHandler<? extends T> create(Type... types) throws NoHandlerFoundException;
- protected int getRequiredParameters() {
- return 1;
- }
- protected boolean canHandle(Type type) {
- return !IHandler.isAssignable(getRootType(), type);
- }
- @Override
- public IHandler<? extends T> getHandler(Type type) {
- if (!canHandle(type)) {
- return null;
- }
- if (type instanceof ParameterizedType) {
- try {
- return create(((ParameterizedType) type).getActualTypeArguments());
- } catch (NoHandlerFoundException e) {
- } // Fallthrough to return null
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment