com.helger.commons.callback.CallbackList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ph-commons Show documentation
Show all versions of ph-commons Show documentation
Java 1.6+ Library with tons of utility classes required in all projects
/**
* Copyright (C) 2014-2016 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.function.Consumer;
import java.util.function.Function;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.collection.ext.CommonsLinkedHashSet;
import com.helger.commons.collection.ext.ICommonsList;
import com.helger.commons.collection.ext.ICommonsOrderedSet;
import com.helger.commons.concurrent.SimpleReadWriteLock;
import com.helger.commons.lang.ICloneable;
import com.helger.commons.state.EChange;
import com.helger.commons.state.EContinue;
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 static final Logger s_aLogger = LoggerFactory.getLogger (CallbackList.class);
private final SimpleReadWriteLock m_aRWLock = new SimpleReadWriteLock ();
@GuardedBy ("m_aRWLock")
private final ICommonsOrderedSet m_aCallbacks = new CommonsLinkedHashSet<> ();
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.writeLocked ( () -> m_aCallbacks.add (aCallback));
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;
return m_aRWLock.writeLocked ( () -> m_aCallbacks.removeObject (aCallback));
}
/**
* Remove all callbacks
*
* @return {@link EChange}
*/
@Nonnull
public EChange removeAllCallbacks ()
{
return m_aRWLock.writeLocked ( () -> m_aCallbacks.removeAll ());
}
@Nonnull
@ReturnsMutableCopy
public ICommonsList getAllCallbacks ()
{
return m_aRWLock.readLocked ( () -> m_aCallbacks.getCopyAsList ());
}
@Nullable
public CALLBACKTYPE getCallbackAtIndex (@Nonnegative final int nIndex)
{
return m_aRWLock.readLocked ( () -> m_aCallbacks.getAtIndex (nIndex));
}
@Nonnegative
public int getCallbackCount ()
{
return m_aRWLock.readLocked ( () -> m_aCallbacks.size ());
}
public boolean hasCallbacks ()
{
return m_aRWLock.readLocked ( () -> m_aCallbacks.isNotEmpty ());
}
@Nonnull
public CallbackList getClone ()
{
return m_aRWLock.readLocked ( () -> new CallbackList<> (this));
}
public void forEach (@Nonnull final Consumer aConsumer)
{
for (final CALLBACKTYPE aCallback : getAllCallbacks ())
try
{
aConsumer.accept (aCallback);
}
catch (final Throwable t)
{
s_aLogger.error ("Failed to invoke callback " + aCallback, t);
}
}
@Nonnull
public EContinue forEachWithReturn (@Nonnull final Function aFunction)
{
for (final CALLBACKTYPE aCallback : getAllCallbacks ())
try
{
if (aFunction.apply (aCallback).isBreak ())
return EContinue.BREAK;
}
catch (final Throwable t)
{
s_aLogger.error ("Failed to invoke callback " + aCallback, t);
}
return EContinue.CONTINUE;
}
@Override
public String toString ()
{
return new ToStringGenerator (this).append ("callbacks", m_aCallbacks).toString ();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy