/*-
 * Copyright 2015, 2016 Diamond Light Source Ltd.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.eclipse.january.dataset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Class used by DynamicDataset to delegate
 */
public class DataListenerDelegate {

	private List<IDataListener> listeners;

	public DataListenerDelegate() {
		listeners = Collections.synchronizedList(new ArrayList<IDataListener>());
	}
	
	public void addDataListener(IDataListener l) {
		synchronized (listeners) {
			if (!listeners.contains(l)) {
				listeners.add(l);
			}
		}
	}

	public void removeDataListener(IDataListener l) {
		listeners.remove(l);
	}

	public void fire(DataEvent evt) {
		synchronized (listeners) {
			for (Iterator<IDataListener> iterator = listeners.iterator(); iterator.hasNext();) {
				iterator.next().dataChangePerformed(evt);
			}
		}
	}

	public boolean hasDataListeners() {
		return listeners.size() > 0;
	}

	public void clear() {
		listeners.clear();
 	}

}

@@ -343,36 +343,8 @@
 t;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -386,24 +386,19 @@
 va.util.
-Iterator
+Set
 ;%0Aimport
@@ -408,20 +408,44 @@
 va.util.
-List
+concurrent.ConcurrentHashMap
 ;%0A%0A/**%0A 
@@ -537,19 +537,18 @@
 private 
-Lis
+Se
 t%3CIDataL
@@ -631,38 +631,43 @@
 ons.
-synchronizedList(new ArrayList
+newSetFromMap(new ConcurrentHashMap
 %3CIDa
@@ -676,16 +676,25 @@
 Listener
+, Boolean
 %3E());%0A%09%7D
@@ -694,17 +694,16 @@
 ());%0A%09%7D%0A
-%09
 %0A%09public
@@ -747,72 +747,8 @@
 ) %7B%0A
-%09%09synchronized (listeners) %7B%0A%09%09%09if (!listeners.contains(l)) %7B%0A%09%09
 %09%09li
@@ -763,25 +763,16 @@
 add(l);%0A
-%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A%0A%09pub
@@ -886,52 +886,13 @@
 %7B%0A%09%09
-synchronized (listeners) %7B%0A%09%09%09for (Iterator%3C
+for (
 IDat
@@ -904,20 +904,19 @@
 ener
-%3E iterator =
+ listener :
  lis
@@ -925,63 +925,23 @@
 ners
-.iterator(); iterator.hasNext();) %7B%0A%09%09%09%09iterator.next()
+) %7B%0A%09%09%09listener
 .dat
@@ -967,13 +967,8 @@
 t);%0A
-%09%09%09%7D%0A
 %09%09%7D%0A
@@ -1087,15 +1087,14 @@
 lear();%0A
- 
 %09%7D%0A%0A%7D%0A

Improve thread safety around adding and removing IDataListener's