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

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

package net.sf.javagimmicks.collections8.event;

import java.util.SortedSet;

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

/**
 * A {@link SortedSet} decorator that serves as an {@link Observable} for
 * {@link SortedSetEvent}s.
 */
public class ObservableEventSortedSet extends AbstractEventSortedSet implements
      Observable>
{
   private static final long serialVersionUID = 7595639007080114146L;

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

   /**
    * Wraps a new {@link ObservableEventSortedSet} around a given
    * {@link SortedSet}.
    * 
    * @param decorated
    *           the {@link SortedSet} to wrap around
    */
   public ObservableEventSortedSet(final SortedSet 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 ObservableEventSortedSet headSet(final E toElement)
   {
      return new ObservableEventSubSortedSet(this, getDecorated().headSet(toElement));
   }

   @Override
   public ObservableEventSortedSet subSet(final E fromElement, final E toElement)
   {
      return new ObservableEventSubSortedSet(this, getDecorated().subSet(fromElement, toElement));
   }

   @Override
   public ObservableEventSortedSet tailSet(final E fromElement)
   {
      return new ObservableEventSubSortedSet(this, getDecorated().tailSet(fromElement));
   }

   @Override
   protected void fireElementAdded(final E element)
   {
      _helper.fireEvent(new SortedSetEventImpl(Type.ADDED, element));
   }

   @Override
   protected void fireElementReadded(final E element)
   {
      _helper.fireEvent(new SortedSetEventImpl(Type.READDED, element));
   }

   @Override
   protected void fireElementRemoved(final E element)
   {
      _helper.fireEvent(new SortedSetEventImpl(Type.REMOVED, element));
   }

   private class SortedSetEventImpl implements SortedSetEvent
   {
      protected final Type _type;
      protected final E _element;

      public SortedSetEventImpl(final Type type, final E element)
      {
         _type = type;
         _element = element;
      }

      @Override
      public Type getType()
      {
         return _type;
      }

      @Override
      public E getElement()
      {
         return _element;
      }

      @Override
      public ObservableEventSortedSet getSource()
      {
         return ObservableEventSortedSet.this;
      }
   }

   protected static class ObservableEventSubSortedSet extends ObservableEventSortedSet
   {
      private static final long serialVersionUID = 6739254388693769351L;

      protected final ObservableEventSortedSet _parent;

      protected ObservableEventSubSortedSet(final ObservableEventSortedSet parent, final SortedSet decorated)
      {
         super(decorated);

         _parent = parent;
      }

      @Override
      protected void fireElementAdded(final E element)
      {
         super.fireElementAdded(element);
         _parent.fireElementAdded(element);
      }

      @Override
      protected void fireElementReadded(final E element)
      {
         super.fireElementReadded(element);
         _parent.fireElementReadded(element);
      }

      @Override
      protected void fireElementRemoved(final E element)
      {
         super.fireElementRemoved(element);
         _parent.fireElementRemoved(element);
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy