This class provides a centralized mechanism for counting security violations, enabling monitoring, alerting, and security analytics. It uses atomic operations and concurrent collections to ensure thread safety without locks.
Design Principles
- Thread Safety - All operations are atomic and thread-safe
- Lock-Free - Uses lock-free data structures for performance
- Memory Efficient - Only allocates counters for observed failure types
- Non-Blocking - All operations complete in constant time
Usage Examples
// Create event counter SecurityEventCounter counter = new SecurityEventCounter(); // Increment counters for different failure types long pathTraversalCount = counter.increment(UrlSecurityFailureType.PATH_TRAVERSAL_DETECTED); long encodingCount = counter.increment(UrlSecurityFailureType.DOUBLE_ENCODING); // Query current counts long currentCount = counter.getCount(UrlSecurityFailureType.PATH_TRAVERSAL_DETECTED); // Get all counts for reporting Map<UrlSecurityFailureType, Long> allCounts = counter.getAllCounts(); // Reset all counters counter.reset();
Concurrent Access
This class is designed for high-concurrency environments where multiple threads may be simultaneously incrementing counters for different or the same failure types. All operations are atomic and consistent.
Memory Characteristics
Counters are created lazily - only failure types that have been observed will
consume memory. This makes the class efficient even when dealing with the full
range of possible UrlSecurityFailureType values.
- Since:
- 1.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Completely clears all counters and removes failure types from tracking.Returns a snapshot of all current counts.longgetCount(UrlSecurityFailureType failureType) Returns the current count for the specified failure type.intReturns the number of distinct failure types that have been observed.longReturns the total count across all failure types.booleanChecks if any security events have been recorded at all.booleanhasEvents(UrlSecurityFailureType failureType) Checks if any events have been recorded for the specified failure type.longincrement(UrlSecurityFailureType failureType) Increments the counter for the specified failure type and returns the new count.longincrementBy(UrlSecurityFailureType failureType, long delta) Increments the counter for the specified failure type by the given delta.voidreset()Resets all counters to zero.voidreset(UrlSecurityFailureType failureType) Resets the counter for a specific failure type to zero.toString()Returns a string representation of the counter state.
-
Constructor Details
-
SecurityEventCounter
public SecurityEventCounter()
-
-
Method Details
-
increment
Increments the counter for the specified failure type and returns the new count.This operation is atomic and thread-safe. If this is the first time the failure type has been observed, a new counter will be created and initialized to 1.
- Parameters:
failureType- The type of security failure to increment. Must not be null.- Returns:
- The new count value after incrementing
- Throws:
NullPointerException- if failureType is null
-
incrementBy
Increments the counter for the specified failure type by the given delta.This operation is atomic and thread-safe. If this is the first time the failure type has been observed, a new counter will be created and initialized to the delta value.
- Parameters:
failureType- The type of security failure to increment. Must not be null.delta- The amount to add to the counter. Must be positive.- Returns:
- The new count value after incrementing
- Throws:
NullPointerException- if failureType is nullIllegalArgumentException- if delta is negative
-
getCount
Returns the current count for the specified failure type.Returns 0 if no events of this type have been recorded. This operation is thread-safe and returns a consistent snapshot of the counter value.
- Parameters:
failureType- The failure type to query. Must not be null.- Returns:
- The current count for the failure type, or 0 if no events recorded
- Throws:
NullPointerException- if failureType is null
-
getAllCounts
Returns a snapshot of all current counts.Returns an immutable map containing all observed failure types and their current counts. Only failure types with non-zero counts are included. This is useful for reporting and monitoring purposes.
- Returns:
- An immutable map of failure types to their current counts
-
getTotalCount
Returns the total count across all failure types.This method sums all individual counters to provide a total count of security events. Note that this is a snapshot in time and may not be consistent across concurrent modifications.
- Returns:
- The total count of all security events
-
getFailureTypeCount
Returns the number of distinct failure types that have been observed.This returns the number of different
UrlSecurityFailureTypevalues that have had at least one event recorded.- Returns:
- The number of distinct failure types with recorded events
-
hasEvents
Checks if any events have been recorded for the specified failure type.- Parameters:
failureType- The failure type to check. Must not be null.- Returns:
- true if at least one event has been recorded for this failure type
- Throws:
NullPointerException- if failureType is null
-
hasAnyEvents
Checks if any security events have been recorded at all.- Returns:
- true if any security events have been recorded
-
reset
Resets the counter for a specific failure type to zero.This atomically sets the counter for the specified failure type to zero. If no events have been recorded for this failure type, this operation has no effect.
- Parameters:
failureType- The failure type to reset. Must not be null.- Throws:
NullPointerException- if failureType is null
-
reset
Resets all counters to zero.This atomically resets all failure type counters to zero. The failure types remain in the internal map but with zero counts. This is useful for periodic reporting cycles where you want to start fresh counts.
-
clear
Completely clears all counters and removes failure types from tracking.This removes all failure types from the internal map, effectively returning the counter to its initial state. This is more aggressive than
reset()as it also frees the memory used by the counter objects. -
toString
Returns a string representation of the counter state.This includes the total count and the number of distinct failure types being tracked. It does not include detailed counts to avoid exposing potentially sensitive information.
-