TheAlgorithms/Java · error · IllegalArgumentException

Interval must be > 0

Error message

Interval must be > 0

What it means

Thrown by the constructor of LIFOCache.PeriodicEvictionStrategy when interval <= 0. The strategy evicts every Nth access via counter % interval, so zero would divide-by-zero and a negative would never trigger. The guard runs in the constructor, preventing creation of an invalid strategy.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java:467

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

            return 0;
        }
    }

    /**
     * A builder for constructing a {@link LIFOCache} 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 a positive constant (e.g. 1) when config is missing.
  2. Validate at startup with a descriptive message.
  3. Use ImmediateEvictionStrategy when periodic eviction is not wanted.

Example fix

// before
new LIFOCache.PeriodicEvictionStrategy<>(config.getEvictEvery());
// after
int n = config.getEvictEvery();
if (n <= 0) n = 1;
new LIFOCache.PeriodicEvictionStrategy<>(n);
Defensive patterns

Strategy: validation

Validate before calling

int n = configuredInterval;
if (n <= 0) n = 1;
new LIFOCache.PeriodicEvictionStrategy<K, V>(n);

Type guard

static boolean isValidInterval(int interval) {
    return interval > 0;
}

Prevention

When it happens

Trigger: new LIFOCache.PeriodicEvictionStrategy<>(0); new PeriodicEvictionStrategy<>(-3); interval from config defaulting to 0; arithmetic yielding 0 for small caches.

Common situations: Optional config defaulting to 0; feature-flagged eviction off; capacity/batch math underflowing to zero.

Related errors


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