TheAlgorithms/Java · error · IllegalArgumentException

Interval must be > 0

Error message

Interval must be > 0

What it means

The PeriodicEvictionStrategy constructor requires a strictly positive interval — the number of cache accesses between each expired-entry cleanup cycle. Zero or negative would make the internal modulo check (++counter % interval) meaningless (division by zero for 0).

Source

Thrown at src/main/java/com/thealgorithms/datastructures/caches/RRCache.java:393

     * <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 int counter = 0;

        /**
         * 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(RRCache<K, V> cache) {
            if (++counter % interval == 0) {
                return cache.evictExpired();
            }

            return 0;
        }
    }

    /**
     * A builder for constructing an {@link RRCache} instance with customizable settings.
     *
     * <p>Allows configuring capacity, default TTL, random eviction behavior, eviction listener,

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure the interval is >= 1 before constructing the strategy
  2. Use NoEvictionStrategy instead if you want eviction on every access
  3. Validate the interval at config load time and fail fast

Example fix

// before
int interval = config.getInt("eviction.interval");
new RRCache.PeriodicEvictionStrategy<>(interval); // throws if 0

// after
int interval = config.getInt("eviction.interval", 100);
if (interval < 1) interval = 100;
new RRCache.PeriodicEvictionStrategy<>(interval);
Defensive patterns

Strategy: validation

Validate before calling

int interval = Math.max(1, configuredInterval);
new RRCache.PeriodicEvictionStrategy<>(interval);

Try / catch

try {
    strategy = new RRCache.PeriodicEvictionStrategy<>(interval);
} catch (IllegalArgumentException e) {
    strategy = new RRCache.PeriodicEvictionStrategy<>(100); // safe default
}

Prevention

When it happens

Trigger: Constructing new RRCache.PeriodicEvictionStrategy<>(0), new RRCache.PeriodicEvictionStrategy<>(-1), or passing a config-derived interval <= 0.

Common situations: Interval loaded from configuration that defaults to 0 or is unset. Arithmetic deriving the interval that evaluates to zero on edge cases.

Related errors


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