Class StmUtils

java.lang.Object
org.multiverse.api.StmUtils

public final class StmUtils extends Object
A utility class with convenience methods to access the Stm or Txn. These methods can be imported using the static import for a less ugly syntax.
  • Field Details

    • refFactory

      private static final TxnRefFactory refFactory
    • defaultTxnExecutor

      private static final TxnExecutor defaultTxnExecutor
    • orelseBlock

      private static final OrElseBlock orelseBlock
    • txnCollectionsFactory

      private static final TxnCollectionsFactory txnCollectionsFactory
  • Constructor Details

    • StmUtils

      private StmUtils()
  • Method Details

    • newTxnLinkedList

      public static <E> TxnList<E> newTxnLinkedList()
      Creates a new committed TxnList based on a double linked list.
      Returns:
      the created TxnList.
    • newTxnStack

      public static <E> TxnStack<E> newTxnStack()
      Creates a new committed unbound TxnStack.
      Returns:
      the created TxnStack.
    • newTxnStack

      public static <E> TxnStack<E> newTxnStack(int capacity)
      Creates a new committed bound TxnStack.
      Parameters:
      capacity - the maximum capacity of the stack. Integer.MAX_VALUE indicates that there is no bound.
      Returns:
      the create TxnStack
      Throws:
      IllegalArgumentException - if capacity smaller than 0.
    • newTxnQueue

      public static <E> TxnQueue<E> newTxnQueue()
      Creates a new committed unbound TxnQueue.
      Returns:
      the created TxnQueue.
    • newTxnQueue

      public static <E> TxnQueue<E> newTxnQueue(int capacity)
      Creates a new committed bound TxnQueue.
      Parameters:
      capacity - the maximum capacity of the queue. Integer.MAX_VALUE indicates that there is no bound.
      Returns:
      the created TxnQueue
      Throws:
      IllegalArgumentException - if capacity smaller than 0.
    • newTxnDeque

      public static <E> TxnDeque<E> newTxnDeque()
      Creates a new committed unbound TxnDeque.
      Returns:
      the created TxnDeque
    • newTxnDeque

      public static <E> TxnDeque<E> newTxnDeque(int capacity)
      Creates a new committed bound TxnDeque.
      Parameters:
      capacity - the maximum capacity of the deque. Integer.MAX_VALUE indicates that there is no bound.
      Returns:
      the created TxnDeque.
      Throws:
      IllegalArgumentException - if capacity is smaller than 0.
    • newTxnHashSet

      public static <E> TxnSet<E> newTxnHashSet()
      Creates a new committed TxnSet that is based on a 'hashtable'.
      Returns:
      the created TxnSet.
    • newTxnHashMap

      public static <K, V> TxnMap<K,V> newTxnHashMap()
      Creates a new committed TxnMap.
      Returns:
      the created TxnMap
    • atomic

      public static void atomic(Runnable runnable)
    • atomic

      public static <E> E atomic(Callable<E> callable)
    • atomic

      public static <E> E atomic(TxnCallable<E> callable)
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      This method doesn't throw a checked exception, but if the callable does, it is wrapped inside an InvisibleCheckedException.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The callable TxnCallable to execute.
      Returns:
      the result of the execution
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if the callable throws a checked exception.
    • atomicChecked

      public static <E> E atomicChecked(TxnCallable<E> callable) throws Exception
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The TxnCallable to execute.
      Returns:
      the result of the execution
      Throws:
      NullPointerException - if callable is null.
      Exception - is the callable throws an Exception
    • atomic

      public static <E> E atomic(TxnCallable<E> either, TxnCallable<E> orelse)
      Executes the either block, or in case of a retry, the orelse block is executed. If in the execution of the callable a checked exception is thrown, the exception is wrapped in a InvisibleCheckedException. The original exception can be retrieved by calling the getCause method.
      Parameters:
      either - the either block
      orelse - the orelse block.
      Returns:
      the result of the execution.
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if a checked exception is thrown by the callable.
    • atomicChecked

      public static <E> E atomicChecked(TxnCallable<E> either, TxnCallable<E> orelse) throws Exception
      Executes the either block, or in case of a retry, the orelse block is executed.
      Parameters:
      either - the either block
      orelse - the orelse block
      Returns:
      the result of the execution.
      Throws:
      NullPointerException - if callable is null.
      Exception - if the execute call fails.
    • atomic

      public static int atomic(TxnIntCallable callable)
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      This method doesn't throw a checked exception, but if the callable does, it is wrapped inside an InvisibleCheckedException.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The callable TxnIntCallable to execute.
      Returns:
      the result of the execution
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if the callable throws a checked exception.
    • atomicChecked

      public static int atomicChecked(TxnIntCallable callable) throws Exception
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The TxnIntCallable to execute.
      Returns:
      the result of the execution
      Throws:
      NullPointerException - if callable is null.
      Exception - is the callable throws an Exception
    • atomic

      public static int atomic(TxnIntCallable either, TxnIntCallable orelse)
      Executes the either block, or in case of a retry, the orelse block is executed. If in the execution of the callable a checked exception is thrown, the exception is wrapped in a InvisibleCheckedException. The original exception can be retrieved by calling the getCause method.
      Parameters:
      either - the either block
      orelse - the orelse block.
      Returns:
      the result of the execution.
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if a checked exception is thrown by the callable.
    • atomicChecked

      public static int atomicChecked(TxnIntCallable either, TxnIntCallable orelse) throws Exception
      Executes the either block, or in case of a retry, the orelse block is executed.
      Parameters:
      either - the either block
      orelse - the orelse block
      Returns:
      the result of the execution.
      Throws:
      NullPointerException - if callable is null.
      Exception - if the execute call fails.
    • atomic

      public static long atomic(TxnLongCallable callable)
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      This method doesn't throw a checked exception, but if the callable does, it is wrapped inside an InvisibleCheckedException.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The callable TxnLongCallable to execute.
      Returns:
      the result of the execution
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if the callable throws a checked exception.
    • atomicChecked

      public static long atomicChecked(TxnLongCallable callable) throws Exception
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The TxnLongCallable to execute.
      Returns:
      the result of the execution
      Throws:
      NullPointerException - if callable is null.
      Exception - is the callable throws an Exception
    • atomic

      public static long atomic(TxnLongCallable either, TxnLongCallable orelse)
      Executes the either block, or in case of a retry, the orelse block is executed. If in the execution of the callable a checked exception is thrown, the exception is wrapped in a InvisibleCheckedException. The original exception can be retrieved by calling the getCause method.
      Parameters:
      either - the either block
      orelse - the orelse block.
      Returns:
      the result of the execution.
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if a checked exception is thrown by the callable.
    • atomicChecked

      public static long atomicChecked(TxnLongCallable either, TxnLongCallable orelse) throws Exception
      Executes the either block, or in case of a retry, the orelse block is executed.
      Parameters:
      either - the either block
      orelse - the orelse block
      Returns:
      the result of the execution.
      Throws:
      NullPointerException - if callable is null.
      Exception - if the execute call fails.
    • atomic

      public static double atomic(TxnDoubleCallable callable)
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      This method doesn't throw a checked exception, but if the callable does, it is wrapped inside an InvisibleCheckedException.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The callable TxnDoubleCallable to execute.
      Returns:
      the result of the execution
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if the callable throws a checked exception.
    • atomicChecked

      public static double atomicChecked(TxnDoubleCallable callable) throws Exception
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The TxnDoubleCallable to execute.
      Returns:
      the result of the execution
      Throws:
      NullPointerException - if callable is null.
      Exception - is the callable throws an Exception
    • atomic

      public static double atomic(TxnDoubleCallable either, TxnDoubleCallable orelse)
      Executes the either block, or in case of a retry, the orelse block is executed. If in the execution of the callable a checked exception is thrown, the exception is wrapped in a InvisibleCheckedException. The original exception can be retrieved by calling the getCause method.
      Parameters:
      either - the either block
      orelse - the orelse block.
      Returns:
      the result of the execution.
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if a checked exception is thrown by the callable.
    • atomicChecked

      public static double atomicChecked(TxnDoubleCallable either, TxnDoubleCallable orelse) throws Exception
      Executes the either block, or in case of a retry, the orelse block is executed.
      Parameters:
      either - the either block
      orelse - the orelse block
      Returns:
      the result of the execution.
      Throws:
      NullPointerException - if callable is null.
      Exception - if the execute call fails.
    • atomic

      public static boolean atomic(TxnBooleanCallable callable)
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      This method doesn't throw a checked exception, but if the callable does, it is wrapped inside an InvisibleCheckedException.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The callable TxnBooleanCallable to execute.
      Returns:
      the result of the execution
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if the callable throws a checked exception.
    • atomicChecked

      public static boolean atomicChecked(TxnBooleanCallable callable) throws Exception
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The TxnBooleanCallable to execute.
      Returns:
      the result of the execution
      Throws:
      NullPointerException - if callable is null.
      Exception - is the callable throws an Exception
    • atomic

      public static boolean atomic(TxnBooleanCallable either, TxnBooleanCallable orelse)
      Executes the either block, or in case of a retry, the orelse block is executed. If in the execution of the callable a checked exception is thrown, the exception is wrapped in a InvisibleCheckedException. The original exception can be retrieved by calling the getCause method.
      Parameters:
      either - the either block
      orelse - the orelse block.
      Returns:
      the result of the execution.
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if a checked exception is thrown by the callable.
    • atomicChecked

      public static boolean atomicChecked(TxnBooleanCallable either, TxnBooleanCallable orelse) throws Exception
      Executes the either block, or in case of a retry, the orelse block is executed.
      Parameters:
      either - the either block
      orelse - the orelse block
      Returns:
      the result of the execution.
      Throws:
      NullPointerException - if callable is null.
      Exception - if the execute call fails.
    • atomic

      public static void atomic(TxnVoidCallable callable)
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      This method doesn't throw a checked exception, but if the callable does, it is wrapped inside an InvisibleCheckedException.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The callable TxnVoidCallable to execute.
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if the callable throws a checked exception.
    • atomicChecked

      public static void atomicChecked(TxnVoidCallable callable) throws Exception
      Executes the callable transactionally on the GlobalStmInstance using the default TxnExecutor. If a Transaction already is active on the TxnThreadLocal, this transaction will lift on that transaction (so the propagation level is Requires) and will not commit this transaction.

      If you want to get most out of performance, it is best to make use of a customized TxnExecutor instead of relying on the default TxnExecutor that will always provide the most expensive transaction available.

      Parameters:
      callable - The TxnVoidCallable to execute.
      Throws:
      NullPointerException - if callable is null.
      Exception - is the callable throws an Exception
    • atomic

      public static void atomic(TxnVoidCallable either, TxnVoidCallable orelse)
      Executes the either block, or in case of a retry, the orelse block is executed. If in the execution of the callable a checked exception is thrown, the exception is wrapped in a InvisibleCheckedException. The original exception can be retrieved by calling the getCause method.
      Parameters:
      either - the either block
      orelse - the orelse block.
      Throws:
      NullPointerException - if callable is null.
      InvisibleCheckedException - if a checked exception is thrown by the callable.
    • atomicChecked

      public static void atomicChecked(TxnVoidCallable either, TxnVoidCallable orelse) throws Exception
      Executes the either block, or in case of a retry, the orelse block is executed.
      Parameters:
      either - the either block
      orelse - the orelse block
      Throws:
      NullPointerException - if callable is null.
      Exception - if the execute call fails.
    • newTxnInteger

      public static TxnInteger newTxnInteger(int value)
      Creates a committed TxnInteger with the provided value using the GlobalStmInstance.
      Parameters:
      value - the initial value of the TnxInteger
      Returns:
      the created TnxInteger.
    • newTxnInteger

      public static TxnInteger newTxnInteger()
      Creates a committed TxnInteger with 0 as initial value using the GlobalStmInstance.
      Returns:
      the created TxnInteger.
    • newTxnLong

      public static TxnLong newTxnLong()
      Creates a committed TxnLong with 0 as initial value using the GlobalStmInstance.
      Returns:
      the created TnxLong.
    • newTxnLong

      public static TxnLong newTxnLong(long value)
      Creates a committed TxnLong with the provided value using the GlobalStmInstance.
      Parameters:
      value - the initial value of the TxnLong.
      Returns:
      the created TnxLong.
    • newTxnDouble

      public static TxnDouble newTxnDouble()
      Creates a committed TxnDouble with 0 as initial value using the GlobalStmInstance.
      Returns:
      the created TxnDouble.
    • newTxnDouble

      public static TxnDouble newTxnDouble(double value)
      Creates a committed TxnDouble with the provided value using the GlobalStmInstance.
      Parameters:
      value - the initial value.
      Returns:
      the created TnxDouble.
    • newTxnBoolean

      public static TxnBoolean newTxnBoolean()
      Creates a committed TxnBoolean with false as initial value using the GlobalStmInstance.
      Returns:
      the created TxnBoolean.
    • newTxnBoolean

      public static TxnBoolean newTxnBoolean(boolean value)
      Creates a committed TxnBoolean with the provided value using the GlobalStmInstance.
      Parameters:
      value - the initial value
      Returns:
      the created TxnBoolean.
    • newTxnRef

      public static <E> TxnRef<E> newTxnRef()
      Creates a committed TxnRef with null as initial value using the GlobalStmInstance.
      Type Parameters:
      E - the type of the TxnRef.
      Returns:
      the created Ref.
    • newTxnRef

      public static <E> TxnRef<E> newTxnRef(E value)
      Creates a committed TxnRef with the provided value using the GlobalStmInstance.
      Type Parameters:
      E - the type of the TxnRef.
      Parameters:
      value - the initial value of the TxnRef.
      Returns:
      the created TxnRef.
    • retry

      public static void retry()
      Does a retry. This behavior is needed for blocking transactions; transaction that wait for a state change to happen on certain datastructures, e.g. an item to come available on a transactional blocking queue.

      Under the hood the retry throws an Retry that will be caught up the callstack (by the TxnExecutor for example). The Retry should not be caught by user code in almost all cases.

    • prepare

      public static void prepare()
      Prepares the Transaction in the TxnThreadLocal transaction.

      For more information see Txn.prepare().

      Throws:
      TxnMandatoryException - if no active transaction is found.
      IllegalTransactionStateException - if the active transaction is not in the correct state for this operation.
      ControlFlowError
    • abort

      public static void abort()
      Aborts the Transaction in the TxnThreadLocal transaction.

      For more information see Txn.abort().

      Throws:
      TxnMandatoryException - if no active transaction is found.
      IllegalTransactionStateException - if the active transaction is not in the correct state for this operation.
      ControlFlowError
    • commit

      public static void commit()
      Commits the Transaction in the TxnThreadLocal transaction.

      For more information see Txn.commit().

      Throws:
      TxnMandatoryException - if no active transaction is found.
      IllegalTransactionStateException - if the active transaction is not in the correct state for this operation.
      ControlFlowError
    • scheduleCompensatingOrDeferredTask

      public static void scheduleCompensatingOrDeferredTask(Runnable task)
      Scheduled an deferred or compensating task on the Txn in the TxnThreadLocal. This task is executed after the transaction commits or aborts.
      Parameters:
      task - the deferred task to execute.
      Throws:
      NullPointerException - if task is null.
      TxnMandatoryException - if no transaction is set on the TxnThreadLocal.
      IllegalTxnStateException - if the transaction is not in the correct state to accept a compensating or deferred task.
    • scheduleDeferredTask

      public static void scheduleDeferredTask(Runnable task)
      Scheduled an deferred task on the Txn in the TxnThreadLocal. This task is executed after the transaction commits and one of the use cases is starting transactions.
      Parameters:
      task - the deferred task to execute.
      Throws:
      NullPointerException - if task is null.
      TxnMandatoryException - if no transaction is set on the TxnThreadLocal.
      IllegalTxnStateException - if the transaction is not in the correct state to accept a deferred task.
    • scheduleCompensatingTask

      public static void scheduleCompensatingTask(Runnable task)
      Scheduled a compensating task on the Txn in the TxnThreadLocal. This task is executed after the transaction aborts and one of the use cases is cleaning up non transaction resources like the file system.
      Parameters:
      task - the deferred task to execute.
      Throws:
      NullPointerException - if task is null.
      TxnMandatoryException - if no transaction is set on the TxnThreadLocal.
      IllegalTxnStateException - if the transaction is not in the correct state to accept a compensating task.