From 74e30bfa86aae81f35cacc3666d60b18e5f22948 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Tue, 4 Jun 2019 16:47:59 +0200 Subject: Expect IllegalArgumentException and be consistent --- .../src/main/java/com/yahoo/tensor/DimensionSizes.java | 18 ++++++++++++++---- .../src/main/java/com/yahoo/tensor/IndexedTensor.java | 14 +++++++------- .../yahoo/tensor/serialization/JsonFormatTestCase.java | 2 +- 3 files changed, 22 insertions(+), 12 deletions(-) (limited to 'vespajlib/src') 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()); } } -- cgit v1.2.3