All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.collections8.event.ObservableEventMap Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.collections8.event;

import java.util.Map;

import net.sf.javagimmicks.collections8.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