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

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

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

import java.util.SortedMap;

import net.sf.javagimmicks.collections8.event.SortedMapEvent.Type;
import net.sf.javagimmicks.event.EventListener;
import net.sf.javagimmicks.event.Observable;
import net.sf.javagimmicks.event.ObservableBase;

/**
 * A {@link SortedMap} decorator that serves as an {@link Observable} for
 * {@link SortedMapEvent}s.
 */
public class ObservableEventSortedMap extends AbstractEventSortedMap implements
      Observable>
{
   private static final long serialVersionUID = -4377528012758388630L;

   protected final ObservableBase> _helper = new ObservableBase>();

   /**
    * Wraps a new {@link ObservableEventSortedMap} around a given
    * {@link SortedMap}.
    * 
    * @param decorated
    *           the {@link SortedMap} to wrap around
    */
   public ObservableEventSortedMap(final SortedMap decorated)
   {
      super(decorated);
   }

   @Override
   public >> void addEventListener(final L listener)
   {
      _helper.addEventListener(listener);
   }

   @Override
   public >> void removeEventListener(final L listener)
   {
      _helper.removeEventListener(listener);
   }

   @Override
   public ObservableEventSortedMap headMap(final K toKey)
   {
      return new ObservableEventSubSortedMap(this, getDecorated().headMap(toKey));
   }

   @Override
   public ObservableEventSortedMap subMap(final K fromKey, final K toKey)
   {
      return new ObservableEventSubSortedMap(this, getDecorated().subMap(fromKey, toKey));
   }

   @Override
   public ObservableEventSortedMap tailMap(final K fromKey)
   {
      return new ObservableEventSubSortedMap(this, getDecorated().tailMap(fromKey));
   }

   @Override
   protected void fireEntryAdded(final K key, final V value)
   {
      _helper.fireEvent(new SortedMapEventImpl(Type.ADDED, key, value));
   }

   @Override
   protected void fireEntryRemoved(final K key, final V value)
   {
      _helper.fireEvent(new SortedMapEventImpl(Type.REMOVED, key, value));
   }

   @Override
   protected void fireEntryUpdated(final K key, final V oldValue, final V newValue)
   {
      _helper.fireEvent(new SortedMapEventImpl(Type.UPDATED, key, oldValue, newValue));
   }

   private class SortedMapEventImpl implements SortedMapEvent
   {
      protected final Type _type;
      protected final K _key;
      protected final V _value;
      protected final V _newValue;

      public SortedMapEventImpl(final Type type, final K key, final V value, final V newValue)
      {
         _type = type;
         _key = key;
         _value = value;
         _newValue = newValue;
      }

      public SortedMapEventImpl(final Type type, final K key, final V value)
      {
         this(type, key, value, null);
      }

      @Override
      public ObservableEventSortedMap getSource()
      {
         return ObservableEventSortedMap.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;
      }
   }

   protected static class ObservableEventSubSortedMap extends ObservableEventSortedMap
   {
      private static final long serialVersionUID = 8431554623263356463L;

      protected final ObservableEventSortedMap _parent;

      protected ObservableEventSubSortedMap(final ObservableEventSortedMap parent, final SortedMap decorated)
      {
         super(decorated);

         _parent = parent;
      }

      @Override
      protected void fireEntryAdded(final K key, final V value)
      {
         super.fireEntryAdded(key, value);
         _parent.fireEntryAdded(key, value);
      }

      @Override
      protected void fireEntryRemoved(final K key, final V value)
      {
         super.fireEntryRemoved(key, value);
         _parent.fireEntryRemoved(key, value);
      }

      @Override
      protected void fireEntryUpdated(final K key, final V oldValue, final V newValue)
      {
         super.fireEntryUpdated(key, oldValue, newValue);
         _parent.fireEntryUpdated(key, oldValue, newValue);
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy