From b5a23d01b65fcd23d03769e0db3918b6b308bf91 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Thu, 8 Jul 2010 23:45:06 +0000 Subject: [PATCH] MATH-361 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@962306 13f79535-47bb-0310-9956-ffa450edef68 --- .../interpolation/LinearInterpolator.java | 5 +- .../interpolation/SplineInterpolator.java | 4 +- .../MathIllegalArgumentException.java | 46 ++++++++++++++++--- .../exception/MathIllegalNumberException.java | 22 +++++++-- .../exception/NumberIsTooSmallException.java | 20 +++++++- .../commons/math/util/LocalizedFormats.java | 1 + .../LocalizedFormats_fr.properties | 1 + 7 files changed, 85 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/commons/math/analysis/interpolation/LinearInterpolator.java b/src/main/java/org/apache/commons/math/analysis/interpolation/LinearInterpolator.java index 11e21485f..98c8f34f2 100644 --- a/src/main/java/org/apache/commons/math/analysis/interpolation/LinearInterpolator.java +++ b/src/main/java/org/apache/commons/math/analysis/interpolation/LinearInterpolator.java @@ -21,6 +21,7 @@ import org.apache.commons.math.exception.NumberIsTooSmallException; import org.apache.commons.math.analysis.polynomials.PolynomialFunction; import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; import org.apache.commons.math.util.MathUtils; +import org.apache.commons.math.util.LocalizedFormats; /** * Implements a linear function for interpolation of real univariate functions. @@ -44,7 +45,8 @@ public class LinearInterpolator implements UnivariateRealInterpolator { } if (x.length < 2) { - throw new NumberIsTooSmallException(x.length, 2, true); + throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS, + x.length, 2, true); } // Number of intervals. The number of data points is n + 1. @@ -68,5 +70,4 @@ public class LinearInterpolator implements UnivariateRealInterpolator { return new PolynomialSplineFunction(x, polynomials); } - } diff --git a/src/main/java/org/apache/commons/math/analysis/interpolation/SplineInterpolator.java b/src/main/java/org/apache/commons/math/analysis/interpolation/SplineInterpolator.java index 288c6b9af..9eb124711 100644 --- a/src/main/java/org/apache/commons/math/analysis/interpolation/SplineInterpolator.java +++ b/src/main/java/org/apache/commons/math/analysis/interpolation/SplineInterpolator.java @@ -21,6 +21,7 @@ import org.apache.commons.math.exception.NumberIsTooSmallException; import org.apache.commons.math.analysis.polynomials.PolynomialFunction; import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; import org.apache.commons.math.util.MathUtils; +import org.apache.commons.math.util.LocalizedFormats; /** * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set. @@ -69,7 +70,8 @@ public class SplineInterpolator implements UnivariateRealInterpolator { } if (x.length < 3) { - throw new NumberIsTooSmallException(x.length, 3, true); + throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS, + x.length, 3, true); } // Number of intervals. The number of data points is n + 1. diff --git a/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java b/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java index 5fb3a0d9c..3ebfb7eb6 100644 --- a/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java +++ b/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java @@ -33,34 +33,66 @@ import org.apache.commons.math.util.Localizable; */ public class MathIllegalArgumentException extends IllegalArgumentException { /** - * Pattern used to build the message. + * Pattern used to build the message (specific context). */ - private final Localizable pattern; + private final Localizable specific; + /** + * Pattern used to build the message (general problem description). + */ + private final Localizable general; /** * Arguments used to build the message. */ private final Object[] arguments; /** - * @param pattern Message pattern. + * @param specific Message pattern providing the specific context of + * the error. + * @param general Message pattern explaining the cause of the error. * @param args Arguments. */ - protected MathIllegalArgumentException(Localizable pattern, + protected MathIllegalArgumentException(Localizable specific, + Localizable general, Object ... args) { - this.pattern = pattern; + this.specific = specific; + this.general = general; arguments = flatten(args).toArray(); } + /** + * @param general Message pattern explaining the cause of the error. + * @param args Arguments. + */ + protected MathIllegalArgumentException(Localizable general, + Object ... args) { + this(null, general, args); + } /** {@inheritDoc} */ @Override public String getMessage() { - return MessageFactory.buildMessage(Locale.US, pattern, arguments); + final StringBuilder sb = new StringBuilder(); + + if (specific != null) { + sb.append(MessageFactory.buildMessage(Locale.US, specific, arguments)); + sb.append(": "); + } + sb.append(MessageFactory.buildMessage(Locale.US, general, arguments)); + + return sb.toString(); } /** {@inheritDoc} */ @Override public String getLocalizedMessage() { - return MessageFactory.buildMessage(Locale.getDefault(), pattern, arguments); + final StringBuilder sb = new StringBuilder(); + + if (specific != null) { + sb.append(MessageFactory.buildMessage(Locale.getDefault(), specific, arguments)); + sb.append(": "); + } + sb.append(MessageFactory.buildMessage(Locale.getDefault(), general, arguments)); + + return sb.toString(); } /** diff --git a/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java b/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java index dde06d746..ccf7b2c16 100644 --- a/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java +++ b/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java @@ -34,14 +34,30 @@ public class MathIllegalNumberException extends MathIllegalArgumentException { /** * Construct an exception. * - * @param pattern Localizable pattern. + * @param specific Localizable pattern. + * @param general Localizable pattern. * @param arguments Arguments. The first element must be the requested * value that raised the exception. */ - protected MathIllegalNumberException(Localizable pattern, + protected MathIllegalNumberException(Localizable specific, + Localizable general, Number wrong, Object ... arguments) { - super(pattern, wrong, arguments); + super(specific, general, wrong, arguments); + argument = wrong; + } + + /** + * Construct an exception. + * + * @param general Localizable pattern. + * @param arguments Arguments. The first element must be the requested + * value that raised the exception. + */ + protected MathIllegalNumberException(Localizable general, + Number wrong, + Object ... arguments) { + super(general, wrong, arguments); argument = wrong; } diff --git a/src/main/java/org/apache/commons/math/exception/NumberIsTooSmallException.java b/src/main/java/org/apache/commons/math/exception/NumberIsTooSmallException.java index 0a9fdc928..ee8f5dd41 100644 --- a/src/main/java/org/apache/commons/math/exception/NumberIsTooSmallException.java +++ b/src/main/java/org/apache/commons/math/exception/NumberIsTooSmallException.java @@ -16,6 +16,7 @@ */ package org.apache.commons.math.exception; +import org.apache.commons.math.util.Localizable; import org.apache.commons.math.util.LocalizedFormats; /** @@ -39,11 +40,28 @@ public class NumberIsTooSmallException extends MathIllegalNumberException { * * @param wrong Value that is smaller than the minimum. * @param min minimum. + * @param boundIsAllowed Whether {@code min} is included in the allowed range. */ public NumberIsTooSmallException(Number wrong, Number min, boolean boundIsAllowed) { - super((boundIsAllowed ? + this(null, wrong, min, boundIsAllowed); + } + + /** + * Construct the exception with a specific context. + * + * @param specific Specific contexte pattern . + * @param wrong Value that is smaller than the minimum. + * @param min minimum. + * @param boundIsAllowed Whether {@code min} is included in the allowed range. + */ + public NumberIsTooSmallException(Localizable specific, + Number wrong, + Number min, + boolean boundIsAllowed) { + super(specific, + (boundIsAllowed ? LocalizedFormats.NUMBER_TOO_SMALL : LocalizedFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED), wrong, min); 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 c3bd9e50b..ac4496280 100644 --- a/src/main/java/org/apache/commons/math/util/LocalizedFormats.java +++ b/src/main/java/org/apache/commons/math/util/LocalizedFormats.java @@ -279,6 +279,7 @@ public enum LocalizedFormats implements Localizable { WEIGHT_AT_LEAST_ONE_NON_ZERO("weigth array must contain at least one non-zero value"), WRONG_BLOCK_LENGTH("wrong array shape (block length = {0}, expected {1})"), WRONG_NUMBER_OF_POINTS("{0} points are required, got only {1}"), + NUMBER_OF_POINTS("number of points ({0})"), /* keep */ ZERO_DENOMINATOR("denominator must be different from 0"), ZERO_DENOMINATOR_IN_FRACTION("zero denominator in fraction {0}/{1}"), ZERO_FRACTION_TO_DIVIDE_BY("the fraction to divide by must not be zero: {0}/{1}"), diff --git a/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties b/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties index f40a507e9..6d1f7aa5b 100644 --- a/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties +++ b/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties @@ -251,6 +251,7 @@ VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT = un vecteur doit comporter au moins un \u WEIGHT_AT_LEAST_ONE_NON_ZERO = le tableau des poids doit contenir au moins une valeur non nulle WRONG_BLOCK_LENGTH = forme de tableau erron\u00e9e (bloc de longueur {0} au lieu des {1} attendus WRONG_NUMBER_OF_POINTS = {0} sont n\u00e9cessaires, seuls {1} ont \u00e9t\u00e9 fournis +NUMBER_OF_POINTS = nombre de points ({0}) ZERO_DENOMINATOR = le d\u00e9nominateur doit \u00eatre diff\u00e9rent de 0 ZERO_DENOMINATOR_IN_FRACTION = d\u00e9nominateur null dans le nombre rationnel {0}/{1} ZERO_FRACTION_TO_DIVIDE_BY = division par un nombre rationnel nul : {0}/{1}