{"record":{"id":"aabdec149a752047","repo":"TheAlgorithms/Java","slug":"capacity-must-be-0-aabdec","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/RRCache.java","lineNumber":431,"sourceCode":"     *\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 Random random;\n        private BiConsumer<K, V> evictionListener;\n        private EvictionStrategy<K, V> evictionStrategy = new RRCache.PeriodicEvictionStrategy<>(100);\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":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/RRCache.java#L413-L449","documentation":"The RRCache.Builder constructor requires a strictly positive capacity — it is the only mandatory parameter and defines the maximum number of entries the cache can hold. Zero or negative capacity is rejected immediately.","triggerScenarios":"Calling new RRCache.Builder<>(0), new RRCache.Builder<>(-10), or passing a config-derived capacity <= 0.","commonSituations":"Capacity from a properties file or environment variable that can be 0. Deriving capacity from a collection that can be empty (e.g., list.size()).","solutions":["Validate that capacity is >= 1 before constructing the Builder","Provide a sensible positive default when configuration is missing or invalid","Fail fast at startup with a clear configuration error"],"exampleFix":"// before\nRRCache<String,String> c = new RRCache.Builder<String,String>(poolSize).build();\n\n// after\nint cap = Math.max(1, poolSize);\nRRCache<String,String> c = new RRCache.Builder<String,String>(cap).build();","handlingStrategy":"validation","validationCode":"int cap = Math.max(1, configuredCapacity);\nRRCache<String,String> cache = new RRCache.Builder<String,String>(cap).build();","typeGuard":null,"tryCatchPattern":"try {\n    builder = new RRCache.Builder<>(cap);\n} catch (IllegalArgumentException e) {\n    builder = new RRCache.Builder<>(100); // safe default\n}","preventionTips":["Validate capacity > 0 before builder construction","Provide positive defaults in configuration schemas","Fail fast at startup on invalid cache sizing"],"tags":["rr-cache","configuration","validation","builder"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}