net.sf.javagimmicks.collections8.event.ObservableEventCollection Maven / Gradle / Ivy
package net.sf.javagimmicks.collections8.event;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import net.sf.javagimmicks.collections8.event.CollectionEvent.Type;
import net.sf.javagimmicks.event.EventListener;
import net.sf.javagimmicks.event.Observable;
import net.sf.javagimmicks.event.ObservableBase;
/**
* A {@link Collection} decorator that serves as an {@link Observable} for
* {@link CollectionEvent}s.
*/
public class ObservableEventCollection extends AbstractEventCollection implements
Observable>
{
private static final long serialVersionUID = -4055919694275882002L;
protected final ObservableBase> _helper = new ObservableBase>();
/**
* Wraps a new {@link ObservableEventCollection} around a given
* {@link Collection}.
*
* @param decorated
* the {@link Collection} to wrap around
*/
public ObservableEventCollection(final Collection decorated)
{
super(decorated);
}
@Override
public >> void addEventListener(final L listener)
{
_helper.addEventListener(listener);
}
@Override
public >> void removeEventListener(final L listener)
{
_helper.removeEventListener(listener);
}
@Override
protected void fireElementsAdded(final Collection extends E> elements)
{
_helper.fireEvent(new CollectionEventImpl(Type.ADDED, Collections.unmodifiableCollection(new ArrayList(
elements))));
}
@Override
protected void fireElementRemoved(final E element)
{
_helper.fireEvent(new CollectionEventImpl(Type.REMOVED, Collections.singleton(element)));
}
private class CollectionEventImpl implements CollectionEvent
{
protected final Type _type;
protected final Collection _elements;
public CollectionEventImpl(final Type type,
final Collection elements)
{
_type = type;
_elements = elements;
}
@Override
public Type getType()
{
return _type;
}
@Override
public Collection getElements()
{
return _elements;
}
@Override
public ObservableEventCollection getSource()
{
return ObservableEventCollection.this;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy