fr.dyade.aaa.agent.CountDownCallback Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a3-rt Show documentation
Show all versions of a3-rt Show documentation
Builds the Joram a3 rt project.
/*
* Copyright (C) 2013 - 2023 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package fr.dyade.aaa.agent;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Callback to be invoked once a countdown has reached the value zero.
* The countdown is incremented when the CountDownCallback
is
* assigned to a CallbackNotification
and decremented after the
* CallbackNotification
has been successfully processed, or after
* an error has been raised.
*/
public class CountDownCallback {
private AtomicInteger countDown;
private List errors;
private Callback callback;
/**
* Creates a callback by specifying the
* Callback
to invoke.
* Not permitted in an Agent
.
* @param callback the callback to invoke
* @throws RuntimeException if called by an Agent
*/
public CountDownCallback(Callback callback) {
super();
if (AgentServer.isEngineThread()) {
throw new RuntimeException(
"Not permitted: the current thread belongs to the Engine");
}
this.callback = callback;
countDown = new AtomicInteger();
}
void incrementAndGet() {
countDown.incrementAndGet();
}
void done() {
int count = countDown.decrementAndGet();
if (count == 0) {
if (errors != null) {
callback.failed(errors);
} else {
callback.done();
}
}
}
void failed(Throwable error) {
if (errors == null) {
errors = new ArrayList<>(countDown.get());
}
errors.add(error);
int count = countDown.decrementAndGet();
if (count == 0) {
callback.failed(errors);
}
}
}