git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@957930 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2010-06-25 13:00:41 +00:00
parent 66df76d89a
commit 4102ba5dfb
9 changed files with 178 additions and 48 deletions

View File

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

View File

@ -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<Object> flatten(Object[] array) {
final List<Object> list = new ArrayList<Object>();
for (Object o : array) {
if (o instanceof Object[]) {
list.addAll(flatten((Object[]) o));
} else {
list.add(o);
}
}
return list;
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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