Class TimeGuard
- java.lang.Object
-
- com.igormaznitsa.meta.common.utils.TimeGuard
-
public final class TimeGuard extends java.lang.ObjectAllows 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 interfaceTimeGuard.TimeAlertListenerInterface for any object to be informed about time alerts.static classTimeGuard.TimeDataData container for time watching action.
-
Field Summary
Fields Modifier and Type Field Description static TimeGuard.TimeAlertListenerNULL_TIME_ALERT_LISTENERSome variant of "null-device" for time alerts, it does absolutely nothing.private static java.lang.ThreadLocal<java.util.List<TimeGuard.TimeData>>REGISTRYInside thread local storage of registered processors.
-
Constructor Summary
Constructors Modifier Constructor Description privateTimeGuard()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddGuard(java.lang.String alertMessage, long maxAllowedDelayInMilliseconds)Add a time watcher.static voidaddGuard(java.lang.String alertMessage, long maxAllowedDelayInMilliseconds, TimeGuard.TimeAlertListener timeAlertListener)Add a time watcher and provide processor of time violation.static voidaddPoint(java.lang.String timePointName, TimeGuard.TimeAlertListener listener)Add a named time point.static voidcancel()Cancel all time watchers and time points for the current stack level.static voidcancelAll()Cancel all time watchers and time points globally for the current thread.static voidcheck()Check all registered time watchers for time bound violations.static voidcheckPoint(java.lang.String timePointName)Check named time point(s).static voidcheckPoints()Process all time points for the current stack level.static booleanisEmpty()Check that the thread local for the current thread contains time points or watchers.
-
-
-
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
-
REGISTRY
@MustNotContainNull private static final java.lang.ThreadLocal<java.util.List<TimeGuard.TimeData>> REGISTRY
Inside thread local storage of registered processors.- Since:
- 1.0
-
-
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 violationmaxAllowedDelayInMilliseconds- max allowed delay in milliseconds for executing block- Since:
- 1.0
- See Also:
check(),cancelAll(),MetaErrorListeners
-
addPoint
@Weight(value=VARIABLE, comment="Depends on the current call stack depth") public static void addPoint(java.lang.String timePointName, TimeGuard.TimeAlertListener listener)
Add a named time point.- Parameters:
timePointName- name for the time pointlistener- listener to be notified- Since:
- 1.0
- See Also:
checkPoint(java.lang.String)
-
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 violationmaxAllowedDelayInMilliseconds- max allowed delay in milliseconds for executing blocktimeAlertListener- 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()
-
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.
-
-