From 9a8bd27fe5ba231585b29aab664a0a2e7e8f9351 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Sun, 1 Aug 2010 16:09:56 +0000 Subject: [PATCH] improved error messages git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@981254 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/math/genetics/ListPopulation.java | 11 ++++++++--- .../commons/math/util/LocalizedFormats.java | 4 ++++ .../apache/commons/math/util/MathUtils.java | 18 +++++++++--------- .../LocalizedFormats_fr.properties | 6 +++++- .../commons/math/util/MathUtilsTest.java | 3 ++- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/commons/math/genetics/ListPopulation.java b/src/main/java/org/apache/commons/math/genetics/ListPopulation.java index ab22a9a21..3799e47df 100644 --- a/src/main/java/org/apache/commons/math/genetics/ListPopulation.java +++ b/src/main/java/org/apache/commons/math/genetics/ListPopulation.java @@ -20,6 +20,10 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import org.apache.commons.math.exception.NotPositiveException; +import org.apache.commons.math.exception.NumberIsTooLargeException; +import org.apache.commons.math.util.LocalizedFormats; + /** * Population of chromosomes represented by a {@link List}. * @@ -43,10 +47,11 @@ public abstract class ListPopulation implements Population { */ public ListPopulation (List chromosomes, int populationLimit) { if (chromosomes.size() > populationLimit) { - throw new IllegalArgumentException("List of chromosomes bigger than maxPopulationSize."); + throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE, + chromosomes.size(), populationLimit, false); } if (populationLimit < 0) { - throw new IllegalArgumentException("Population limit has to be >= 0"); + throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit); } this.chromosomes = chromosomes; @@ -61,7 +66,7 @@ public abstract class ListPopulation implements Population { */ public ListPopulation (int populationLimit) { if (populationLimit < 0) { - throw new IllegalArgumentException("Population limit has to be >= 0"); + throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit); } this.populationLimit = populationLimit; this.chromosomes = new ArrayList(populationLimit); diff --git a/src/main/java/org/apache/commons/math/util/LocalizedFormats.java b/src/main/java/org/apache/commons/math/util/LocalizedFormats.java index ed1cca112..b6228efb9 100644 --- a/src/main/java/org/apache/commons/math/util/LocalizedFormats.java +++ b/src/main/java/org/apache/commons/math/util/LocalizedFormats.java @@ -131,6 +131,7 @@ public enum LocalizedFormats implements Localizable { ITERATOR_EXHAUSTED("iterator exhausted"), LCM_OVERFLOW_32_BITS("overflow: lcm({0}, {1}) is 2^31"), LCM_OVERFLOW_64_BITS("overflow: lcm({0}, {1}) is 2^63"), + LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE("list of chromosomes bigger than maxPopulationSize"), LOESS_EXPECTS_AT_LEAST_ONE_POINT("Loess expects at least 1 point"), LOWER_BOUND_NOT_BELOW_UPPER_BOUND("lower bound ({0}) must be strictly less than upper bound ({1})"), /* keep */ LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT("lower endpoint ({0}) must be less than or equal to upper endpoint ({1})"), @@ -238,10 +239,13 @@ public enum LocalizedFormats implements Localizable { OUT_OF_RANGE_ROOT_OF_UNITY_INDEX("out of range root of unity index {0} (must be in [{1};{2}])"), OUT_OF_RANGE_SIMPLE("{0} out of [{1}, {2}] range"), /* keep */ OVERFLOW_IN_FRACTION("overflow in fraction {0}/{1}, cannot negate"), + OVERFLOW_IN_ADDITION("overflow in addition: {0} + {1}"), + OVERFLOW_IN_SUBTRACTION("overflow in subtraction: {0} - {1}"), PERCENTILE_IMPLEMENTATION_CANNOT_ACCESS_METHOD("cannot access {0} method in percentile implementation {1}"), PERCENTILE_IMPLEMENTATION_UNSUPPORTED_METHOD("percentile implementation {0} does not support {1}"), PERMUTATION_EXCEEDS_N("permutation size ({0}) exceeds permuation domain ({1})"), /* keep */ POLYNOMIAL_INTERPOLANTS_MISMATCH_SEGMENTS("number of polynomial interpolants must match the number of segments ({0} != {1} - 1)"), + POPULATION_LIMIT_NOT_POSITIVE("population limit has to be positive"), POSITION_SIZE_MISMATCH_INPUT_ARRAY("position {0} and size {1} don't fit to the size of the input array {2}"), POWER_NEGATIVE_PARAMETERS("cannot raise an integral value to a negative power ({0}^{1})"), PROPAGATION_DIRECTION_MISMATCH("propagation direction mismatch"), diff --git a/src/main/java/org/apache/commons/math/util/MathUtils.java b/src/main/java/org/apache/commons/math/util/MathUtils.java index d11393499..23168a59a 100644 --- a/src/main/java/org/apache/commons/math/util/MathUtils.java +++ b/src/main/java/org/apache/commons/math/util/MathUtils.java @@ -99,7 +99,7 @@ public final class MathUtils { public static int addAndCheck(int x, int y) { long s = (long)x + (long)y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { - throw new ArithmeticException("overflow: add"); + throw MathRuntimeException.createArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, x, y); } return (int)s; } @@ -115,7 +115,7 @@ public final class MathUtils { * @since 1.2 */ public static long addAndCheck(long a, long b) { - return addAndCheck(a, b, "overflow: add"); + return addAndCheck(a, b, LocalizedFormats.OVERFLOW_IN_ADDITION); } /** @@ -123,17 +123,17 @@ public final class MathUtils { * * @param a an addend * @param b an addend - * @param msg the message to use for any thrown exception. + * @param pattern the pattern to use for any thrown exception. * @return the sum a+b * @throws ArithmeticException if the result can not be represented as an * long * @since 1.2 */ - private static long addAndCheck(long a, long b, String msg) { + private static long addAndCheck(long a, long b, Localizable pattern) { long ret; if (a > b) { // use symmetry to reduce boundary cases - ret = addAndCheck(b, a, msg); + ret = addAndCheck(b, a, pattern); } else { // assert a <= b @@ -143,7 +143,7 @@ public final class MathUtils { if (Long.MIN_VALUE - b <= a) { ret = a + b; } else { - throw new ArithmeticException(msg); + throw MathRuntimeException.createArithmeticException(pattern, a, b); } } else { // opposite sign addition is always safe @@ -157,7 +157,7 @@ public final class MathUtils { if (a <= Long.MAX_VALUE - b) { ret = a + b; } else { - throw new ArithmeticException(msg); + throw MathRuntimeException.createArithmeticException(pattern, a, b); } } } @@ -1544,7 +1544,7 @@ public final class MathUtils { public static int subAndCheck(int x, int y) { long s = (long)x - (long)y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { - throw new ArithmeticException("overflow: subtract"); + throw MathRuntimeException.createArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, x, y); } return (int)s; } @@ -1570,7 +1570,7 @@ public final class MathUtils { } } else { // use additive inverse - ret = addAndCheck(a, -b, msg); + ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION); } return ret; } diff --git a/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties b/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties index 025b7f44d..67d5624f7 100644 --- a/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties +++ b/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties @@ -102,6 +102,7 @@ INVALID_ROUNDING_METHOD = m\u00e9thode d''arondi {0} invalide, m\u00e9thodes val ITERATOR_EXHAUSTED = it\u00e9ration achev\u00e9e LCM_OVERFLOW_32_BITS = d\u00e9passement de capacit\u00e9 : le MCM de {0} et {1} vaut 2^31 LCM_OVERFLOW_64_BITS = d\u00e9passement de capacit\u00e9 : le MCM de {0} et {1} vaut 2^63 +LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE = la liste des chromosomes d\u00e9passe maxPopulationSize LOESS_EXPECTS_AT_LEAST_ONE_POINT = la r\u00e9gression Loess n\u00e9cessite au moins un point LOWER_BOUND_NOT_BELOW_UPPER_BOUND = la borne inf\u00e9rieure ({0}) doit \u00eatre strictement plus petite que la borne sup\u00e9rieure ({1}) LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT = la borne inf\u00e9rieure ({0}) devrait \u00eatre inf\u00e9rieure @@ -198,7 +199,7 @@ NUMBER_TOO_LARGE = {0} est plus grand que le maximum ({1}) NUMBER_TOO_SMALL = {0} est plus petit que le minimum ({1}) NUMBER_TOO_LARGE_BOUND_EXCLUDED = {0} n''est pas strictement plus grand que le maximum ({1}) NUMBER_TOO_SMALL_BOUND_EXCLUDED = {0} n''est pas strictement plus petit que le minimum ({1}) -NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE = le nombre de succ\u00e8s doit \u00eatre inf\u00e9rieur +NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE = le nombre de succ\u00e8s ({0}) doit \u00eatre inf\u00e9rieur ou \u00e9gal \u00e0 la taille de la population ({1}) NUMERATOR_OVERFLOW_AFTER_MULTIPLY = d\u00e9passement de capacit\u00e9 pour le num\u00e9rateur apr\u00e8s multiplication : {0} N_POINTS_GAUSS_LEGENDRE_INTEGRATOR_NOT_SUPPORTED = l''int\u00e9grateur de Legendre-Gauss en {0} points n''est pas disponible, le nombre de points doit \u00eatre entre {1} et {2} OBSERVED_COUNTS_ALL_ZERO = aucune occurrence dans le tableau des observations {0} @@ -209,10 +210,13 @@ OUT_OF_ORDER_ABSCISSA_ARRAY = les abscisses doivent \u00eatre en ordre stricteme OUT_OF_RANGE_ROOT_OF_UNITY_INDEX = l''indice de racine de l''unit\u00e9 {0} est hors du domaine autoris\u00e9 [{1};{2}] OUT_OF_RANGE_SIMPLE = {0} hors du domaine [{1}, {2}] OVERFLOW_IN_FRACTION = d\u00e9passement de capacit\u00e9 pour la fraction {0}/{1}, son signe ne peut \u00eatre chang\u00e9 +OVERFLOW_IN_ADDITION = d\u00e9passement de capacit\u00e9 pour l''addition : {0} + {1} +OVERFLOW_IN_SUBTRACTION = d\u00e9passement de capacit\u00e9 pour la soustraction : {0} - {1} PERCENTILE_IMPLEMENTATION_CANNOT_ACCESS_METHOD = acc\u00e8s impossible \u00e0 la m\u00e9thode {0} PERCENTILE_IMPLEMENTATION_UNSUPPORTED_METHOD = l''implantation de pourcentage {0} ne dispose pas de la m\u00e9thode {1} PERMUTATION_EXCEEDS_N = la taille de la permutation ({0}) d\u00e9passe le domaine de la permutation ({1}) POLYNOMIAL_INTERPOLANTS_MISMATCH_SEGMENTS = le nombre d''interpolants polyn\u00f4miaux doit correspondre au nombre de segments ({0} != {1} - 1) +POPULATION_LIMIT_NOT_POSITIVE = la limite de population doit \u00eatre positive POSITION_SIZE_MISMATCH_INPUT_ARRAY = la position {0} et la taille {1} sont incompatibles avec la taille du tableau d''entr\u00e9e {2} POWER_NEGATIVE_PARAMETERS = impossible d''\u00e9lever une valeur enti\u00e8re PROPAGATION_DIRECTION_MISMATCH = directions de propagation incoh\u00e9rentes diff --git a/src/test/java/org/apache/commons/math/util/MathUtilsTest.java b/src/test/java/org/apache/commons/math/util/MathUtilsTest.java index f3d6e0d98..0c0f3287c 100644 --- a/src/test/java/org/apache/commons/math/util/MathUtilsTest.java +++ b/src/test/java/org/apache/commons/math/util/MathUtilsTest.java @@ -18,6 +18,7 @@ import java.math.BigInteger; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import junit.framework.TestCase; @@ -1328,7 +1329,7 @@ public final class MathUtilsTest extends TestCase { MathUtils.subAndCheck(big, -1); fail("Expecting ArithmeticException"); } catch (ArithmeticException ex) { - assertEquals("overflow: subtract", ex.getMessage()); + assertTrue(ex.getMessage().length() > 1); } }