{"record":{"id":"7b541c9b0efb2997","repo":"TheAlgorithms/Java","slug":"interval-must-be-0","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/FIFOCache.java","lineNumber":453,"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(FIFOCache<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 FIFOCache} instance with customizable settings.\n     *\n     * <p>Allows configuring capacity, default TTL, eviction listener, and a pluggable eviction","sourceCodeStart":435,"sourceCodeEnd":471,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java#L435-L471","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nnew FIFOCache.PeriodicEvictionStrategy<>(config.getEvictEvery());\n// after\nint n = config.getEvictEvery();\nif (n <= 0) n = 1;\nnew FIFOCache.PeriodicEvictionStrategy<>(n);","handlingStrategy":"validation","validationCode":"int n = configuredInterval;\nif (n <= 0) n = 1;\nnew FIFOCache.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.","When periodic eviction is unwanted, use ImmediateEvictionStrategy instead of passing 0.","Validate the value at startup with a descriptive message."],"tags":["java","fifo-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"}