package org.realityforge.replicant.client.runtime;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import org.realityforge.replicant.client.Channel;

public final class AreaOfInterestListenerSupport
  implements AreaOfInterestListener
{
  private final ArrayList<AreaOfInterestListener> _listeners = new ArrayList<>();
  private final List<AreaOfInterestListener> _roListeners = Collections.unmodifiableList( _listeners );

  public synchronized boolean addListener( @Nonnull final AreaOfInterestListener listener )
  {
    if ( !_listeners.contains( listener ) )
    {
      _listeners.add( listener );
      return true;
    }
    else
    {
      return false;
    }
  }

  public synchronized boolean removeListener( @Nonnull final AreaOfInterestListener listener )
  {
    return _listeners.remove( listener );
  }

  @Nonnull
  public List<AreaOfInterestListener> getListeners()
  {
    return _roListeners;
  }

  @Override
  public void channelCreated( @Nonnull final Channel channel )
  {
    cloneListeners().forEach( l -> l.channelCreated( channel ) );
  }

  @Override
  public void channelUpdated( @Nonnull final Channel channel )
  {
    cloneListeners().forEach( l -> l.channelUpdated( channel ) );
  }

  @Override
  public void channelDeleted( @Nonnull final Channel channel )
  {
    cloneListeners().forEach( l -> l.channelDeleted( channel ) );
  }

  /**
   * Return a copy of the listeners.
   * This avoids concurrent operation exceptions.
   */
  @Nonnull
  protected synchronized ArrayList<AreaOfInterestListener> cloneListeners()
  {
    final ArrayList<AreaOfInterestListener> listeners = new ArrayList<>( _listeners.size() );
    listeners.addAll( _listeners );
    return listeners;
  }
}

@@ -210,23 +210,16 @@
 annel;%0A%0A
-public 
 final cl
@@ -252,16 +252,16 @@
 Support%0A
+
   implem
@@ -475,31 +475,24 @@
 eners );%0A%0A  
-public 
 synchronized
@@ -723,23 +723,16 @@
 %0A  %7D%0A%0A  
-public 
 synchron
@@ -873,23 +873,16 @@
 nnull%0A  
-public 
 List%3CAre
@@ -1515,20 +1515,10 @@
 ull%0A
+
   
-protected 
 sync

Convert class to package access