{"record":{"id":"d36671c4aac38b2b","repo":"TheAlgorithms/Java","slug":"capacity-must-be-0-d36671","errorCode":null,"errorMessage":"Capacity must be > 0","messagePattern":"Capacity must be > 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java","lineNumber":504,"sourceCode":"     * strategy. Call {@link #build()} to create the configured cache instance.\n     *\n     * @param <K> the type of keys maintained by the cache\n     * @param <V> the type of values stored in the cache\n     */\n    public static class Builder<K, V> {\n        private final int capacity;\n        private long defaultTTL = 0;\n        private BiConsumer<K, V> evictionListener;\n        private EvictionStrategy<K, V> evictionStrategy = new LIFOCache.ImmediateEvictionStrategy<>();\n        /**\n         * Creates a new {@code Builder} with the specified cache capacity.\n         *\n         * @param capacity the maximum number of entries the cache can hold; must be > 0\n         * @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        }","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java#L486-L522","documentation":"Thrown by the LIFOCache.Builder constructor when capacity <= 0. Capacity bounds the internal structures and underpins eviction; a non-positive capacity makes the cache unable to hold entries. The guard runs in the constructor, so no builder is created in an invalid state.","triggerScenarios":"new LIFOCache.Builder<>(0); new LIFOCache.Builder<>(-5); capacity from config defaulting to 0 or computed as 0.","commonSituations":"Optional config defaulting to 0; capacity from memory/entrySize math that underflows; tests at capacity 0.","solutions":["Default to a positive capacity (e.g. 256) when config is absent.","Validate config at startup and fail fast.","Compute defensively: int cap = Math.max(1, configuredCap)."],"exampleFix":"// before\nnew LIFOCache.Builder<>(config.getCacheSize())\n// after\nint cap = config.getCacheSize();\nif (cap <= 0) throw new IllegalStateException(\"cache.size must be > 0, got \" + cap);\nnew LIFOCache.Builder<>(cap)","handlingStrategy":"validation","validationCode":"int cap = configuredCapacity;\nif (cap <= 0) throw new IllegalStateException(\"cache.capacity must be > 0, got \" + cap);\nnew LIFOCache.Builder<K, V>(cap);","typeGuard":"static boolean isValidCapacity(int capacity) {\n    return capacity > 0;\n}","tryCatchPattern":null,"preventionTips":["Default optional capacity config to a sane positive value.","Validate capacity at startup, not at first operation.","Guard capacity math against underflow."],"tags":["java","lifo-cache","builder","capacity","argument-validation","cache"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}