From 4102ba5dfb2bd58d49c4a60687c65bd2919af597 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Fri, 25 Jun 2010 13:00:41 +0000 Subject: [PATCH] MATH-361 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@957930 13f79535-47bb-0310-9956-ffa450edef68 --- .../exception/DimensionMismatchException.java | 35 +++++------- .../MathIllegalArgumentException.java | 35 +++++++++--- .../exception/MathIllegalNumberException.java | 54 +++++++++++++++++++ .../math/exception/MessageFactory.java | 3 +- .../math/exception/NotPositiveException.java | 36 +++++++++++++ .../NotStrictlyPositiveException.java | 36 +++++++++++++ .../math/exception/OutOfRangeException.java | 19 ++----- .../commons/math/util/LocalizedFormats.java | 6 ++- .../LocalizedFormats_fr.properties | 2 + 9 files changed, 178 insertions(+), 48 deletions(-) create mode 100644 src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java create mode 100644 src/main/java/org/apache/commons/math/exception/NotPositiveException.java create mode 100644 src/main/java/org/apache/commons/math/exception/NotStrictlyPositiveException.java diff --git a/src/main/java/org/apache/commons/math/exception/DimensionMismatchException.java b/src/main/java/org/apache/commons/math/exception/DimensionMismatchException.java index 765f0f839..65b722c05 100644 --- a/src/main/java/org/apache/commons/math/exception/DimensionMismatchException.java +++ b/src/main/java/org/apache/commons/math/exception/DimensionMismatchException.java @@ -24,37 +24,26 @@ import org.apache.commons.math.util.LocalizedFormats; * @since 2.2 * @version $Revision$ $Date$ */ -public class DimensionMismatchException extends MathIllegalArgumentException { - /** First dimension. */ - private final int dimension1; - - /** Second dimension. */ - private final int dimension2; +public class DimensionMismatchException extends MathIllegalNumberException { + /** Correct dimension. */ + private final int dimension; /** * Construct an exception from the mismatched dimensions. * - * @param dimension1 First dimension. - * @param dimension2 Second dimension. + * @param wrong Wrong dimension. + * @param expected Expected dimension. */ - public DimensionMismatchException(int dimension1, - int dimension2) { - super(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, dimension1, dimension2); - - this.dimension1 = dimension1; - this.dimension2 = dimension2; + public DimensionMismatchException(int wrong, + int expected) { + super(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, wrong, expected); + dimension = expected; } /** - * @return the first dimension. + * @return the expected dimension. */ - public int getDimension1() { - return dimension1; - } - /** - * @return the second dimension. - */ - public int getDimension2() { - return dimension2; + public int getDimension() { + return dimension; } } 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 a78f77a10..bc985810b 100644 --- a/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java +++ b/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java @@ -17,14 +17,16 @@ package org.apache.commons.math.exception; import java.util.Locale; +import java.util.List; +import java.util.ArrayList; import org.apache.commons.math.util.Localizable; /** * Base class for all preconditions violation exceptions. - * This class is not intended to be instantiated directly in most case: it - * should serve as a base class to create all the exceptions that share the - * semantics of the standard {@link IllegalArgumentException}, but must also - * provide a localized message. + * This class is not intended to be instantiated directly: it should serve + * as a base class to create all the exceptions that share the semantics of + * the standard {@link IllegalArgumentException}, but must also provide a + * localized message. * * @since 2.2 * @version $Revision$ $Date$ @@ -41,12 +43,12 @@ public class MathIllegalArgumentException extends IllegalArgumentException { /** * @param pattern Message pattern. - * @param arguments Arguments. + * @param args Arguments. */ protected MathIllegalArgumentException(Localizable pattern, - Object ... arguments) { + Object ... args) { this.pattern = pattern; - this.arguments = arguments.clone(); + arguments = flatten(args).toArray(); } /** {@inheritDoc} */ @@ -60,4 +62,23 @@ public class MathIllegalArgumentException extends IllegalArgumentException { public String getLocalizedMessage() { return MessageFactory.buildMessage(Locale.getDefault(), pattern, arguments); } + + /** + * Transform a multidimensional array into a one-dimensional list. + * + * @param array Array (possibly multidimensional). + * @return a list of all the {@code Object} instances contained in + * {@code array}. + */ + private List flatten(Object[] array) { + final List list = new ArrayList(); + for (Object o : array) { + if (o instanceof Object[]) { + list.addAll(flatten((Object[]) o)); + } else { + list.add(o); + } + } + return list; + } } diff --git a/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java b/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java new file mode 100644 index 000000000..cfac79dd8 --- /dev/null +++ b/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import org.apache.commons.math.util.Localizable; + +/** + * Base class for exceptions raised by a wrong number. + * This class is not intended to be instantiated directly: it should serve + * as a base class to create all the exceptions that are raised because some + * precondition is violated by a number argument. + * + * @since 2.2 + * @version $Revision$ $Date$ + */ +public class MathIllegalNumberException extends MathIllegalArgumentException { + /** Requested. */ + private final Number argument; + + /** + * Construct an exception. + * + * @param Localizable pattern. + * @param arguments Arguments. The first element must be the requested + * value that raised the exception. + */ + protected MathIllegalNumberException(Localizable pattern, + Number wrong, + Object ... arguments) { + super(pattern, wrong, arguments); + argument = wrong; + } + + /** + * @return the requested value. + */ + public Number getArgument() { + return argument; + } +} diff --git a/src/main/java/org/apache/commons/math/exception/MessageFactory.java b/src/main/java/org/apache/commons/math/exception/MessageFactory.java index c17aa556b..9a08cdee1 100644 --- a/src/main/java/org/apache/commons/math/exception/MessageFactory.java +++ b/src/main/java/org/apache/commons/math/exception/MessageFactory.java @@ -44,6 +44,7 @@ public class MessageFactory { public static String buildMessage(Locale locale, Localizable pattern, Object ... arguments) { - return new MessageFormat(pattern.getLocalizedString(locale), locale).format(arguments); + final String locPattern = pattern.getLocalizedString(locale); + return (new MessageFormat(locPattern, locale)).format(arguments); } } diff --git a/src/main/java/org/apache/commons/math/exception/NotPositiveException.java b/src/main/java/org/apache/commons/math/exception/NotPositiveException.java new file mode 100644 index 000000000..251381494 --- /dev/null +++ b/src/main/java/org/apache/commons/math/exception/NotPositiveException.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import org.apache.commons.math.util.LocalizedFormats; + +/** + * Exception to be thrown when the argument is negative. + * + * @since 2.2 + * @version $Revision$ $Date$ + */ +public class NotPositiveException extends MathIllegalNumberException { + /** + * Construct the exception. + * + * @param value Argument. + */ + public NotPositiveException(Number value) { + super(LocalizedFormats.NOT_POSITIVE, value); + } +} diff --git a/src/main/java/org/apache/commons/math/exception/NotStrictlyPositiveException.java b/src/main/java/org/apache/commons/math/exception/NotStrictlyPositiveException.java new file mode 100644 index 000000000..b2f422904 --- /dev/null +++ b/src/main/java/org/apache/commons/math/exception/NotStrictlyPositiveException.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import org.apache.commons.math.util.LocalizedFormats; + +/** + * Exception to be thrown when the argument is negative. + * + * @since 2.2 + * @version $Revision$ $Date$ + */ +public class NotStrictlyPositiveException extends MathIllegalNumberException { + /** + * Construct the exception. + * + * @param value Argument. + */ + public NotStrictlyPositiveException(Number value) { + super(LocalizedFormats.NOT_STRICTLY_POSITIVE, value); + } +} diff --git a/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java b/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java index cf297bd66..0ae1e3e15 100644 --- a/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java +++ b/src/main/java/org/apache/commons/math/exception/OutOfRangeException.java @@ -24,37 +24,26 @@ import org.apache.commons.math.util.LocalizedFormats; * @since 2.2 * @version $Revision$ $Date$ */ -public class OutOfRangeException extends MathIllegalArgumentException { +public class OutOfRangeException extends MathIllegalNumberException { /** Lower bound. */ private final Number lo; /** Higher bound. */ private final Number hi; - /** Requested. */ - private final Number requested; /** * Construct an exception from the mismatched dimensions. * - * @param requested Requested value. + * @param wrong Requested value. * @param lo Lower bound. * @param hi Higher bound. */ - public OutOfRangeException(Number requested, + public OutOfRangeException(Number wrong, Number lo, Number hi) { - super(LocalizedFormats.OUT_OF_RANGE_SIMPLE, requested, lo, hi); - - this.requested = requested; + super(LocalizedFormats.OUT_OF_RANGE_SIMPLE, wrong, lo, hi); this.lo = lo; this.hi = hi; } - - /** - * @return the requested value. - */ - public Number getRequested() { - return requested; - } /** * @return the lower bound. */ 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 9b74ca820..627ec8473 100644 --- a/src/main/java/org/apache/commons/math/util/LocalizedFormats.java +++ b/src/main/java/org/apache/commons/math/util/LocalizedFormats.java @@ -80,7 +80,7 @@ public enum LocalizedFormats implements Localizable { DIFFERENT_ROWS_LENGTHS("some rows have length {0} while others have length {1}"), DIGEST_NOT_INITIALIZED("digest not initialized"), DIMENSIONS_MISMATCH_2x2("dimensions mismatch: got {0}x{1} but expected {2}x{3}"), - DIMENSIONS_MISMATCH_SIMPLE("dimensions mismatch {0} != {1}"), + DIMENSIONS_MISMATCH_SIMPLE("dimensions mismatch {0} != {1}"), /* keep */ DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN("Discrete cumulative probability function returned NaN for argument {0}"), DISTRIBUTION_NOT_LOADED("distribution not loaded"), DUPLICATED_ABSCISSA("Abscissa {0} is duplicated at both indices {1} and {2}"), @@ -164,6 +164,8 @@ public enum LocalizedFormats implements Localizable { NOT_ENOUGH_POINTS_IN_SPLINE_PARTITION("spline partition must have at least {0} points, got {1}"), NOT_INCREASING_NUMBER_OF_POINTS("points {0} and {1} are not increasing ({2} > {3})"), NOT_MULTIPLICATION_COMPATIBLE_MATRICES("{0}x{1} and {2}x{3} matrices are not multiplication compatible"), + NOT_STRICTLY_POSITIVE("{0} is not strictly positive"), /* keep */ + NOT_POSITIVE("{0} is not positive"), /* keep */ NOT_POSITIVE_ALPHA("alpha must be positive ({0})"), NOT_POSITIVE_BETA("beta must be positive ({0})"), NOT_POSITIVE_COLUMNDIMENSION("invalid column dimension: {0} (must be positive)"), @@ -222,7 +224,7 @@ public enum LocalizedFormats implements Localizable { OUT_OF_BOUND_SIGNIFICANCE_LEVEL("out of bounds significance level {0}, must be between {1} and {2}"), OUT_OF_ORDER_ABSCISSA_ARRAY("the abscissae array must be sorted in a strictly increasing order, but the {0}-th element is {1} whereas {2}-th is {3}"), 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"), + OUT_OF_RANGE_SIMPLE("{0} out of [{1}, {2}] range"), /* keep */ OVERFLOW_IN_FRACTION("overflow in fraction {0}/{1}, cannot negate"), PERCENTILE_IMPLEMENTATION_CANNOT_ACCESS_METHOD("cannot access {0} method in percentile implementation {1}"), PERCENTILE_IMPLEMENTATION_UNSUPPORTED_METHOD("percentile implementation {0} does not support {1}"), diff --git a/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties b/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties index dad3cfd0e..4849d2b30 100644 --- a/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties +++ b/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties @@ -136,6 +136,8 @@ NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS = pas assez de donn\u00e9es ({0} lignes NOT_ENOUGH_POINTS_IN_SPLINE_PARTITION = une partiction spline n\u00e9cessite au moins {0} points, seuls {1} ont \u00e9t\u00e9 fournis NOT_INCREASING_NUMBER_OF_POINTS = les points {0} et {1} ne sont pas croissants ({2} > {3}) NOT_MULTIPLICATION_COMPATIBLE_MATRICES = les dimensions {0}x{1} et {2}x{3} sont incompatibles pour la multiplication matricielle +NOT_STRICTLY_POSITIVE = {0} n''est pas strictement positif +NOT_POSITIVE = {0} n''est pas positif NOT_POSITIVE_ALPHA = alpha doit \u00eatre positif ({0}) NOT_POSITIVE_BETA = beta doit \u00eatre positif ({0}) NOT_POSITIVE_COLUMNDIMENSION = nombre de colonnes invalide : {0} (doit \u00eatre positif)