{"record":{"id":"ae0c94257f395190","repo":"TheAlgorithms/Java","slug":"default-ttl-must-be-0","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/FIFOCache.java","lineNumber":504,"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 an eviction listener to be notified when entries are evicted from the cache.\n         *\n         * @param listener a {@link BiConsumer} that accepts evicted keys and values; must not be {@code null}\n         * @return this builder instance for chaining\n         * @throws IllegalArgumentException if {@code listener} is {@code null}\n         */\n        public Builder<K, V> evictionListener(BiConsumer<K, V> listener) {\n            if (listener == null) {\n                throw new IllegalArgumentException(\"Listener must not be null\");\n            }\n            this.evictionListener = listener;\n            return this;","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java#L486-L522","documentation":"Thrown by FIFOCache.Builder.defaultTTL(long) when ttlMillis is negative. The default TTL is applied to entries added without an explicit TTL; a negative value would set every such entry's expiry in the past. The check runs before storing the field and returns this for chaining, so the builder remains valid on rejection (the exception propagates first).","triggerScenarios":"builder.defaultTTL(-1); defaultTTL(parsed from a negative Duration); config typo with a leading minus; TTL computed from inverted timestamps.","commonSituations":"Environment config (e.g. cache.defaultTtl=-60000) typoed; duration derived from clock skew; reusing a variable that was negated for a different purpose.","solutions":["Clamp the value: builder.defaultTTL(Math.max(0, configuredTtl)).","Treat negative config as 'use 0 (no expiry)' explicitly, or reject the config at startup.","Add a startup assertion on all TTL config keys."],"exampleFix":"// before\nbuilder.defaultTTL(props.getTtlMillis());\n// after\nlong ttl = props.getTtlMillis();\nbuilder.defaultTTL(ttl < 0 ? 0 : ttl);","handlingStrategy":"validation","validationCode":"long ttl = configuredDefaultTtl;\nbuilder.defaultTTL(ttl < 0 ? 0 : ttl);","typeGuard":"static boolean isValidDefaultTtl(long ttlMillis) {\n    return ttlMillis >= 0;\n}","tryCatchPattern":null,"preventionTips":["Clamp negative config to 0 (no expiry) explicitly, or reject at startup.","Add startup assertions for all TTL config keys.","Beware Duration negation and clock-skew arithmetic."],"tags":["java","fifo-cache","builder","ttl","argument-validation","cache"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}