org.jgroups.util.MatchingPromise Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
package org.jgroups.util;
import java.util.Objects;
/**
* A promise which only sets the result if it matches the expected result
* @author Bela Ban
* @since 4.0.16
*/
public class MatchingPromise extends Promise {
protected T expected_result;
public MatchingPromise(T expected_result) {
this.expected_result=expected_result;
}
public T getExpectedResult() {
return expected_result;
}
/** Sets the result only if expected_result matches result */
public void setResult(T result) {
lock.lock();
try {
if(Objects.equals(expected_result, result))
super.setResult(result);
}
finally {
lock.unlock();
}
}
public void reset(T expected_result) {
lock.lock();
try {
this.expected_result=expected_result;
super.reset(true);
}
finally {
lock.unlock();
}
}
public void reset(T expected_result, boolean signal) {
lock.lock();
try {
this.expected_result=expected_result;
super.reset(signal);
}
finally {
lock.unlock();
}
}
public String toString() {
return super.toString() + String.format(" (expected result: %s)", expected_result);
}
}