org.multiverse.templates.OrElseTemplate Maven / Gradle / Ivy
package org.multiverse.templates;
import org.multiverse.api.Transaction;
import org.multiverse.api.exceptions.RetryError;
import static org.multiverse.api.ThreadLocalTransaction.getThreadLocalTransaction;
/**
* A template for the 'orelse' functionality. Example:
*
* String item = new OrElseTemplate(){
*
* String run(Transaction t){
* return stack1.pop();
* }
*
* String orelserun(Transaction t){
* return stack2.pop();
* }
* }.execute();
*
*
* If an exception is thrown in the run block, the block is ended but the
* orelse block is not started and the exception is propagated.
*
* Does not start a transaction if no transaction is found.
*
* @author Peter Veentjer.
* @param
*/
public abstract class OrElseTemplate {
private final Transaction t;
/**
* Creates a OrElseTemplate using the transaction in the getThreadLocalTransaction.
*
* @throws NullPointerException if no transaction is found.
*/
public OrElseTemplate() {
this(getThreadLocalTransaction());
}
/**
* Creates an OrElseTemplate using the provided transaction.
*
* @param t the provided transaction.
* @throws NullPointerException if is null.
*/
public OrElseTemplate(Transaction t) {
if (t == null) throw new NullPointerException();
this.t = t;
}
public abstract E run(Transaction t);
public abstract E orelserun(Transaction t);
public final E execute() {
t.startOr();
boolean endOr = true;
try {
return run(t);
} catch (RetryError e) {
endOr = false;
t.endOrAndStartElse();
return orelserun(t);
} finally {
if (endOr) {
t.endOr();
}
}
}
}