net.sf.javagimmicks.collections.event.AbstractEventCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gimmicks Show documentation
Show all versions of gimmicks Show documentation
Utility classes, APIs and tools for Java
package net.sf.javagimmicks.collections.event;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import net.sf.javagimmicks.collections.decorators.AbstractUnmodifiableCollectionDecorator;
/**
* A base {@link Collection} wrapper that reports changes to internal callback
* methods - these must be overwritten by concrete implementations in order to
* react in any way to the changes.
*
* Methods that must be overwritten:
*
* - {@link #fireElementsAdded(Collection)}
* - {@link #fireElementRemoved(Object)}
*
*
* Methods that may be overwritten:
*
* - {@link #fireElementAdded(Object)}
*
*/
public abstract class AbstractEventCollection extends AbstractUnmodifiableCollectionDecorator
{
private static final long serialVersionUID = -8335291555421718053L;
/**
* Wraps a new instance around a given {@link Collection}
*
* @param decorated
* the {@link Collection} to wrap
*/
public AbstractEventCollection(final Collection decorated)
{
super(decorated);
}
@Override
public boolean add(final E e)
{
final boolean result = getDecorated().add(e);
if (result)
{
fireElementAdded(e);
}
return result;
}
@Override
public boolean addAll(final Collection extends E> c)
{
final Collection decorated = getDecorated();
final ArrayList added = new ArrayList();
for (final E element : c)
{
if (decorated.add(element))
{
added.add(element);
}
}
if (!added.isEmpty())
{
fireElementsAdded(added);
return true;
}
else
{
return false;
}
}
@Override
public Iterator iterator()
{
return new EventCollectionIterator(getDecorated().iterator());
}
@SuppressWarnings("unchecked")
@Override
public boolean remove(final Object o)
{
final boolean result = getDecorated().remove(o);
if (result)
{
fireElementRemoved((E) o);
}
return result;
}
abstract protected void fireElementsAdded(Collection extends E> c);
abstract protected void fireElementRemoved(E element);
protected void fireElementAdded(final E element)
{
fireElementsAdded(Collections.singleton(element));
}
protected class EventCollectionIterator implements Iterator
{
protected final Iterator _decorated;
protected E _lastElement = null;
public EventCollectionIterator(final Iterator decorated)
{
_decorated = decorated;
}
@Override
public boolean hasNext()
{
return _decorated.hasNext();
}
@Override
public E next()
{
_lastElement = _decorated.next();
return _lastElement;
}
@Override
public void remove()
{
_decorated.remove();
fireElementRemoved(_lastElement);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy