{"record":{"id":"08788461e44abf71","repo":"TheAlgorithms/Java","slug":"capacity-must-be-greater-than-zero","errorCode":null,"errorMessage":"Capacity must be greater than zero.","messagePattern":"Capacity must be greater than zero\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java","lineNumber":75,"sourceCode":"    private final int capacity;\n    private static final int DEFAULT_CAPACITY = 100;\n\n    /**\n     * Constructs an LFU cache with the default capacity.\n     */\n    public LFUCache() {\n        this(DEFAULT_CAPACITY);\n    }\n\n    /**\n     * Constructs an LFU cache with the specified capacity.\n     *\n     * @param capacity The maximum number of items that the cache can hold.\n     * @throws IllegalArgumentException if the specified capacity is less than or equal to zero.\n     */\n    public LFUCache(int capacity) {\n        if (capacity <= 0) {\n            throw new IllegalArgumentException(\"Capacity must be greater than zero.\");\n        }\n        this.capacity = capacity;\n        this.cache = new HashMap<>();\n    }\n\n    /**\n     * Retrieves the value associated with the given key from the cache.\n     * If the key exists, the node's frequency is incremented, and the node is repositioned\n     * in the linked list based on its updated frequency.\n     *\n     * @param key The key whose associated value is to be returned.\n     * @return The value associated with the key, or {@code null} if the key is not present in the cache.\n     */\n    public V get(K key) {\n        Node node = cache.get(key);\n        if (node == null) {\n            return null;\n        }","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java#L57-L93","documentation":"Thrown by the LFUCache(int capacity) constructor when capacity <= 0. LFU eviction depends on a frequency-ordered linked list whose invariants assume at least one slot; a zero capacity would make the cache unusable and every put an immediate eviction. The no-arg constructor delegates with DEFAULT_CAPACITY, so this only fires on an explicit bad capacity.","triggerScenarios":"new LFUCache<>(0); new LFUCache<>(-1); capacity sourced from config that defaulted to 0 or was computed as size/batch with a 0 divisor.","commonSituations":"Optional config defaulting to 0; capacity derived from memory math that underflows; tests asserting behavior at capacity 0.","solutions":["Default to a positive capacity when config is absent.","Validate config at startup: if (cap <= 0) throw with a clear message.","Use the no-arg constructor new LFUCache<>() to get the default capacity."],"exampleFix":"// before\nnew LFUCache<>(config.getMaxEntries())\n// after\nint cap = config.getMaxEntries();\nif (cap <= 0) cap = 1000;\nnew LFUCache<>(cap)","handlingStrategy":"validation","validationCode":"int cap = configuredCapacity;\nif (cap <= 0) cap = DEFAULT_CAPACITY;\nnew LFUCache<>(cap);","typeGuard":"static boolean isValidCapacity(int capacity) {\n    return capacity > 0;\n}","tryCatchPattern":null,"preventionTips":["Use the no-arg constructor new LFUCache<>() to get the default capacity.","Default optional capacity config to a positive constant.","Guard capacity math against underflow to zero."],"tags":["java","lfu-cache","capacity","argument-validation","cache"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}