goldmansachs / gs-collections

GS Collections has been migrated to the Eclipse Foundation, re-branded as Eclipse Collections. https://www.eclipse.org/collections/
https://www.eclipse.org/collections/
1.81k stars 276 forks source link

FastList IndexOutOfBoundError even after calling ensureCapacity #25

Closed younes-abouelnagah closed 9 years ago

younes-abouelnagah commented 9 years ago

Scenario: Call ensureCapacity on a FastList then add to the list with a particular index.

Expected: success of all adds where the index is less than the capacity. Looking at the code of ensureCapacity supports this expectation since the method actually allocates a backing array of the requested capacity.

Current behavior: IndexOutOfBound can happen when adding at arbitrary indexes.

Reason: The add method with the index specified checks the index against the size rather than the capacity.

goldmansachs commented 9 years ago

The implementation of add(index, object) follows the contract defined in the Javadoc of java.util.List.

Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

The purpose of ensureCapacity() is to make sure that subsequent adding can be done without repeatedly resizing the backing array. It's an optimization only. It does not mutate the list in any visible way, like changing its size.