refinements/simplification

This commit is contained in:
Ben Fry
2015-01-21 15:53:55 -05:00
parent ba1f496247
commit 7096c953b2
3 changed files with 23 additions and 12 deletions

View File

@@ -423,10 +423,13 @@ 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.";
private void boundsProblem(int index, String method) {
final String msg = String.format("The list size is %d. " +
"You cannot %s() to element %d.", count, method, index);
throw new ArrayIndexOutOfBoundsException(msg);
}
/**
* @webref floatlist:method
* @brief Add to a value
@@ -435,7 +438,7 @@ public class FloatList implements Iterable<Float> {
if (index < count) {
data[index] += amount;
} else {
throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "add"));
boundsProblem(index, "add");
}
}
@@ -448,7 +451,7 @@ public class FloatList implements Iterable<Float> {
if (index < count) {
data[index] -= amount;
} else {
throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "sub"));
boundsProblem(index, "sub");
}
}
@@ -461,7 +464,7 @@ public class FloatList implements Iterable<Float> {
if (index < count) {
data[index] *= amount;
} else {
throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "mult"));
boundsProblem(index, "mult");
}
}
@@ -474,7 +477,7 @@ public class FloatList implements Iterable<Float> {
if (index < count) {
data[index] /= amount;
} else {
throw new ArrayIndexOutOfBoundsException(exceptionText(count, index, "div"));
boundsProblem(index, "div");
}
}

View File

@@ -407,10 +407,14 @@ 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.";
private void boundsProblem(int index, String method) {
final String msg = String.format("The list size is %d. " +
"You cannot %s() to element %d.", count, method, index);
throw new ArrayIndexOutOfBoundsException(msg);
}
/**
* @webref intlist:method
* @brief Add to a value
@@ -419,7 +423,7 @@ public class IntList implements Iterable<Integer> {
if (index < count) {
data[index] += amount;
} else {
throw new IndexOutOfBoundsException(exceptionText(count, index, "add"));
boundsProblem(index, "add");
}
}
@@ -431,7 +435,7 @@ public class IntList implements Iterable<Integer> {
if (index < count) {
data[index] -= amount;
} else {
throw new IndexOutOfBoundsException(exceptionText(count, index, "sub"));
boundsProblem(index, "sub");
}
}
@@ -443,7 +447,7 @@ public class IntList implements Iterable<Integer> {
if (index < count) {
data[index] *= amount;
} else {
throw new IndexOutOfBoundsException(exceptionText(count, index, "mult"));
boundsProblem(index, "mult");
}
}
@@ -455,7 +459,7 @@ public class IntList implements Iterable<Integer> {
if (index < count) {
data[index] /= amount;
} else {
throw new IndexOutOfBoundsException(exceptionText(count, index, "div"));
boundsProblem(index, "div");
}
}