ImmutableArrayList: better exception types and messages
This commit is contained in:
@@ -11,24 +11,24 @@ private class ImmutableArrayList<T>(
|
|||||||
{
|
{
|
||||||
// impossible
|
// impossible
|
||||||
if (offset < 0) {
|
if (offset < 0) {
|
||||||
throw IllegalArgumentException("negative offset")
|
throw IllegalArgumentException("negative offset ($offset)")
|
||||||
}
|
}
|
||||||
// impossible
|
// impossible
|
||||||
if (length < 0) {
|
if (length < 0) {
|
||||||
throw IllegalArgumentException("negative length")
|
throw IllegalArgumentException("negative length ($length)")
|
||||||
}
|
}
|
||||||
// possible when builder is used from different threads
|
// possible when builder is used from different threads
|
||||||
if (offset + length > array.size) {
|
if (offset + length > array.size) {
|
||||||
throw IllegalArgumentException("offset + length > array.length")
|
throw IllegalArgumentException("offset ($offset) + length ($length) > array.length (${array.size})")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun indexInArray(index: Int): Int {
|
protected fun indexInArray(index: Int): Int {
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
throw IllegalArgumentException("negative index")
|
throw IndexOutOfBoundsException("negative index ($index)")
|
||||||
}
|
}
|
||||||
if (index >= length) {
|
if (index >= length) {
|
||||||
throw IllegalArgumentException("index > length")
|
throw IndexOutOfBoundsException("index ($index) >= length ($length)")
|
||||||
}
|
}
|
||||||
return index + offset
|
return index + offset
|
||||||
}
|
}
|
||||||
@@ -39,13 +39,13 @@ private class ImmutableArrayList<T>(
|
|||||||
|
|
||||||
public override fun subList(fromIndex: Int, toIndex: Int) : List<T> {
|
public override fun subList(fromIndex: Int, toIndex: Int) : List<T> {
|
||||||
if (fromIndex < 0) {
|
if (fromIndex < 0) {
|
||||||
throw IllegalArgumentException("negative from index")
|
throw IndexOutOfBoundsException("negative from index ($fromIndex)")
|
||||||
}
|
}
|
||||||
if (toIndex < fromIndex) {
|
if (toIndex < fromIndex) {
|
||||||
throw IllegalArgumentException("toIndex < fromIndex")
|
throw IndexOutOfBoundsException("toIndex ($toIndex) < fromIndex ($fromIndex)")
|
||||||
}
|
}
|
||||||
if (toIndex > length) {
|
if (toIndex > length) {
|
||||||
throw IllegalArgumentException("fromIndex + toIndex > length")
|
throw IndexOutOfBoundsException("fromIndex ($fromIndex) + toIndex ($toIndex) > length ($length)")
|
||||||
}
|
}
|
||||||
return ImmutableArrayList(array, offset + fromIndex, toIndex - fromIndex)
|
return ImmutableArrayList(array, offset + fromIndex, toIndex - fromIndex)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ class ImmutableArrayListTest() : TestCase() {
|
|||||||
try {
|
try {
|
||||||
list[expectedLength]
|
list[expectedLength]
|
||||||
fail()
|
fail()
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IndexOutOfBoundsException) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user