{"record":{"id":"8ee4a5d83c3bda72","repo":"TheAlgorithms/Java","slug":"capacity-must-be-greater-than-0","errorCode":null,"errorMessage":"Capacity must be greater than 0!","messagePattern":"Capacity must be greater than 0!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java","lineNumber":66,"sourceCode":"     */\n    private void setCapacity(int newCapacity) {\n        checkCapacity(newCapacity);\n        while (data.size() > newCapacity) {\n            Entry<K, V> evicted = evict();\n            data.remove(evicted.getKey());\n        }\n        this.cap = newCapacity;\n    }\n\n    /**\n     * Checks if the specified capacity is valid.\n     *\n     * @param capacity the capacity to check\n     * @throws IllegalArgumentException if the capacity is less than or equal to zero\n     */\n    private void checkCapacity(int capacity) {\n        if (capacity <= 0) {\n            throw new IllegalArgumentException(\"Capacity must be greater than 0!\");\n        }\n    }\n\n    /**\n     * Evicts the most recently used entry from the cache.\n     *\n     * @return the evicted entry\n     * @throws RuntimeException if the cache is empty\n     */\n    private Entry<K, V> evict() {\n        if (head == null) {\n            throw new RuntimeException(\"Cache cannot be empty!\");\n        }\n        final Entry<K, V> evicted = this.tail;\n        tail = evicted.getPreEntry();\n        if (tail != null) {\n            tail.setNextEntry(null);\n        }","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java#L48-L84","documentation":"Thrown by MRUCache.checkCapacity() when the cache is constructed with a capacity of zero or less. MRUCache requires at least one slot. Unlike LRUCache, this correctly uses IllegalArgumentException, making it catchable by type.","triggerScenarios":"Calling new MRUCache(0), new MRUCache(-5), or passing any int <= 0 to the MRUCache(int cap) constructor. The no-arg constructor defaults to 100.","commonSituations":"Capacity sourced from a config file or environment variable that can be 0 or unset. Deriving capacity from arithmetic that can produce a non-positive result.","solutions":["Ensure the capacity value is >= 1 before constructing the cache","Validate configuration values at startup","Use the no-arg constructor if you only need the default capacity of 100"],"exampleFix":"// before\nMRUCache<String,String> cache = new MRUCache<>(props.getInt(\"cache.max\"));\n\n// after\nint cap = props.getInt(\"cache.max\", 100);\nif (cap <= 0) cap = 100;\nMRUCache<String,String> cache = new MRUCache<>(cap);","handlingStrategy":"validation","validationCode":"if (cap < 1) {\n    throw new IllegalStateException(\"MRUCache capacity must be >= 1, got: \" + cap);\n}\nMRUCache<String,String> cache = new MRUCache<>(cap);","typeGuard":null,"tryCatchPattern":"try {\n    cache = new MRUCache<>(cap);\n} catch (IllegalArgumentException e) {\n    logger.error(\"Invalid MRUCache capacity: \" + cap, e);\n    cache = new MRUCache<>(); // default 100\n}","preventionTips":["Validate capacity at configuration load time","Use IllegalArgumentException for user-facing API errors (this class already does)","Provide sensible defaults in config schemas"],"tags":["mru-cache","configuration","validation","constructor"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}