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

org.multiverse.templates.OrElseTemplate Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
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(); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy