TheAlgorithms/Java · error · IllegalArgumentException
Interval must be > 0
Error message
Interval must be > 0
What it means
Thrown by the constructor of FIFOCache.PeriodicEvictionStrategy when interval <= 0. The strategy evicts expired entries every Nth access via counter % interval, so a zero or negative interval would cause division-by-zero or never trigger. The guard runs in the constructor, so the strategy object is never created in an invalid state.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java:453
* <p>This deterministic strategy ensures cleanup occurs at predictable intervals,
* ideal for moderately active caches where memory usage is a concern.
*
* @param <K> the type of keys
* @param <V> the type of values
*/
public static class PeriodicEvictionStrategy<K, V> implements EvictionStrategy<K, V> {
private final int interval;
private final AtomicInteger counter = new AtomicInteger();
/**
* Constructs a periodic eviction strategy.
*
* @param interval the number of accesses between evictions; must be > 0
* @throws IllegalArgumentException if {@code interval} is less than or equal to 0
*/
public PeriodicEvictionStrategy(int interval) {
if (interval <= 0) {
throw new IllegalArgumentException("Interval must be > 0");
}
this.interval = interval;
}
@Override
public int onAccess(FIFOCache<K, V> cache) {
if (counter.incrementAndGet() % interval == 0) {
return cache.evictExpired();
}
return 0;
}
}
/**
* A builder for constructing a {@link FIFOCache} instance with customizable settings.
*
* <p>Allows configuring capacity, default TTL, eviction listener, and a pluggable evictionView on GitHub (pinned to fdfb9a395b)
Solutions
- Default interval to 1 or a sane positive constant when config is missing.
- Validate the config value at startup: Objects.checkIndex(interval-1, Integer.MAX_VALUE) or an explicit check.
- Use ImmediateEvictionStrategy instead when periodic eviction is not desired.
Example fix
// before new FIFOCache.PeriodicEvictionStrategy<>(config.getEvictEvery()); // after int n = config.getEvictEvery(); if (n <= 0) n = 1; new FIFOCache.PeriodicEvictionStrategy<>(n);
Defensive patterns
Strategy: validation
Validate before calling
int n = configuredInterval; if (n <= 0) n = 1; new FIFOCache.PeriodicEvictionStrategy<K, V>(n);
Type guard
static boolean isValidInterval(int interval) {
return interval > 0;
} Prevention
- Default optional eviction-interval config to a positive constant.
- When periodic eviction is unwanted, use ImmediateEvictionStrategy instead of passing 0.
- Validate the value at startup with a descriptive message.
When it happens
Trigger: new FIFOCache.PeriodicEvictionStrategy<>(0); new PeriodicEvictionStrategy<>(-5); interval read from config that defaulted to 0; computing interval from capacity/batch where the divisor yields 0.
Common situations: Config keys that are optional and default to 0; feature-flagged eviction where the flag is off and the interval was never set; arithmetic that underflows to zero for small caches.
Related errors
- Eviction strategy must not be null
- Key must not be null
- Key and value must not be null
- TTL must be >= 0
- Key cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/7b541c9b0efb2997.
Report an issue: GitHub.