Android Open Source - RZAndroidBaseUtils Event






From Project

Back to project page RZAndroidBaseUtils.

License

The source code is released under:

MIT License

If you think the Android project RZAndroidBaseUtils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.raizlabs.events;
//from  w  w w .j  av  a2  s .  co  m
import java.util.HashSet;

/**
 * Class which represents an Event with arguments of type T.
 * @author Dylan James
 *
 * @param <T>
 */
public class Event<T> {
  private HashSet<EventListener<T>> listeners;
  private HashSet<EventListener<T>> listenersToAdd;
  private HashSet<EventListener<T>> listenersToRemove;
  private boolean raisingEvent;
  
  /**
   * Creates a new RZEvent
   */
  public Event() {
    listeners = new HashSet<EventListener<T>>();
    listenersToAdd = new HashSet<EventListener<T>>();
    listenersToRemove = new HashSet<EventListener<T>>();
    raisingEvent = false;
  }
  
  /**
   * Adds an RZEventListener to be notified when this event happens.
   * @param listener The listener to be notified.
   */
  public void addListener(EventListener<T> listener) {
    if (listener != null) {
      synchronized (this) {
        // If the event is currently being raised, we can't modify the collection
        // Add it to the toAdd collection
        if (raisingEvent) {
          listenersToAdd.add(listener);
        } else {
          listeners.add(listener);
        }
      }
    }
  }
  
  /**
   * Removes an RZEventListener so it will no longer be notified of
   * this event.
   * @param listener The RZEventListener to be removed.
   * @return True if the listener was removed, false if it wasn't found.
   */
  public boolean removeListener(EventListener<T> listener) {
    synchronized (this) {
      // If the event is currently being raised, we can't modify the collection
      // Add it to the toRemove collection
      if (raisingEvent) {
        listenersToRemove.add(listener);
        return listeners.contains(listener);
      } else {
        return listeners.remove(listener);
      }
    }
  }
  
  /**
   * Raises this event and notifies its listeners.
   * @param sender The object raising the even.
   * @param args The arguments to the event which will be passed to the listeners.
   */
  public void raiseEvent(Object sender, T args) {
    synchronized (this) {
      raisingEvent = true;
      listenersToAdd.clear();
      listenersToRemove.clear();
      for (EventListener<T> listener : listeners) {
        // Don't raise the event if the listener is flagged to be removed.
        if (!listenersToRemove.contains(listener)) {
          listener.onEvent(sender, args);
        }
      }
      
      for (EventListener<T> listener : listenersToAdd) {
        listeners.add(listener);
      }
      
      for (EventListener<T> listener : listenersToRemove) {
        listeners.remove(listener);
      }
      raisingEvent = false;
    }
  }
  
  /**
   * Clears all listeners from this event.
   */
  public void clear() {
    synchronized (this) {
      // If the event is currently being raised, we can't modify the collection
      // Add all of the current listeners to the toRemove collection
      if (raisingEvent) {
        for (EventListener<T> listener : listeners) {
          listenersToRemove.add(listener);
        }
      } else {
        listeners.clear();
      }
    }
  }
}




Java Source Code List

com.raizlabs.baseutils.CompatibilityUtils.java
com.raizlabs.baseutils.IOUtils.java
com.raizlabs.baseutils.Logger.java
com.raizlabs.baseutils.Math.java
com.raizlabs.baseutils.StringUtils.java
com.raizlabs.baseutils.ThreadingUtils.java
com.raizlabs.baseutils.Wrapper.java
com.raizlabs.baseutils.examples.MainActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableExampleActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableListExampleActivity.java
com.raizlabs.baseutils.examples.simplegenericadapter.SimpleGenericAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewgroupadapter.ViewGroupAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewholderstrategy.SimpleViewHolderStrategyExampleActivity.java
com.raizlabs.collections.ListUtils.java
com.raizlabs.collections.MappableSet.java
com.raizlabs.collections.TransactionalHashSet.java
com.raizlabs.concurrent.BasePrioritizedRunnable.java
com.raizlabs.concurrent.ConcurrencyUtils.java
com.raizlabs.concurrent.PrioritizedRunnable.java
com.raizlabs.concurrent.Prioritized.java
com.raizlabs.content.sharing.SharingUtils.java
com.raizlabs.database.CursorIterable.java
com.raizlabs.database.CursorIterator.java
com.raizlabs.events.EventListener.java
com.raizlabs.events.Event.java
com.raizlabs.events.ProgressListener.java
com.raizlabs.events.SimpleEventListener.java
com.raizlabs.functions.Delegate.java
com.raizlabs.functions.Predicate.java
com.raizlabs.functions.Provider.java
com.raizlabs.graphics.ImageFactory.java
com.raizlabs.graphics.drawable.async.AsyncDrawableTask.java
com.raizlabs.graphics.drawable.async.AsyncDrawableUtils.java
com.raizlabs.graphics.drawable.async.AsyncDrawableWrapper.java
com.raizlabs.graphics.drawable.async.AsyncDrawable.java
com.raizlabs.graphics.drawable.async.BaseAsyncDrawableTask.java
com.raizlabs.imagecaching.ImageCache.java
com.raizlabs.imagecaching.PrefixedImageCacheAdapter.java
com.raizlabs.imagecaching.StubImageCache.java
com.raizlabs.json.JSONArrayParserDelegate.java
com.raizlabs.json.JSONHelper.java
com.raizlabs.synchronization.OneShotLock.java
com.raizlabs.tasks.RZAsyncTaskEvent.java
com.raizlabs.tasks.RZAsyncTaskListener.java
com.raizlabs.tasks.RZAsyncTask.java
com.raizlabs.util.observable.ObservableData.java
com.raizlabs.util.observable.ObservableListAdapter.java
com.raizlabs.util.observable.ObservableList.java
com.raizlabs.view.ViewCompatibility.java
com.raizlabs.view.animation.AnimationListenerWrapper.java
com.raizlabs.view.animation.RelativeLayoutParamsAnimation.java
com.raizlabs.view.animation.ResizeAnimation.java
com.raizlabs.widget.EvenLinearLayout.java
com.raizlabs.widget.ImageMixView.java
com.raizlabs.widget.SlideRevealLayout.java
com.raizlabs.widget.ViewUtils.java
com.raizlabs.widget.adapters.ListBasedAdapter.java
com.raizlabs.widget.adapters.SimpleGenericAdapter.java
com.raizlabs.widget.adapters.ViewGroupAdapter.java
com.raizlabs.widget.adapters.ViewHolderStrategyAdapter.java
com.raizlabs.widget.utils.SimpleViewHolderStrategy.java
com.raizlabs.widget.utils.ViewHolderStrategyConverter.java
com.raizlabs.widget.utils.ViewHolderStrategyUtils.java
com.raizlabs.widget.utils.ViewHolderStrategy.java