All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.arquillian.spacelift.task.impl.TaskRegistryImpl Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package org.arquillian.spacelift.task.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.arquillian.spacelift.task.InvalidTaskException;
import org.arquillian.spacelift.task.NoArgConstructorTaskFactory;
import org.arquillian.spacelift.task.Task;
import org.arquillian.spacelift.task.TaskFactory;
import org.arquillian.spacelift.task.TaskRegistry;

public class TaskRegistryImpl implements TaskRegistry {

    private final Map, TaskFactory> classRegistry;
    private final Map> aliasRegistry;

    public TaskRegistryImpl() {
        this.classRegistry = new ConcurrentHashMap, TaskFactory>();
        this.aliasRegistry = new ConcurrentHashMap>();
    }

    @Override
    public , TASK_FACTORY extends TaskFactory> TaskRegistry register(
        Class taskDef, TASK_FACTORY taskFactory) throws InvalidTaskException {

        for (String alias : taskFactory.aliases()) {
            aliasRegistry.put(alias, taskFactory);
        }

        // use class name and simple name as aliases as well in case task definition was provided
        if(taskDef!=null) {
            classRegistry.put(taskDef, taskFactory);
            aliasRegistry.put(taskDef.getName(), taskFactory);
            aliasRegistry.put(taskDef.getSimpleName(), taskFactory);
        }

        return this;
    }

    @Override
    public , TASK_FACTORY extends TaskFactory> TaskRegistry register(
        TASK_FACTORY taskFactory) throws InvalidTaskException {
        return register(null, taskFactory);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public > TaskRegistry register(Class taskType, String... aliases)
        throws InvalidTaskException {
        return register(taskType, new NoArgConstructorTaskFactory(taskType, aliases));
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public > TASK find(Class taskType) throws InvalidTaskException {

        // if there is no such class, try to register default factory
        if (!classRegistry.containsKey(taskType)) {
            register(taskType, new NoArgConstructorTaskFactory(taskType));
        }

        return (TASK) classRegistry.get(taskType).create();
    }

    @Override
    public Task find(String alias) throws InvalidTaskException {

        if (!aliasRegistry.containsKey(alias)) {
            throw new InvalidTaskException("No task with alias {0} was registered.", alias);
        }
        return aliasRegistry.get(alias).create();
    }

    @SuppressWarnings("unchecked")
    @Override
    public  Task find(String alias, Class inType, Class outType) throws InvalidTaskException {

        if (!aliasRegistry.containsKey(alias)) {
            throw new InvalidTaskException("No task with alias {0} was registered.", alias);
        }
        return (Task) aliasRegistry.get(alias).create();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy