{"record":{"id":"614f03ae77677e44","repo":"TheAlgorithms/Java","slug":"default-ttl-must-be-0-614f03","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/LIFOCache.java","lineNumber":518,"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":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java#L500-L536","documentation":"Thrown by LIFOCache.Builder.defaultTTL(long) when ttlMillis is negative. The default TTL is applied to entries added without an explicit TTL; a negative value sets their expiry in the past. The check runs before storing the field.","triggerScenarios":"builder.defaultTTL(-1); defaultTTL from a negative Duration; config typo with a leading minus; clock-skew arithmetic.","commonSituations":"Env config (cache.defaultTtl=-60000) typoed; duration from skewed clocks; reused negated variable.","solutions":["Clamp the value: builder.defaultTTL(Math.max(0, configuredTtl)).","Treat negative config as 'no expiry' explicitly, or reject at startup.","Add startup assertions 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 explicitly, or reject at startup.","Add startup assertions for all TTL config keys.","Beware Duration negation and clock-skew arithmetic."],"tags":["java","lifo-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"}