improved error messages

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@981254 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2010-08-01 16:09:56 +00:00
parent df328dbd46
commit 9a8bd27fe5
5 changed files with 28 additions and 14 deletions

View File

@ -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<Chromosome> 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<Chromosome>(populationLimit);

View File

@ -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"),

View File

@ -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 <code>a+b</code>
* @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;
}

View File

@ -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

View File

@ -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);
}
}