{"record":{"id":"27c1e97b54bd3b9f","repo":"TheAlgorithms/Java","slug":"buffer-size-must-be-positive","errorCode":null,"errorMessage":"Buffer size must be positive","messagePattern":"Buffer size must be positive","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java","lineNumber":28,"sourceCode":" *\n * @param <Item> The type of elements stored in the circular buffer.\n */\n@SuppressWarnings(\"unchecked\")\npublic class CircularBuffer<Item> {\n    private final Item[] buffer;\n    private final CircularPointer putPointer;\n    private final CircularPointer getPointer;\n    private final AtomicInteger size = new AtomicInteger(0);\n\n    /**\n     * Constructor to initialize the circular buffer with a specified size.\n     *\n     * @param size The size of the circular buffer.\n     * @throws IllegalArgumentException if the size is zero or negative.\n     */\n    public CircularBuffer(int size) {\n        if (size <= 0) {\n            throw new IllegalArgumentException(\"Buffer size must be positive\");\n        }\n        // noinspection unchecked\n        this.buffer = (Item[]) new Object[size];\n        this.putPointer = new CircularPointer(0, size);\n        this.getPointer = new CircularPointer(0, size);\n    }\n\n    /**\n     * Checks if the circular buffer is empty.\n     * This method is based on the current size of the buffer.\n     *\n     * @return {@code true} if the buffer is empty, {@code false} otherwise.\n     */\n    public boolean isEmpty() {\n        return size.get() == 0;\n    }\n\n    /**","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java#L10-L46","documentation":"Thrown by the CircularBuffer constructor when size <= 0. The buffer allocates a fixed Object array of the given size, so zero or negative sizes are structurally invalid — there would be no slots to store items and the modulo-based pointer logic would divide by zero.","triggerScenarios":"new CircularBuffer<>(0), new CircularBuffer<>(-5). Often the size comes from a config value, a computed capacity, or a user-provided parameter that was left unset (defaulting to 0) or misparsed.","commonSituations":"Configuration with missing/zeroed buffer capacity; computed sizes that underflow for small workloads; reading capacity from a properties file where the key was absent; passing a primitive default (0) from an uninitialized field.","solutions":["Validate and clamp the size to a positive minimum before construction.","Provide a sensible default capacity when config is absent.","Fail fast at config load with a clear error naming the required property."],"exampleFix":"// before\nnew CircularBuffer<>(config.getBufferSize()); // returns 0 if unset\n// after\nint size = config.getBufferSize();\nif (size <= 0) {\n    throw new IllegalArgumentException(\"buffer size must be positive, got \" + size);\n}\nnew CircularBuffer<>(size);","handlingStrategy":"validation","validationCode":"static CircularBuffer<T> createBuffer(int size) {\n    if (size <= 0) throw new IllegalArgumentException(\"buffer size must be positive, got \" + size);\n    return new CircularBuffer<>(size);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return new CircularBuffer<>(capacity);\n} catch (IllegalArgumentException e) {\n    throw new ConfigException(\"CircularBuffer requires size > 0; got \" + capacity);\n}","preventionTips":["Validate capacity is positive before constructing.","Provide a sensible default capacity when config is absent.","Fail fast at config load with a clear message naming the property."],"tags":["java","datastructures","circular-buffer","construction","illegalargument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}