Added and used a specialized exception for arguments outside domains

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@506595 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2007-02-12 19:28:57 +00:00
parent 1bd2978235
commit ee556907bb
3 changed files with 66 additions and 7 deletions

View File

@ -0,0 +1,21 @@
package org.apache.commons.math;
public class ArgumentOutsideDomainException extends FunctionEvaluationException {
/** Serializable version identifier. */
private static final long serialVersionUID = -4965972841162580234L;
/**
* Constructs an exception with specified formatted detail message.
* Message formatting is delegated to {@link java.text.MessageFormat}.
* @param argument the failing function argument
* @param lower lower bound of the domain
* @param upper upper bound of the domain
*/
public ArgumentOutsideDomainException(double argument, double lower, double upper) {
super(argument,
"Argument {0} outside domain [{1} ; {2}]",
new Object[] { new Double(argument), new Double(lower), new Double(upper) });
}
}

View File

@ -19,7 +19,7 @@ package org.apache.commons.math.analysis;
import java.io.Serializable;
import java.util.Arrays;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.ArgumentOutsideDomainException;
/**
* Represents a polynomial spline function.
@ -55,10 +55,10 @@ import org.apache.commons.math.FunctionEvaluationException;
*/
public class PolynomialSplineFunction
implements DifferentiableUnivariateRealFunction, Serializable {
/** Serializable version identifier */
private static final long serialVersionUID = 7011031166416885789L;
private static final long serialVersionUID = 1619940313389547244L;
/** Spline segment interval delimiters (knots). Size is n+1 for n segments. */
private double knots[];
@ -125,13 +125,13 @@ public class PolynomialSplineFunction
*
* @param v the point for which the function value should be computed
* @return the value
* @throws FunctionEvaluationException if v is outside of the domain of
* @throws ArgumentOutsideDomainException if v is outside of the domain of
* of the spline function (less than the smallest knot point or greater
* than the largest knot point)
*/
public double value(double v) throws FunctionEvaluationException {
public double value(double v) throws ArgumentOutsideDomainException {
if (v < knots[0] || v > knots[n]) {
throw new FunctionEvaluationException(v,"Argument outside domain");
throw new ArgumentOutsideDomainException(v, knots[0], knots[n]);
}
int i = Arrays.binarySearch(knots, v);
if (i < 0) {

View File

@ -0,0 +1,38 @@
/*
* 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;
import java.util.Locale;
import junit.framework.TestCase;
/**
* @version $Revision:$
*/
public class ArgumentOutsideDomainExceptionTest extends TestCase {
public void testConstructor(){
ArgumentOutsideDomainException ex = new ArgumentOutsideDomainException(Math.PI, 10.0, 20.0);
assertNull(ex.getCause());
assertNotNull(ex.getMessage());
assertTrue(ex.getMessage().indexOf("3.14") > 0);
assertEquals(Math.PI, ex.getArgument(), 0);
assertFalse(ex.getMessage().equals(ex.getMessage(Locale.FRENCH)));
}
}