Added FunctionEvaluationException and test class.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141385 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f2a78ef3b1
commit
24a29e0177
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright 2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed 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;
|
||||
|
||||
/**
|
||||
* Exeption thrown when an error occurs evaluating a function.
|
||||
* <p>
|
||||
* Maintains an <code>argument</code> property holding the input value that
|
||||
* caused the function evaluation to fail.
|
||||
*
|
||||
* @version $Revision: 1.1 $ $Date: 2004/07/17 19:41:05 $
|
||||
*/
|
||||
public class FunctionEvaluationException extends MathException {
|
||||
|
||||
/** Serializable version identifier */
|
||||
static final long serialVersionUID = -317289374378977972L;
|
||||
|
||||
/** Argument causing function evaluation failure */
|
||||
private double argument = Double.NaN;
|
||||
|
||||
/**
|
||||
* Construct an exception indicating the argument value
|
||||
* that caused the function evaluation to fail. Generates an exception
|
||||
* message of the form "Evaluation failed for argument = " + argument.
|
||||
*
|
||||
* @param argument the failing function argument
|
||||
*/
|
||||
public FunctionEvaluationException(double argument) {
|
||||
this(argument, "Evaluation failed for argument = " + argument);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an exception using the given argument and message
|
||||
* text. The message text of the exception will start with
|
||||
* <code>message</code> and be followed by
|
||||
* " Evaluation failed for argument = " + argument.
|
||||
*
|
||||
* @param argument the failing function argument
|
||||
* @param message the exception message text
|
||||
*/
|
||||
public FunctionEvaluationException(double argument, String message) {
|
||||
this(argument, message, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an exception with the given argument, message and root cause.
|
||||
* The message text of the exception will start with <code>message</code>
|
||||
* and be followed by " Evaluation failed for argument = " + argument.
|
||||
*
|
||||
* @param message descriptive error message.
|
||||
* @param cause root cause.
|
||||
*/
|
||||
public FunctionEvaluationException(double argument, String message,
|
||||
Throwable cause) {
|
||||
super(message + " Evaluation failed for argument=" + argument, cause);
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the function argument that caused this exception.
|
||||
*
|
||||
* @return argument that caused function evaluation to fail
|
||||
*/
|
||||
public double getArgument() {
|
||||
return this.argument;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright 2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed 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 junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.1 $ $Date: 2004/07/17 19:41:05 $
|
||||
*/
|
||||
public class FunctionEvaluationExceptionTest extends TestCase {
|
||||
|
||||
public void testConstructor(){
|
||||
FunctionEvaluationException ex = new FunctionEvaluationException(0.0);
|
||||
assertNull(ex.getCause());
|
||||
assertNotNull(ex.getMessage());
|
||||
assertEquals(0.0, ex.getArgument(), 0);
|
||||
}
|
||||
|
||||
public void testConstructorMessage(){
|
||||
String msg = "message";
|
||||
FunctionEvaluationException ex = new FunctionEvaluationException(0.0, msg);
|
||||
assertNull(ex.getCause());
|
||||
assertTrue(ex.getMessage().startsWith(msg));
|
||||
assertTrue(ex.getMessage().indexOf("0") > 0);
|
||||
assertEquals(0.0, ex.getArgument(), 0);
|
||||
}
|
||||
|
||||
public void testConstructorMessageCause(){
|
||||
String outMsg = "outer message";
|
||||
String inMsg = "inner message";
|
||||
Exception cause = new Exception(inMsg);
|
||||
FunctionEvaluationException ex = new FunctionEvaluationException(0, outMsg, cause);
|
||||
assertTrue(ex.getMessage().startsWith(outMsg));
|
||||
assertTrue(ex.getMessage().indexOf("0") > 0);
|
||||
assertEquals(cause, ex.getCause());
|
||||
assertEquals(0.0, ex.getArgument(), 0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue