{"record":{"id":"cf15e0e6644f66d0","repo":"TheAlgorithms/Java","slug":"capacity-must-be-0","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/FIFOCache.java","lineNumber":490,"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 FIFOCache.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":472,"sourceCodeEnd":508,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java#L472-L508","documentation":"Thrown by the FIFOCache.Builder constructor when capacity <= 0. Capacity bounds the internal HashMap and is fundamental to eviction logic; a non-positive capacity would make the cache unable to hold any entry. The check runs in the constructor, so no builder is created with an invalid capacity.","triggerScenarios":"new FIFOCache.Builder<>(0); new FIFOCache.Builder<>(-10); capacity sourced from a config value or property that defaulted to 0 or was not set.","commonSituations":"Optional config that defaults to 0; capacity derived from maxMemory / entrySize where entrySize is mis-estimated to be larger than memory; tests that construct a builder with capacity 0 expecting an empty cache.","solutions":["Default to a positive capacity (e.g. 256 or 1000) when config is missing.","Validate the config value at startup and fail fast with a descriptive message.","Compute capacity defensively: int cap = Math.max(1, configuredCap)."],"exampleFix":"// before\nnew FIFOCache.Builder<>(config.getCacheSize())\n// after\nint cap = config.getCacheSize();\nif (cap <= 0) throw new IllegalStateException(\"cache.cacheSize must be > 0, got \" + cap);\nnew FIFOCache.Builder<>(cap)","handlingStrategy":"validation","validationCode":"int cap = configuredCapacity;\nif (cap <= 0) throw new IllegalStateException(\"cache.capacity must be > 0, got \" + cap);\nnew FIFOCache.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 rather than at first cache operation.","Guard capacity math (memory/entrySize) against underflow to zero."],"tags":["java","fifo-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"}