Class StatisticsProvider
- java.lang.Object
-
- io.github.dhruv1110.jcachex.internal.util.StatisticsProvider
-
public final class StatisticsProvider extends Object
Utility class for common statistics handling patterns. This class provides static methods for creating and managing cache statistics, eliminating duplication across cache implementations.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double
calculateAverageLoadTime(long totalLoadTime, long loadCount)
Calculates the average load time.static double
calculateHitRatio(long hitCount, long missCount)
Calculates the hit ratio from hit and miss counts.static double
calculateMissRatio(long hitCount, long missCount)
Calculates the miss ratio from hit and miss counts.static CacheStats
createBasicStats(AtomicLong hitCount, AtomicLong missCount)
Creates a basic CacheStats object from hit and miss counters.static CacheStats
createComprehensiveStats(AtomicLong hitCount, AtomicLong missCount, AtomicLong loadCount, AtomicLong loadTime, AtomicLong evictionCount)
Creates a comprehensive CacheStats object with all counters.static String
formatDetailedMetrics(long hitCount, long missCount, long loadCount, long totalLoadTime, long evictionCount, long cacheSize)
Creates a detailed performance metrics string.static String
formatStatsSummary(long hitCount, long missCount, long loadCount, long evictionCount)
Creates a formatted statistics summary string.static void
recordEviction(AtomicLong evictionCount, boolean statsEnabled)
Records a cache eviction.static void
recordHit(AtomicLong hitCount, boolean statsEnabled)
Records a cache hit in a thread-safe manner.static void
recordLoad(AtomicLong loadCount, AtomicLong loadTime, long operationTimeNanos, boolean statsEnabled)
Records a cache load operation.static void
recordMiss(AtomicLong missCount, boolean statsEnabled)
Records a cache miss in a thread-safe manner.static void
resetCounters(AtomicLong... counters)
Resets all statistics counters to zero.
-
-
-
Method Detail
-
createBasicStats
public static CacheStats createBasicStats(AtomicLong hitCount, AtomicLong missCount)
Creates a basic CacheStats object from hit and miss counters. This pattern is used by many cache implementations.- Parameters:
hitCount
- the hit countermissCount
- the miss counter- Returns:
- a populated CacheStats object
-
createComprehensiveStats
public static CacheStats createComprehensiveStats(AtomicLong hitCount, AtomicLong missCount, AtomicLong loadCount, AtomicLong loadTime, AtomicLong evictionCount)
Creates a comprehensive CacheStats object with all counters.- Parameters:
hitCount
- the hit countermissCount
- the miss counterloadCount
- the load counterloadTime
- the total load time in nanosecondsevictionCount
- the eviction counter- Returns:
- a fully populated CacheStats object
-
calculateHitRatio
public static double calculateHitRatio(long hitCount, long missCount)
Calculates the hit ratio from hit and miss counts.- Parameters:
hitCount
- the number of hitsmissCount
- the number of misses- Returns:
- the hit ratio between 0.0 and 1.0
-
calculateMissRatio
public static double calculateMissRatio(long hitCount, long missCount)
Calculates the miss ratio from hit and miss counts.- Parameters:
hitCount
- the number of hitsmissCount
- the number of misses- Returns:
- the miss ratio between 0.0 and 1.0
-
calculateAverageLoadTime
public static double calculateAverageLoadTime(long totalLoadTime, long loadCount)
Calculates the average load time.- Parameters:
totalLoadTime
- the total load time in nanosecondsloadCount
- the number of loads- Returns:
- the average load time in nanoseconds, or 0.0 if no loads
-
recordHit
public static void recordHit(AtomicLong hitCount, boolean statsEnabled)
Records a cache hit in a thread-safe manner.- Parameters:
hitCount
- the hit counter to incrementstatsEnabled
- whether statistics recording is enabled
-
recordMiss
public static void recordMiss(AtomicLong missCount, boolean statsEnabled)
Records a cache miss in a thread-safe manner.- Parameters:
missCount
- the miss counter to incrementstatsEnabled
- whether statistics recording is enabled
-
recordLoad
public static void recordLoad(AtomicLong loadCount, AtomicLong loadTime, long operationTimeNanos, boolean statsEnabled)
Records a cache load operation.- Parameters:
loadCount
- the load counter to incrementloadTime
- the total load time counter to updateoperationTimeNanos
- the time this operation took in nanosecondsstatsEnabled
- whether statistics recording is enabled
-
recordEviction
public static void recordEviction(AtomicLong evictionCount, boolean statsEnabled)
Records a cache eviction.- Parameters:
evictionCount
- the eviction counter to incrementstatsEnabled
- whether statistics recording is enabled
-
resetCounters
public static void resetCounters(AtomicLong... counters)
Resets all statistics counters to zero.- Parameters:
counters
- the counters to reset
-
formatStatsSummary
public static String formatStatsSummary(long hitCount, long missCount, long loadCount, long evictionCount)
Creates a formatted statistics summary string.- Parameters:
hitCount
- the number of hitsmissCount
- the number of missesloadCount
- the number of loadsevictionCount
- the number of evictions- Returns:
- a formatted statistics summary
-
formatDetailedMetrics
public static String formatDetailedMetrics(long hitCount, long missCount, long loadCount, long totalLoadTime, long evictionCount, long cacheSize)
Creates a detailed performance metrics string.- Parameters:
hitCount
- the number of hitsmissCount
- the number of missesloadCount
- the number of loadstotalLoadTime
- the total load time in nanosecondsevictionCount
- the number of evictionscacheSize
- the current cache size- Returns:
- a detailed performance summary
-
-