Class TimeGuard

java.lang.Object
com.igormaznitsa.meta.common.utils.TimeGuard

public final class TimeGuard extends Object
Allows to detect violations of execution time for code blocks or just measure time for them. It works separately for every Thread through ThreadLocal and check stack depth to be informed about current operation level.
Since:
1.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Interface for any object to be informed about time alerts.
    static final class 
    Data container for time watching action.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Some variant of "null-device" for time alerts, it does absolutely nothing.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addGuard(String alertMessage, long maxAllowedDelayInMilliseconds)
    Add a time watcher.
    static void
    addGuard(String alertMessage, long maxAllowedDelayInMilliseconds, TimeGuard.TimeAlertListener timeAlertListener)
    Add a time watcher and provide processor of time violation.
    static void
    addPoint(String timePointName, TimeGuard.TimeAlertListener listener)
    Add a named time point.
    static void
    Cancel all time watchers and time points for the current stack level.
    static void
    Cancel all time watchers and time points globally for the current thread.
    static void
    Check all registered time watchers for time bound violations.
    static void
    checkPoint(String timePointName)
    Check named time point(s).
    static void
    Process all time points for the current stack level.
    static boolean
    Check that the thread local for the current thread contains time points or watchers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • NULL_TIME_ALERT_LISTENER

      public static final TimeGuard.TimeAlertListener NULL_TIME_ALERT_LISTENER
      Some variant of "null-device" for time alerts, it does absolutely nothing.
      Since:
      1.0
  • Method Details

    • addGuard

      @Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void addGuard(String alertMessage, @Constraint("X>0") long maxAllowedDelayInMilliseconds)
      Add a time watcher. As target of notification meta error listeners will be used.
      Parameters:
      alertMessage - message for time violation
      maxAllowedDelayInMilliseconds - max allowed delay in milliseconds for executing block
      Since:
      1.0
      See Also:
    • addPoint

      @Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void addPoint(String timePointName, TimeGuard.TimeAlertListener listener)
      Add a named time point.
      Parameters:
      timePointName - name for the time point
      listener - listener to be notified
      Since:
      1.0
      See Also:
    • checkPoint

      @Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void checkPoint(String timePointName)
      Check named time point(s). Listener registered for the point will be notified and the point will be removed.
      Parameters:
      timePointName - the name of time point
      Since:
      1.0
    • checkPoints

      @Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void checkPoints()
      Process all time points for the current stack level.
      Since:
      1.0
    • addGuard

      @Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void addGuard(String alertMessage, @Constraint("X>0") long maxAllowedDelayInMilliseconds, TimeGuard.TimeAlertListener timeAlertListener)
      Add a time watcher and provide processor of time violation.
      Parameters:
      alertMessage - message for time violation
      maxAllowedDelayInMilliseconds - max allowed delay in milliseconds for executing block
      timeAlertListener - alert listener to be notified, if it is null then the global one will get notification
      Since:
      1.0
      See Also:
    • cancelAll

      @Weight(NORMAL) public static void cancelAll()
      Cancel all time watchers and time points globally for the current thread.
      Since:
      1.0
      See Also:
    • cancel

      @Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void cancel()
      Cancel all time watchers and time points for the current stack level.
      Since:
      1.0
      See Also:
    • check

      @Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void check()
      Check all registered time watchers for time bound violations.
      Since:
      1.0
      See Also:
    • isEmpty

      @Weight(value=NORMAL, comment="May create list in thread local storage") public static boolean isEmpty()
      Check that the thread local for the current thread contains time points or watchers.
      Returns:
      true if the thread local storage is empty, false otherwise