{"record":{"id":"32f42f8e4976270d","repo":"TheAlgorithms/Java","slug":"interval-must-be-0-32f42f","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/RRCache.java","lineNumber":393,"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 int counter = 0;\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(RRCache<K, V> cache) {\n            if (++counter % interval == 0) {\n                return cache.evictExpired();\n            }\n\n            return 0;\n        }\n    }\n\n    /**\n     * A builder for constructing an {@link RRCache} instance with customizable settings.\n     *\n     * <p>Allows configuring capacity, default TTL, random eviction behavior, eviction listener,","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/RRCache.java#L375-L411","documentation":"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).","triggerScenarios":"Constructing new RRCache.PeriodicEvictionStrategy<>(0), new RRCache.PeriodicEvictionStrategy<>(-1), or passing a config-derived interval <= 0.","commonSituations":"Interval loaded from configuration that defaults to 0 or is unset. Arithmetic deriving the interval that evaluates to zero on edge cases.","solutions":["Ensure the interval is >= 1 before constructing the strategy","Use NoEvictionStrategy instead if you want eviction on every access","Validate the interval at config load time and fail fast"],"exampleFix":"// before\nint interval = config.getInt(\"eviction.interval\");\nnew RRCache.PeriodicEvictionStrategy<>(interval); // throws if 0\n\n// after\nint interval = config.getInt(\"eviction.interval\", 100);\nif (interval < 1) interval = 100;\nnew RRCache.PeriodicEvictionStrategy<>(interval);","handlingStrategy":"validation","validationCode":"int interval = Math.max(1, configuredInterval);\nnew RRCache.PeriodicEvictionStrategy<>(interval);","typeGuard":null,"tryCatchPattern":"try {\n    strategy = new RRCache.PeriodicEvictionStrategy<>(interval);\n} catch (IllegalArgumentException e) {\n    strategy = new RRCache.PeriodicEvictionStrategy<>(100); // safe default\n}","preventionTips":["Validate interval > 0 at config load time","Use NoEvictionStrategy for per-access cleanup if interval is uncertain","Default to 100 (the builder's own default) when configuration is absent"],"tags":["rr-cache","configuration","validation","eviction-strategy"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}