Merge pull request #3053 from pvrs12/add-list-exceptions

Add exceptions for FloatList and IntList
This commit is contained in:
Ben Fry
2015-01-21 15:46:18 -05:00
2 changed files with 48 additions and 8 deletions

View File

@@ -423,12 +423,20 @@ public class FloatList implements Iterable<Float> {
}
private String exceptionText(int count, int index, String method){
return "The list size is "+count+". Trying to "+method+" the element at "+index+" which does not exist.";
}
/**
* @webref floatlist:method
* @brief Add to a value
*/
public void add(int index, float amount) {
data[index] += amount;
if (index < count) {
data[index] += amount;
} else {
throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "add"));
}
}
@@ -437,7 +445,11 @@ public class FloatList implements Iterable<Float> {
* @brief Subtract from a value
*/
public void sub(int index, float amount) {
data[index] -= amount;
if (index < count) {
data[index] -= amount;
} else {
throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "sub"));
}
}
@@ -446,7 +458,11 @@ public class FloatList implements Iterable<Float> {
* @brief Multiply a value
*/
public void mult(int index, float amount) {
data[index] *= amount;
if (index < count) {
data[index] *= amount;
} else {
throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "mult"));
}
}
@@ -455,7 +471,11 @@ public class FloatList implements Iterable<Float> {
* @brief Divide a value
*/
public void div(int index, float amount) {
data[index] /= amount;
if (index < count) {
data[index] /= amount;
} else {
throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "div"));
}
}

View File

@@ -407,12 +407,20 @@ public class IntList implements Iterable<Integer> {
data[index]++;
}
private String exceptionText(int count, int index, String method){
return "The list size is "+count+". Trying to "+method+" the element at "+index+" which does not exist.";
}
/**
* @webref intlist:method
* @brief Add to a value
*/
public void add(int index, int amount) {
data[index] += amount;
if (index < count) {
data[index] += amount;
} else {
throw new IndexOutOfBoundsException(exceptionText(count, index, "add"));
}
}
/**
@@ -420,7 +428,11 @@ public class IntList implements Iterable<Integer> {
* @brief Subtract from a value
*/
public void sub(int index, int amount) {
data[index] -= amount;
if (index < count) {
data[index] -= amount;
} else {
throw new IndexOutOfBoundsException(exceptionText(count, index, "sub"));
}
}
/**
@@ -428,7 +440,11 @@ public class IntList implements Iterable<Integer> {
* @brief Multiply a value
*/
public void mult(int index, int amount) {
data[index] *= amount;
if (index < count) {
data[index] *= amount;
} else {
throw new IndexOutOfBoundsException(exceptionText(count, index, "mult"));
}
}
/**
@@ -436,7 +452,11 @@ public class IntList implements Iterable<Integer> {
* @brief Divide a value
*/
public void div(int index, int amount) {
data[index] /= amount;
if (index < count) {
data[index] /= amount;
} else {
throw new IndexOutOfBoundsException(exceptionText(count, index, "div"));
}
}