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 eviction

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Default interval to 1 or a sane positive constant when config is missing.
  2. Validate the config value at startup: Objects.checkIndex(interval-1, Integer.MAX_VALUE) or an explicit check.
  3. 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

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


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/7b541c9b0efb2997. Report an issue: GitHub.