Methods whose sole purpose is to throw are to be avoided.
Project: http://git-wip-us.apache.org/repos/asf/commons-complex/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-complex/commit/e5e3e49a Tree: http://git-wip-us.apache.org/repos/asf/commons-complex/tree/e5e3e49a Diff: http://git-wip-us.apache.org/repos/asf/commons-complex/diff/e5e3e49a Branch: refs/heads/master Commit: e5e3e49afb6d9bbd24c78bc3fd067667d6e536f3 Parents: 0caa98c Author: Gilles Sadowski <[email protected]> Authored: Sun Jan 8 04:19:51 2017 +0100 Committer: Gilles Sadowski <[email protected]> Committed: Sun Jan 8 04:19:51 2017 +0100 ---------------------------------------------------------------------- .../apache/commons/complex/ComplexUtils.java | 46 +++++++++++++------- 1 file changed, 31 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-complex/blob/e5e3e49a/src/main/java/org/apache/commons/complex/ComplexUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/complex/ComplexUtils.java b/src/main/java/org/apache/commons/complex/ComplexUtils.java index e446306..165d907 100755 --- a/src/main/java/org/apache/commons/complex/ComplexUtils.java +++ b/src/main/java/org/apache/commons/complex/ComplexUtils.java @@ -52,7 +52,8 @@ public class ComplexUtils { * @since 1.1 */ public static Complex polar2Complex(double r, double theta) { - if (r < 0) { illegalArgument(); + if (r < 0) { + throw new NegativeModulusException(r); } return new Complex(r * Math.cos(theta), r * Math.sin(theta)); } @@ -71,7 +72,7 @@ public class ComplexUtils { final Complex[] c = new Complex[length]; for (int x = 0; x < length; x++) { if (r[x] < 0) { - illegalArgument(); + throw new NegativeModulusException(r[x]); } c[x] = new Complex(r[x] * Math.cos(theta[x]), r[x] * Math.sin(theta[x])); } @@ -743,7 +744,7 @@ public class ComplexUtils { */ public static double[][] complex2Interleaved(Complex[][] c, int interleavedDim) { if (interleavedDim > 1 || interleavedDim < 0) { - outOfRange(); + new IndexOutOfRangeException(interleavedDim); } final int width = c.length; final int height = c[0].length; @@ -796,7 +797,7 @@ public class ComplexUtils { */ public static double[][][] complex2Interleaved(Complex[][][] c, int interleavedDim) { if (interleavedDim > 2 || interleavedDim < 0) { - outOfRange(); + new IndexOutOfRangeException(interleavedDim); } int width = c.length; int height = c[0].length; @@ -864,7 +865,7 @@ public class ComplexUtils { */ public static float[][] complex2InterleavedFloat(Complex[][] c, int interleavedDim) { if (interleavedDim > 1 || interleavedDim < 0) { - outOfRange(); + new IndexOutOfRangeException(interleavedDim); } final int width = c.length; final int height = c[0].length; @@ -918,7 +919,7 @@ public class ComplexUtils { */ public static float[][][] complex2InterleavedFloat(Complex[][][] c, int interleavedDim) { if (interleavedDim > 2 || interleavedDim < 0) { - outOfRange(); + new IndexOutOfRangeException(interleavedDim); } final int width = c.length; final int height = c[0].length; @@ -986,7 +987,7 @@ public class ComplexUtils { */ public static Complex[][] interleaved2Complex(double[][] d, int interleavedDim) { if (interleavedDim > 1 || interleavedDim < 0) { - outOfRange(); + new IndexOutOfRangeException(interleavedDim); } final int width = d.length; final int height = d[0].length; @@ -1035,7 +1036,7 @@ public class ComplexUtils { */ public static Complex[][][] interleaved2Complex(double[][][] d, int interleavedDim) { if (interleavedDim > 2 || interleavedDim < 0) { - outOfRange(); + new IndexOutOfRangeException(interleavedDim); } final int width = d.length; final int height = d[0].length; @@ -1098,7 +1099,7 @@ public class ComplexUtils { */ public static Complex[][] interleaved2Complex(float[][] d, int interleavedDim) { if (interleavedDim > 1 || interleavedDim < 0) { - outOfRange(); + new IndexOutOfRangeException(interleavedDim); } final int width = d.length; final int height = d[0].length; @@ -1147,7 +1148,7 @@ public class ComplexUtils { */ public static Complex[][][] interleaved2Complex(float[][][] d, int interleavedDim) { if (interleavedDim > 2 || interleavedDim < 0) { - outOfRange(); + new IndexOutOfRangeException(interleavedDim); } final int width = d.length; final int height = d[0].length; @@ -1403,12 +1404,27 @@ public class ComplexUtils { return d; } - private static void illegalArgument() { - throw new IllegalArgumentException("ComplexUtils: Illegal argument"); + /** + * Exception to be throw when a negative value is passed as the modulus. + */ + private static class NegativeModulusException extends IllegalArgumentException { + /** + * @param r Wrong modulus. + */ + NegativeModulusException(double r) { + super("Modulus is negative: " + r); + } } - private static void outOfRange() { - throw new IllegalArgumentException("ComplexUtils: Out of range"); + /** + * Exception to be throw when an out-of-range index value is passed. + */ + private static class IndexOutOfRangeException extends IllegalArgumentException { + /** + * @param i Wrong index. + */ + IndexOutOfRangeException(int i) { + super("Out of range: " + i); + } } - }
