net.sf.javagimmicks.collections.event.ObservableEventMap 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.Map;
import net.sf.javagimmicks.collections.event.MapEvent.Type;
import net.sf.javagimmicks.event.EventListener;
import net.sf.javagimmicks.event.Observable;
import net.sf.javagimmicks.event.ObservableBase;
/**
* A {@link Map} decorator that serves as an {@link Observable} for
* {@link MapEvent}s.
*/
public class ObservableEventMap extends AbstractEventMap implements
Observable>
{
private static final long serialVersionUID = 8006998141057065129L;
protected final ObservableBase> _helper = new ObservableBase>();
/**
* Wraps a new {@link ObservableEventMap} around a given {@link Map}.
*
* @param decorated
* the {@link Map} to wrap around
*/
public ObservableEventMap(final Map 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 fireEntryAdded(final K key, final V value)
{
_helper.fireEvent(new MapEventImpl(Type.ADDED, key, value));
}
@Override
protected void fireEntryRemoved(final K key, final V value)
{
_helper.fireEvent(new MapEventImpl(Type.REMOVED, key, value));
}
@Override
protected void fireEntryUpdated(final K key, final V oldValue, final V newValue)
{
_helper.fireEvent(new MapEventImpl(Type.UPDATED, key, oldValue, newValue));
}
private class MapEventImpl implements MapEvent
{
protected final Type _type;
protected final K _key;
protected final V _value;
protected final V _newValue;
public MapEventImpl(final Type type, final K key, final V value, final V newValue)
{
_type = type;
_key = key;
_value = value;
_newValue = newValue;
}
public MapEventImpl(final Type type, final K key, final V value)
{
this(type, key, value, null);
}
@Override
public ObservableEventMap getSource()
{
return ObservableEventMap.this;
}
@Override
public Type getType()
{
return _type;
}
@Override
public K getKey()
{
return _key;
}
@Override
public V getValue()
{
return _value;
}
@Override
public V getNewValue()
{
return _newValue;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy