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

com.helger.commons.callback.CallbackList Maven / Gradle / Ivy

There is a newer version: 5.0.12
Show newest version
/**
 * Copyright (C) 2014-2015 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.helger.commons.callback;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.collection.CollectionHelper;
import com.helger.commons.lang.ICloneable;
import com.helger.commons.state.EChange;
import com.helger.commons.string.ToStringGenerator;

/**
 * This class manages a list of callbacks.
 *
 * @author Philip Helger
 * @param 
 *        The callback type.
 */
@ThreadSafe
public class CallbackList  implements ICallbackList , ICloneable >
{
  private final ReadWriteLock m_aRWLock = new ReentrantReadWriteLock ();

  @GuardedBy ("m_aRWLock")
  private final List  m_aCallbacks = new ArrayList  ();

  public CallbackList ()
  {}

  public CallbackList (@Nonnull final CallbackList  aOther)
  {
    ValueEnforcer.notNull (aOther, "Other");
    m_aCallbacks.addAll (aOther.m_aCallbacks);
  }

  /**
   * Add a callback.
   *
   * @param aCallback
   *        May not be null.
   * @return this
   */
  @Nonnull
  public CallbackList  addCallback (@Nonnull final CALLBACKTYPE aCallback)
  {
    ValueEnforcer.notNull (aCallback, "Callback");

    m_aRWLock.writeLock ().lock ();
    try
    {
      m_aCallbacks.add (aCallback);
    }
    finally
    {
      m_aRWLock.writeLock ().unlock ();
    }
    return this;
  }

  /**
   * Remove the specified callback
   *
   * @param aCallback
   *        May be null.
   * @return {@link EChange}
   */
  @Nonnull
  public EChange removeCallback (@Nullable final CALLBACKTYPE aCallback)
  {
    if (aCallback == null)
      return EChange.UNCHANGED;

    m_aRWLock.writeLock ().lock ();
    try
    {
      return EChange.valueOf (m_aCallbacks.remove (aCallback));
    }
    finally
    {
      m_aRWLock.writeLock ().unlock ();
    }
  }

  /**
   * Remove all callbacks
   *
   * @return {@link EChange}
   */
  @Nonnull
  public EChange removeAllCallbacks ()
  {
    m_aRWLock.writeLock ().lock ();
    try
    {
      if (m_aCallbacks.isEmpty ())
        return EChange.UNCHANGED;
      m_aCallbacks.clear ();
      return EChange.CHANGED;
    }
    finally
    {
      m_aRWLock.writeLock ().unlock ();
    }
  }

  @Nonnull
  @ReturnsMutableCopy
  public List  getAllCallbacks ()
  {
    m_aRWLock.readLock ().lock ();
    try
    {
      return CollectionHelper.newList (m_aCallbacks);
    }
    finally
    {
      m_aRWLock.readLock ().unlock ();
    }
  }

  @Nullable
  public CALLBACKTYPE getCallbackAtIndex (@Nonnegative final int nIndex)
  {
    m_aRWLock.readLock ().lock ();
    try
    {
      return CollectionHelper.getSafe (m_aCallbacks, nIndex);
    }
    finally
    {
      m_aRWLock.readLock ().unlock ();
    }
  }

  @Nonnegative
  public int getCallbackCount ()
  {
    m_aRWLock.readLock ().lock ();
    try
    {
      return m_aCallbacks.size ();
    }
    finally
    {
      m_aRWLock.readLock ().unlock ();
    }
  }

  public boolean hasCallbacks ()
  {
    m_aRWLock.readLock ().lock ();
    try
    {
      return !m_aCallbacks.isEmpty ();
    }
    finally
    {
      m_aRWLock.readLock ().unlock ();
    }
  }

  @Nonnull
  public CallbackList  getClone ()
  {
    m_aRWLock.readLock ().lock ();
    try
    {
      return new CallbackList  (this);
    }
    finally
    {
      m_aRWLock.readLock ().unlock ();
    }
  }

  @Override
  public String toString ()
  {
    return new ToStringGenerator (this).append ("callbacks", m_aCallbacks).toString ();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy