git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@962306 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2010-07-08 23:45:06 +00:00
parent 45f683a436
commit b5a23d01b6
7 changed files with 85 additions and 14 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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