{"record":{"id":"fa9d0b7b5946b5d0","repo":"TheAlgorithms/Java","slug":"initial-bucket-capacity-must-be-between-1-and-1000","errorCode":null,"errorMessage":"Initial bucket capacity must be between 1 and 1000","messagePattern":"Initial bucket capacity must be between 1 and 1000","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/sorts/SpreadSort.java","lineNumber":31,"sourceCode":"    private static final int MAX_MIN_BUCKETS = 100;\n\n    private final int insertionSortThreshold;\n    private final int initialBucketCapacity;\n    private final int minBuckets;\n\n    /**\n     * Constructor to initialize the SpreadSort algorithm with custom parameters.\n     *\n     * @param insertionSortThreshold the threshold for using insertion sort for small segments (1-1000)\n     * @param initialBucketCapacity  the initial capacity for each bucket (1-1000)\n     * @param minBuckets             the minimum number of buckets to use (1-100)\n     */\n    public SpreadSort(int insertionSortThreshold, int initialBucketCapacity, int minBuckets) {\n        if (insertionSortThreshold < 1 || insertionSortThreshold > MAX_INSERTION_SORT_THRESHOLD) {\n            throw new IllegalArgumentException(\"Insertion sort threshold must be between 1 and \" + MAX_INSERTION_SORT_THRESHOLD);\n        }\n        if (initialBucketCapacity < 1 || initialBucketCapacity > MAX_INITIAL_BUCKET_CAPACITY) {\n            throw new IllegalArgumentException(\"Initial bucket capacity must be between 1 and \" + MAX_INITIAL_BUCKET_CAPACITY);\n        }\n        if (minBuckets < 1 || minBuckets > MAX_MIN_BUCKETS) {\n            throw new IllegalArgumentException(\"Minimum number of buckets must be between 1 and \" + MAX_MIN_BUCKETS);\n        }\n\n        this.insertionSortThreshold = insertionSortThreshold;\n        this.initialBucketCapacity = initialBucketCapacity;\n        this.minBuckets = minBuckets;\n    }\n\n    /**\n     * Default constructor with predefined values.\n     */\n    public SpreadSort() {\n        this(16, 16, 2);\n    }\n\n    /**","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/sorts/SpreadSort.java#L13-L49","documentation":"Thrown by the SpreadSort(int, int, int) constructor when initialBucketCapacity < 1 or > MAX_INITIAL_BUCKET_CAPACITY (1000). The capacity pre-allocates each bucket; values outside [1, 1000] cause either zero-capacity buckets (immediate resize churn) or excessive memory. The message interpolates the upper bound constant.","triggerScenarios":"new SpreadSort(threshold, 0, buckets); new SpreadSort(threshold, 1001, buckets); new SpreadSort(threshold, -1, buckets); loading capacity from config without bounds checking.","commonSituations":"Config default left at 0; capacity derived from an input-size formula that can exceed 1000; user-tunable knob without validation.","solutions":["Validate 1 <= initialBucketCapacity <= 1000 before constructing.","Clamp config-derived values into the valid range with a sensible default.","Validate at config parse time and surface a clear error listing the allowed range."],"exampleFix":"// before\nSpreadSort s = new SpreadSort(threshold, capacity, buckets);\n\n// after\nint c = Math.max(1, Math.min(1000, capacity));\nSpreadSort s = new SpreadSort(threshold, c, buckets);","handlingStrategy":"validation","validationCode":"if (initialBucketCapacity < 1 || initialBucketCapacity > 1000) throw new IllegalArgumentException(\"initialBucketCapacity must be in [1,1000]\");","typeGuard":"public static boolean isValidBucketCapacity(int v) { return v >= 1 && v <= 1000; }","tryCatchPattern":null,"preventionTips":["Validate config-derived capacity at parse time against [1, 1000].","Clamp to a sensible default when out of range.","Document the allowed range in your config schema."],"tags":["sorting","config","range-validation","constructor"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}