{"record":{"id":"a4b32ccb2985b348","repo":"TheAlgorithms/Java","slug":"default-ttl-must-be-0-a4b32c","errorCode":null,"errorMessage":"Default TTL must be >= 0","messagePattern":"Default TTL must be >= 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/caches/RRCache.java","lineNumber":445,"sourceCode":"         * @throws IllegalArgumentException if {@code capacity} is less than or equal to 0\n         */\n        public Builder(int capacity) {\n            if (capacity <= 0) {\n                throw new IllegalArgumentException(\"Capacity must be > 0\");\n            }\n            this.capacity = capacity;\n        }\n\n        /**\n         * Sets the default time-to-live (TTL) in milliseconds for cache entries.\n         *\n         * @param ttlMillis the TTL duration in milliseconds; must be >= 0\n         * @return this builder instance for chaining\n         * @throws IllegalArgumentException if {@code ttlMillis} is negative\n         */\n        public Builder<K, V> defaultTTL(long ttlMillis) {\n            if (ttlMillis < 0) {\n                throw new IllegalArgumentException(\"Default TTL must be >= 0\");\n            }\n            this.defaultTTL = ttlMillis;\n            return this;\n        }\n\n        /**\n         * Sets the {@link Random} instance to be used for random eviction selection.\n         *\n         * @param r a non-null {@code Random} instance\n         * @return this builder instance for chaining\n         * @throws IllegalArgumentException if {@code r} is {@code null}\n         */\n        public Builder<K, V> random(Random r) {\n            if (r == null) {\n                throw new IllegalArgumentException(\"Random must not be null\");\n            }\n            this.random = r;\n            return this;","sourceCodeStart":427,"sourceCodeEnd":463,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/RRCache.java#L427-L463","documentation":"Builder.defaultTTL() rejects negative values. The default TTL is applied to entries inserted via the two-argument put(key, value) method. A value of 0 means entries never expire.","triggerScenarios":"Calling builder.defaultTTL(-1) or any negative value during cache construction.","commonSituations":"TTL sourced from a duration config that can be negative. Unit conversion (e.g., seconds to millis) where a misconfigured value produces a negative result.","solutions":["Clamp to Math.max(0, ttl) or simply omit the call to use the default of 0","Validate TTL configuration before passing it to the builder","Use 0 explicitly to indicate no expiry"],"exampleFix":"// before\n.long defaultTtl = Duration.parse(config.get(\"ttl\")).toMillis(); // can be negative on bad input\nnew RRCache.Builder<String,String>(100).defaultTTL(defaultTtl).build();\n\n// after\nlong defaultTtl = Duration.parse(config.getOrDefault(\"ttl\", \"PT0S\")).toMillis();\nnew RRCache.Builder<String,String>(100).defaultTTL(Math.max(0, defaultTtl)).build();","handlingStrategy":"validation","validationCode":"long ttl = Math.max(0, configuredDefaultTTL);\nnew RRCache.Builder<String,String>(100).defaultTTL(ttl).build();","typeGuard":null,"tryCatchPattern":"try {\n    builder.defaultTTL(ttlMillis);\n} catch (IllegalArgumentException e) {\n    builder.defaultTTL(0); // no-expiry fallback\n}","preventionTips":["Clamp TTLs with Math.max(0, value)","Omit the defaultTTL() call entirely if you want 0 (no expiry)","Validate duration configurations before use"],"tags":["rr-cache","configuration","validation","ttl","builder"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}