Class Mode<E extends Exception>

java.lang.Object
ch.tocco.nice2.persist.core.api.exec.Mode<E>

public final class Mode<E extends Exception> extends Object
Describes the execution of a task; such as
  • whether the task is executed in a transaction
  • can control exception handling of PersistException. It will catch the checked exceptions *and* PersistExceptions thrown by tasks and wrap it using the exception supplied with the mode. Other `RuntimeException`s are propagated, and a log message is printed.
  • whether the task is executed in privileged mode
  • which business-unit to use
  • ...

The first two are provided by this class. The security module is adding the third option, the privileged mode.

Other options can be easily added by creating a new Mode.Key constant and adding values to the mode object. This may be useful to hand parameters to the task itself, or by contributing a persist.spi.ModeWrapper to control the execution environment of a task.

  • Method Details

    • key

      public static <T> Mode.Key<T> key(String name, Class<T> type)
      Creates a new key of the specified name and type.
    • key

      public static <T> Mode.Key<T> key(String name, com.google.common.reflect.TypeToken<T> type)
    • create

      public static Mode<PersistException> create()
    • createTransactional

      public static Mode<PersistException> createTransactional()
    • exception

      public static <E extends Exception> Mode<E> exception(Class<E> exceptionClass)
    • wrapIn

      public <T extends Exception> Mode<T> wrapIn(Class<T> exceptionClass)
      Wrap persist exceptions into the specified one.
    • unwrap

      public Mode<PersistException> unwrap()
    • isTransactional

      public boolean isTransactional()
    • getTxPriority

      public Optional<Priority> getTxPriority()
    • get

      public <V> Optional<V> get(Mode.Key<V> key)
    • getPriority

      public Optional<Priority> getPriority(Mode.Key<?> key)
    • getExceptionMapper

      public ExceptionMapper<E> getExceptionMapper()
    • is

      public boolean is(Mode.Key<?> key)
      Returns true if the given key exists and does not map to a boolean value of false.
    • with

      public <T> Mode<E> with(Mode.Key<T> key, @Nullable T value)
      Creates a new mode object of the current list of options together with the given one.

      If value is null the option key is removed (same as calling without(Key).

    • with

      public <T> Mode<E> with(Mode.Key<T> key, @Nullable T value, Priority p)
    • with

      public Mode<E> with(Mode.Key<Boolean> key)
      Short for with(Key, true)
    • with

      public Mode<E> with(Mode.Key<Boolean> key, Priority p)
    • without

      public Mode<E> without(Mode.Key<?> key)
      Removes the given key from the list of options.
    • withinTx

      public Mode<E> withinTx()
      Specifies to execute a task within a transaction. The default priority is Priority.LOWER.
    • withinTx

      public Mode<E> withinTx(Priority p)
    • withinNewContext

      public Mode<E> withinNewContext()
    • noTx

      public Mode<E> noTx()
      Specifies to not use a transaction when executing the task.
    • use

      public Mode<E> use(Context context)
      Specifies to use the given context when executing the task. In some (rare) cases it can be desired to hand in a concrete context. Most usually, it is better to use the current context, which is the default behaviour.

      Can be reverted by defaultContext().

    • use

      public Mode<E> use(Context context, Priority p)
    • getContextPriority

      public Optional<Priority> getContextPriority()
    • defaultContext

      public Mode<E> defaultContext()
      Specifies explicitely to use the current context (the default).
    • isSubmitted

      public boolean isSubmitted()
      See Also:
    • setSubmittedFlag

      public Mode<E> setSubmittedFlag()
      This flag is used internally to mark execution of a task in a different thread. It is set by CommandExecutor if a task has been submitted
    • context

      public Optional<Context> context()
      Returns the context, if specified with use(Context)
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object