summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2019-06-04 16:47:59 +0200
committerJon Bratseth <bratseth@verizonmedia.com>2019-06-04 16:47:59 +0200
commit74e30bfa86aae81f35cacc3666d60b18e5f22948 (patch)
tree653ad725bb8e40e16ffff7c8a5fdc1010d44dcd9 /vespajlib
parente62578c8451cee6cec2b0a5349b88bb80380fb20 (diff)
Expect IllegalArgumentException and be consistent
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java18
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java14
-rw-r--r--vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java2
3 files changed, 22 insertions, 12 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java b/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java
index 17b62bf1a77..c0d817459d0 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java
@@ -20,9 +20,14 @@ public final class DimensionSizes {
/**
* Returns the length of this in the nth dimension
*
- * @throws IndexOutOfBoundsException if the index is larger than the number of dimensions in this tensor minus one
+ * @throws IllegalArgumentException if the index is larger than the number of dimensions in this tensor minus one
*/
- public long size(int dimensionIndex) { return sizes[dimensionIndex]; }
+ public long size(int dimensionIndex) {
+ if (dimensionIndex <0 || dimensionIndex >= sizes.length)
+ throw new IllegalArgumentException("Illegal dimension index " + dimensionIndex +
+ ": This has " + sizes.length + " dimensions");
+ return sizes[dimensionIndex];
+ }
/** Returns the number of dimensions this provides the size of */
public int dimensions() { return sizes.length; }
@@ -65,9 +70,14 @@ public final class DimensionSizes {
/**
* Returns the length of this in the nth dimension
*
- * @throws IndexOutOfBoundsException if the index is larger than the number of dimensions in this tensor minus one
+ * @throws IllegalArgumentException if the index is larger than the number of dimensions in this tensor minus one
*/
- public long size(int dimensionIndex) { return sizes[dimensionIndex]; }
+ public long size(int dimensionIndex) {
+ if (dimensionIndex <0 || dimensionIndex >= sizes.length)
+ throw new IllegalArgumentException("Illegal dimension index " + dimensionIndex +
+ ": This has " + sizes.length + " dimensions");
+ return sizes[dimensionIndex];
+ }
/** Returns the number of dimensions this provides the size of */
public int dimensions() { return sizes.length; }
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
index 5307ea205ce..aeb3da8ac40 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
@@ -91,7 +91,7 @@ public abstract class IndexedTensor implements Tensor {
* Returns the value at the given indexes as a double
*
* @param indexes the indexes into the dimensions of this. Must be one number per dimension of this
- * @throws IndexOutOfBoundsException if any of the indexes are out of bound or a wrong number of indexes are given
+ * @throws IllegalArgumentException if any of the indexes are out of bound or a wrong number of indexes are given
*/
public double get(long ... indexes) {
return get((int)toValueIndex(indexes, dimensionSizes));
@@ -101,7 +101,7 @@ public abstract class IndexedTensor implements Tensor {
* Returns the value at the given indexes as a float
*
* @param indexes the indexes into the dimensions of this. Must be one number per dimension of this
- * @throws IndexOutOfBoundsException if any of the indexes are out of bound or a wrong number of indexes are given
+ * @throws IllegalArgumentException if any of the indexes are out of bound or a wrong number of indexes are given
*/
public float getFloat(long ... indexes) {
return getFloat((int)toValueIndex(indexes, dimensionSizes));
@@ -124,7 +124,7 @@ public abstract class IndexedTensor implements Tensor {
* if you know the underlying data layout.
*
* @param valueIndex the direct index into the underlying data.
- * @throws IndexOutOfBoundsException if index is out of bounds
+ * @throws IllegalArgumentException if index is out of bounds
*/
public abstract double get(long valueIndex);
@@ -133,7 +133,7 @@ public abstract class IndexedTensor implements Tensor {
* if you know the underlying data layout.
*
* @param valueIndex the direct index into the underlying data.
- * @throws IndexOutOfBoundsException if index is out of bounds
+ * @throws IllegalArgumentException if index is out of bounds
*/
public abstract float getFloat(long valueIndex);
@@ -144,7 +144,7 @@ public abstract class IndexedTensor implements Tensor {
long valueIndex = 0;
for (int i = 0; i < indexes.length; i++) {
if (indexes[i] >= sizes.size(i)) {
- throw new IndexOutOfBoundsException();
+ throw new IllegalArgumentException(indexes + " are not within bounds");
}
valueIndex += productOfDimensionsAfter(i, sizes) * indexes[i];
}
@@ -157,7 +157,7 @@ public abstract class IndexedTensor implements Tensor {
long valueIndex = 0;
for (int i = 0; i < address.size(); i++) {
if (address.numericLabel(i) >= sizes.size(i))
- throw new IllegalArgumentException(address + " is not within bounds of " + type);
+ throw new IllegalArgumentException(address + " is not within the bounds of " + type);
valueIndex += productOfDimensionsAfter(i, sizes) * address.numericLabel(i);
}
return valueIndex;
@@ -562,7 +562,7 @@ public abstract class IndexedTensor implements Tensor {
try {
return get(count++);
}
- catch (IndexOutOfBoundsException e) {
+ catch (IllegalArgumentException e) {
throw new NoSuchElementException("No element at position " + count);
}
}
diff --git a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
index 8c652f5aa27..b466307d3b9 100644
--- a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
+++ b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java
@@ -64,7 +64,7 @@ public class JsonFormatTestCase {
fail("Excpected exception");
}
catch (IllegalArgumentException e) {
- assertEquals("cell address (2) is not within bounds of tensor(x[2])", e.getMessage());
+ assertEquals("cell address (2) is not within the bounds of tensor(x[2])", e.getMessage());
}
}