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

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

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

import java.util.Set;

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

/**
 * A {@link Set} decorator that serves as an {@link Observable} for
 * {@link SetEvent}s.
 */
public class ObservableEventSet extends AbstractEventSet implements Observable>
{
   private static final long serialVersionUID = 4799365684601532982L;

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

   /**
    * Wraps a new {@link ObservableEventSet} around a given {@link Set}.
    * 
    * @param decorated
    *           the {@link Set} to wrap around
    */
   public ObservableEventSet(final Set 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 fireElementAdded(final E element)
   {
      _helper.fireEvent(new SetEventImpl(Type.ADDED, element));
   }

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

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

   private class SetEventImpl implements SetEvent
   {
      protected final Type _type;
      protected final E _element;

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

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

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy