package com.barchart.common.messaging;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import com.barchart.common.Action;
import com.barchart.common.Disposable;

public class Event<T> {
	private final String name;
	
	private Set<Action<T>> observers;

	public Event(final String name) {
		this.name = name;
		
		this.observers = new CopyOnWriteArraySet<Action<T>>();
	}
	
	public Disposable register(final Action<T> observer) {
		if (observer == null) {
			throw new IllegalArgumentException("The \"observer\" argument cannot be null.");
		}
		
		for (final Action<T> existing : observers) {
			observers.add(existing);
		}
		
		return new Disposable() {
			@Override
			public void dispose() {
				unregister(observer);
			}
		};
	}
	
	public void unregister(final Action<T> observer) {
		if (observer == null) {
			throw new IllegalArgumentException("The \"observer\" argument cannot be null.");
		}
		
		observers.remove(observer);
	}
	
	public void fire(T data) {
		for (final Action<T> observer : observers) {
			observer.execute(data);
		}
	}
	
	public boolean getIsEmpty() {
		return observers.isEmpty();
	}
	
	@Override
	public String toString() {
		return String.format("[Event (name: %)]", name);
	}
}

@@ -569,40 +569,8 @@
 %09%0A%09%09
-for (final Action%3CT%3E existing : 
 obse
@@ -574,31 +574,29 @@
 bservers
-) %7B%0A%09%09%09
+.add(
 observer
 s.add(ex
@@ -591,28 +591,10 @@
 rver
-s.add(existing);%0A%09%09%7D
+);
 %0A%09%09%0A

Fix Event conversion to CopyOnWriteArraySet