package nl.tudelft.simulation.event.util;
import java.util.Collection;
import java.util.Iterator;
import nl.tudelft.simulation.event.EventProducer;
import nl.tudelft.simulation.event.EventType;
/**
* The Event producing collection provides a set to which one can subscribe interest in entry changes. This class does
* not keep track of changes which take place indirectly. One is for example not notified on
* map.iterator.remove()
. A listener must subscribe to the iterator individually.
*
* Copyright (c) 2002-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights
* reserved. See for project information
* https://simulation.tudelft.nl. The DSOL project is distributed under a three-clause BSD-style license, which can
* be found at
* https://simulation.tudelft.nl/dsol/3.0/license.html.
*
* @author Peter Jacobs
* @author Alexander Verbraeck
* @since 1.5
* @param The type t od the eventproducing list.
*/
public class EventProducingCollection extends EventProducer implements Collection
{
/** The default serial version UID for serializable classes. */
private static final long serialVersionUID = 1L;
/** OBJECT_ADDED_EVENT is fired on new entries. */
public static final EventType OBJECT_ADDED_EVENT = new EventType("OBJECT_ADDED_EVENT");
/** OBJECT_REMOVED_EVENT is fired on removel of entries. */
public static final EventType OBJECT_REMOVED_EVENT = new EventType("OBJECT_REMOVED_EVENT");
/** the parent collection. */
private Collection parent = null;
/**
* constructs a new EventProducingList.
* @param parent Collection<T>; the parent collection.
*/
public EventProducingCollection(final Collection parent)
{
super();
this.parent = parent;
}
/** {@inheritDoc} */
@Override
public int size()
{
return this.parent.size();
}
/** {@inheritDoc} */
@Override
public boolean isEmpty()
{
return this.parent.isEmpty();
}
/** {@inheritDoc} */
@Override
public void clear()
{
this.parent.clear();
this.fireEvent(OBJECT_REMOVED_EVENT, null);
}
/** {@inheritDoc} */
@Override
public boolean add(final T o)
{
boolean result = this.parent.add(o);
this.fireEvent(OBJECT_ADDED_EVENT, null);
return result;
}
/** {@inheritDoc} */
@Override
public boolean addAll(final Collection extends T> c)
{
boolean result = this.parent.addAll(c);
this.fireEvent(OBJECT_ADDED_EVENT, null);
return result;
}
/** {@inheritDoc} */
@Override
public boolean contains(final Object o)
{
return this.parent.contains(o);
}
/** {@inheritDoc} */
@Override
public boolean containsAll(final Collection> c)
{
return this.parent.containsAll(c);
}
/** {@inheritDoc} */
@Override
public Iterator iterator()
{
return new EventIterator(this.parent.iterator());
}
/** {@inheritDoc} */
@Override
public boolean remove(final Object o)
{
boolean result = this.parent.remove(o);
this.fireEvent(OBJECT_REMOVED_EVENT, null);
return result;
}
/** {@inheritDoc} */
@Override
public boolean removeAll(final Collection> c)
{
boolean result = this.parent.removeAll(c);
this.fireEvent(OBJECT_REMOVED_EVENT, null);
return result;
}
/** {@inheritDoc} */
@Override
public boolean retainAll(final Collection> c)
{
boolean result = this.parent.retainAll(c);
this.fireEvent(OBJECT_REMOVED_EVENT, null);
return result;
}
/** {@inheritDoc} */
@Override
public Object[] toArray()
{
return this.parent.toArray();
}
/** {@inheritDoc} */
@Override
public E[] toArray(final E[] a)
{
return this.parent.toArray(a);
}
}