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

com.opencredo.concourse.domain.commands.dispatching.DispatchingCommandProcessor Maven / Gradle / Ivy

package com.opencredo.concourse.domain.commands.dispatching;

import com.opencredo.concourse.domain.commands.Command;
import com.opencredo.concourse.domain.commands.CommandType;

import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static com.google.common.base.Preconditions.checkNotNull;

public final class DispatchingCommandProcessor implements CommandProcessor, CommandSubscribable {

    public static DispatchingCommandProcessor create() {
        return new DispatchingCommandProcessor(new ConcurrentHashMap<>());
    }

    private final ConcurrentMap processorRegistry;

    private DispatchingCommandProcessor(ConcurrentMap processorRegistry) {
        this.processorRegistry = processorRegistry;
    }

    @Override
    public Optional process(Command command) throws Exception {
        final CommandType commandType = CommandType.of(command);
        CommandProcessor processor = processorRegistry.get(commandType);
        checkNotNull(processor,
                "No command processor registered for command type %s", commandType);

        return processor.process(command);
    }

    @Override
    public CommandSubscribable subscribe(CommandType commandType, CommandProcessor commandProcessor) {
        processorRegistry.put(commandType, commandProcessor);
        return this;
    }
}