/*******************************************************************************
 * This file is part of TERMINAL RECALL
 * Copyright (c) 2012-2015 Chuck Ritola
 * Part of the jTRFP.org project
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     chuck - initial API and implementation
 ******************************************************************************/

package org.jtrfp.trcl.coll;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executor;

import org.jtrfp.trcl.tools.Util;

public class CollectionThreadDecoupler<E> implements Collection<E>, Repopulatable<E>, Decorator<Collection<E>>, BulkRemovable<E> {
    private final Executor 	executor;
    private final Collection<E>	delegate;
    
    public CollectionThreadDecoupler(Collection<E> delegate, Executor executor){
	this.executor=executor;
	this.delegate=delegate;
    }

    @Override
    public boolean add(final E element) {
	executor.execute(new Runnable(){
	    @Override
	    public void run() {
		delegate.add(element);
	    }});
	return true;
    }//end add(E)

    @Override
    public boolean addAll(final Collection<? extends E> elements) {
	final List<E> toAdd = (List<E>)Arrays.asList(elements.toArray());
	executor.execute(new Runnable(){
	    @Override
	    public void run() {
		delegate.addAll(toAdd);
	    }});
	return true;
    }

    @Override
    public void clear() {
	executor.execute(new Runnable(){
	    @Override
	    public void run() {
		delegate.clear();
	    }});
    }

    @Override
    @Deprecated
    public boolean contains(Object arg0) {
	throw new UnsupportedOperationException();
    }

    @Override
    @Deprecated
    public boolean containsAll(Collection<?> arg0) {
	throw new UnsupportedOperationException();
    }

    @Override
    @Deprecated
    public boolean isEmpty() {
	throw new UnsupportedOperationException();
    }

    @Override
    @Deprecated
    public Iterator<E> iterator() {
	throw new UnsupportedOperationException();
    }

    @Override
    public boolean remove(final Object element) {
	executor.execute(new Runnable(){
	    @Override
	    public void run() {
		delegate.remove(element);
	    }});
	return true;
    }

    @Override
    public boolean removeAll(final Collection<?> elements) {
	final List<?> toRemove = (List<?>)Arrays.asList(elements.toArray());
	executor.execute(new Runnable(){
	    @Override
	    public void run() {
		delegate.removeAll(toRemove);
	    }});
	return true;
    }

    @Override
    public boolean retainAll(final Collection<?> elements) {
	final List<?> toRetain = (List<?>)Arrays.asList(elements.toArray());
	executor.execute(new Runnable(){
	    @Override
	    public void run() {
		delegate.retainAll(toRetain);
	    }});
	return true;
    }

    @Override
    @Deprecated
    public int size() {
	throw new UnsupportedOperationException();
    }

    @Override
    @Deprecated
    public Object[] toArray() {
	throw new UnsupportedOperationException();
    }

    @Override
    @Deprecated
    public <T> T[] toArray(T[] arg0) {
	throw new UnsupportedOperationException();
    }

    @Override
    public void repopulate(final Collection<E> c) {
	final List<E> toRepopulate = (List<E>)Arrays.asList(c.toArray());
	executor.execute(new Runnable(){
	    @Override
	    public void run() {
		Util.repopulate(delegate,toRepopulate);
	    }});
    }
    
    public Collection<E> getDelegate(){
	return delegate;
    }

    @Override
    public void bulkRemove(final Collection<E> items) {
	executor.execute(new Runnable(){
	    @Override
	    public void run() {
		Util.bulkRemove(items, delegate);
	    }});
    }
}//end CollectionActionSource

@@ -1152,32 +1152,45 @@
 ride%0A    public 
+synchronized 
 boolean add(fina
@@ -1196,32 +1196,32 @@
 al E element) %7B%0A
-
 %09executor.execut
@@ -1257,32 +1257,45 @@
 ide%0A%09    public 
+synchronized 
 void run() %7B%0A%09%09d
@@ -1376,32 +1376,45 @@
 ride%0A    public 
+synchronized 
 boolean addAll(f
@@ -1574,32 +1574,45 @@
 ide%0A%09    public 
+synchronized 
 void run() %7B%0A%09%09d
@@ -1686,24 +1686,37 @@
 %0A    public 
+synchronized 
 void clear()
@@ -1771,32 +1771,45 @@
 ide%0A%09    public 
+synchronized 
 void run() %7B%0A%09%09d
@@ -1875,32 +1875,45 @@
 ated%0A    public 
+synchronized 
 boolean contains
@@ -2012,32 +2012,45 @@
 ated%0A    public 
+synchronized 
 boolean contains
@@ -2159,32 +2159,45 @@
 ated%0A    public 
+synchronized 
 boolean isEmpty(
@@ -2292,16 +2292,29 @@
  public 
+synchronized 
 Iterator
@@ -2398,32 +2398,45 @@
 ride%0A    public 
+synchronized 
 boolean remove(f
@@ -2511,32 +2511,45 @@
 ide%0A%09    public 
+synchronized 
 void run() %7B%0A%09%09d
@@ -2621,32 +2621,45 @@
 ride%0A    public 
+synchronized 
 boolean removeAl
@@ -2815,32 +2815,45 @@
 ide%0A%09    public 
+synchronized 
 void run() %7B%0A%09%09d
@@ -2937,16 +2937,29 @@
  public 
+synchronized 
 boolean 
@@ -3123,32 +3123,45 @@
 ide%0A%09    public 
+synchronized 
 void run() %7B%0A%09%09d
@@ -3579,24 +3579,37 @@
 %0A    public 
+synchronized 
 void repopul
@@ -3757,32 +3757,45 @@
 ide%0A%09    public 
+synchronized 
 void run() %7B%0A%09%09U
@@ -3940,24 +3940,37 @@
 %0A    public 
+synchronized 
 void bulkRem
@@ -4043,32 +4043,32 @@
 %0A%09    @Override%0A
-
 %09    public void
@@ -4063,16 +4063,29 @@
  public 
+synchronized 
 void run

Add sync to CollectionThreadDecoupler.