package com.herokuapp.directto.client;

import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author Ryan Brainard
 */
public final class EventSubscription {

    public static enum Event {
        DEPLOY_PRE_VERIFICATION_START,
        DEPLOY_PRE_VERIFICATION_END,
        DEPLOY_START,
        UPLOAD_START,
        UPLOAD_END,
        POLL,
        DEPLOY_END
    }

    public static interface Subscriber {
        void handle(Event event);
    }

    private final Map<Event, Set<Subscriber>> subscribers = new ConcurrentHashMap<Event, Set<Subscriber>>();

    void announce(Event event) {
        if (subscribers.containsKey(event)) {
            for (Subscriber subscriber : subscribers.get(event)) {
                subscriber.handle(event);
            }
        }
    }

    public EventSubscription subscribe(Event event, Subscriber subscriber) {
        return subscribe(EnumSet.of(event), subscriber);
    }

    public EventSubscription subscribe(EnumSet<Event> events, Subscriber subscriber) {
        for (Event event : events) {
            if (!subscribers.containsKey(event)) {
                subscribers.put(event, new CopyOnWriteArraySet<Subscriber>());
            }
            subscribers.get(event).add(subscriber);
        }

        return this;
    }
}

@@ -54,155 +54,9 @@
 til.
-EnumSet;%0Aimport java.util.Map;%0Aimport java.util.Set;%0Aimport java.util.concurrent.ConcurrentHashMap;%0Aimport java.util.concurrent.CopyOnWriteArraySet
+*
 ;%0A%0A/
@@ -488,22 +488,12 @@
 new 
-ConcurrentHash
+Enum
 Map%3C
@@ -516,16 +516,27 @@
 riber%3E%3E(
+Event.class
 );%0A%0A    
@@ -1113,24 +1113,12 @@
 new 
-CopyOnWriteArray
+Hash
 Set%3C

remove unneeded thread safety from EventSubscription