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

net.sf.javagimmicks.collections.event.ObservableEventCollection Maven / Gradle / Ivy

The newest version!
package net.sf.javagimmicks.collections.event;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import net.sf.javagimmicks.collections.event.CollectionEvent.Type;

public class ObservableEventCollection extends AbstractEventCollection
{
   private static final long serialVersionUID = -4055919694275882002L;

   protected transient List> _listeners;

   public ObservableEventCollection(Collection decorated)
   {
      super(decorated);
   }

   public void addEventCollectionListener(EventCollectionListener listener)
   {
      if(_listeners == null)
      {
         _listeners = new ArrayList>();
      }
      
      _listeners.add(listener);
   }
   
   public void removeEventCollectionListener(EventCollectionListener listener)
   {
      if(_listeners != null)
      {
         _listeners.remove(listener);
      }
   }

   @Override
   protected void fireElementsAdded(Collection elements)
   {
      fireEvent(new CollectionEvent(this, Type.ADDED, Collections.unmodifiableCollection(new ArrayList(elements))));
   }
   
   @Override
   protected void fireElementRemoved(E element)
   {
      fireEvent(new CollectionEvent(this, Type.REMOVED, Collections.singleton(element)));
   }
   
   private void fireEvent(CollectionEvent event)
   {
      if(_listeners == null)
      {
         return;
      }
      
      for(EventCollectionListener listener : _listeners)
      {
         listener.eventOccured(event);
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy