{"record":{"id":"1e28f7dfaad089c4","repo":"TheAlgorithms/Java","slug":"interval-must-be-0-1e28f7","errorCode":null,"errorMessage":"Interval must be > 0","messagePattern":"Interval must be > 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java","lineNumber":467,"sourceCode":"     * <p>This deterministic strategy ensures cleanup occurs at predictable intervals,\n     * ideal for moderately active caches where memory usage is a concern.\n     *\n     * @param <K> the type of keys\n     * @param <V> the type of values\n     */\n    public static class PeriodicEvictionStrategy<K, V> implements EvictionStrategy<K, V> {\n        private final int interval;\n        private final AtomicInteger counter = new AtomicInteger();\n\n        /**\n         * Constructs a periodic eviction strategy.\n         *\n         * @param interval the number of accesses between evictions; must be > 0\n         * @throws IllegalArgumentException if {@code interval} is less than or equal to 0\n         */\n        public PeriodicEvictionStrategy(int interval) {\n            if (interval <= 0) {\n                throw new IllegalArgumentException(\"Interval must be > 0\");\n            }\n            this.interval = interval;\n        }\n\n        @Override\n        public int onAccess(LIFOCache<K, V> cache) {\n            if (counter.incrementAndGet() % interval == 0) {\n                return cache.evictExpired();\n            }\n\n            return 0;\n        }\n    }\n\n    /**\n     * A builder for constructing a {@link LIFOCache} instance with customizable settings.\n     *\n     * <p>Allows configuring capacity, default TTL, eviction listener, and a pluggable eviction","sourceCodeStart":449,"sourceCodeEnd":485,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java#L449-L485","documentation":"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.","triggerScenarios":"new LIFOCache.PeriodicEvictionStrategy<>(0); new PeriodicEvictionStrategy<>(-3); interval from config defaulting to 0; arithmetic yielding 0 for small caches.","commonSituations":"Optional config defaulting to 0; feature-flagged eviction off; capacity/batch math underflowing to zero.","solutions":["Default interval to a positive constant (e.g. 1) when config is missing.","Validate at startup with a descriptive message.","Use ImmediateEvictionStrategy when periodic eviction is not wanted."],"exampleFix":"// before\nnew LIFOCache.PeriodicEvictionStrategy<>(config.getEvictEvery());\n// after\nint n = config.getEvictEvery();\nif (n <= 0) n = 1;\nnew LIFOCache.PeriodicEvictionStrategy<>(n);","handlingStrategy":"validation","validationCode":"int n = configuredInterval;\nif (n <= 0) n = 1;\nnew LIFOCache.PeriodicEvictionStrategy<K, V>(n);","typeGuard":"static boolean isValidInterval(int interval) {\n    return interval > 0;\n}","tryCatchPattern":null,"preventionTips":["Default optional eviction-interval config to a positive constant.","Use ImmediateEvictionStrategy when periodic eviction is unwanted.","Validate the value at startup with a clear message."],"tags":["java","lifo-cache","eviction-strategy","argument-validation","cache"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}