Class TimeGuard


  • public final class TimeGuard
    extends java.lang.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  TimeGuard.TimeAlertListener
      Interface for any object to be informed about time alerts.
      static class  TimeGuard.TimeData
      Data container for time watching action.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private TimeGuard()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addGuard​(java.lang.String alertMessage, long maxAllowedDelayInMilliseconds)
      Add a time watcher.
      static void addGuard​(java.lang.String alertMessage, long maxAllowedDelayInMilliseconds, TimeGuard.TimeAlertListener timeAlertListener)
      Add a time watcher and provide processor of time violation.
      static void addPoint​(java.lang.String timePointName, TimeGuard.TimeAlertListener listener)
      Add a named time point.
      static void cancel()
      Cancel all time watchers and time points for the current stack level.
      static void cancelAll()
      Cancel all time watchers and time points globally for the current thread.
      static void check()
      Check all registered time watchers for time bound violations.
      static void checkPoint​(java.lang.String timePointName)
      Check named time point(s).
      static void checkPoints()
      Process all time points for the current stack level.
      static boolean isEmpty()
      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 Detail

      • 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
    • Constructor Detail

      • TimeGuard

        private TimeGuard()
    • Method Detail

      • addGuard

        @Weight(value=VARIABLE,
                comment="Depends on the current call stack depth")
        public static void addGuard​(java.lang.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:
        check(), cancelAll(), MetaErrorListeners
      • checkPoint

        @Weight(value=VARIABLE,
                comment="Depends on the current call stack depth")
        public static void checkPoint​(java.lang.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​(java.lang.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:
        check(), cancelAll()
      • 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()
      • 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:
        cancelAll()
      • 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