1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.commons.math;
18 
19 import org.apache.commons.math.exception.util.DummyLocalizable;
20 import org.apache.commons.math.exception.util.Localizable;
21 import org.apache.commons.math.exception.util.LocalizedFormats;
22 import org.apache.commons.math.linear.ArrayRealVector;
23 
24 /**
25  * Exception thrown when an error occurs evaluating a function.
26  * <p>
27  * Maintains an <code>argument</code> property holding the input value that
28  * caused the function evaluation to fail.
29  *
30  * @version $Revision: 1070725 $ $Date: 2011-02-15 02:31:12 +0100 (mar. 15 févr. 2011) $
31  */
32 public class FunctionEvaluationException extends MathException  {
33 
34     /** Serializable version identifier. */
35     private static final long serialVersionUID = 1384427981840836868L;
36 
37     /** Argument causing function evaluation failure */
38     private double[] argument;
39 
40     /**
41      * Construct an exception indicating the argument value
42      * that caused the function evaluation to fail.
43      *
44      * @param argument  the failing function argument
45      */
FunctionEvaluationException(double argument)46     public FunctionEvaluationException(double argument) {
47         super(LocalizedFormats.EVALUATION_FAILED, argument);
48         this.argument = new double[] { argument };
49     }
50 
51     /**
52      * Construct an exception indicating the argument value
53      * that caused the function evaluation to fail.
54      *
55      * @param argument  the failing function argument
56      * @since 2.0
57      */
FunctionEvaluationException(double[] argument)58     public FunctionEvaluationException(double[] argument) {
59         super(LocalizedFormats.EVALUATION_FAILED, new ArrayRealVector(argument));
60         this.argument = argument.clone();
61     }
62 
63     /**
64      * Constructs an exception with specified formatted detail message.
65      * Message formatting is delegated to {@link java.text.MessageFormat}.
66      * @param argument  the failing function argument
67      * @param pattern format specifier
68      * @param arguments format arguments
69      * @since 1.2
70      */
FunctionEvaluationException(double argument, String pattern, Object ... arguments)71     public FunctionEvaluationException(double argument,
72                                        String pattern, Object ... arguments) {
73         this(argument, new DummyLocalizable(pattern), arguments);
74     }
75 
76     /**
77      * Constructs an exception with specified formatted detail message.
78      * Message formatting is delegated to {@link java.text.MessageFormat}.
79      * @param argument  the failing function argument
80      * @param pattern format specifier
81      * @param arguments format arguments
82      * @since 2.2
83      */
FunctionEvaluationException(double argument, Localizable pattern, Object ... arguments)84     public FunctionEvaluationException(double argument,
85                                        Localizable pattern, Object ... arguments) {
86         super(pattern, arguments);
87         this.argument = new double[] { argument };
88     }
89 
90     /**
91      * Constructs an exception with specified formatted detail message.
92      * Message formatting is delegated to {@link java.text.MessageFormat}.
93      * @param argument  the failing function argument
94      * @param pattern format specifier
95      * @param arguments format arguments
96      * @since 2.0
97      */
FunctionEvaluationException(double[] argument, String pattern, Object ... arguments)98     public FunctionEvaluationException(double[] argument,
99                                        String pattern, Object ... arguments) {
100         this(argument, new DummyLocalizable(pattern), arguments);
101     }
102 
103     /**
104      * Constructs an exception with specified formatted detail message.
105      * Message formatting is delegated to {@link java.text.MessageFormat}.
106      * @param argument  the failing function argument
107      * @param pattern format specifier
108      * @param arguments format arguments
109      * @since 2.2
110      */
FunctionEvaluationException(double[] argument, Localizable pattern, Object ... arguments)111     public FunctionEvaluationException(double[] argument,
112                                        Localizable pattern, Object ... arguments) {
113         super(pattern, arguments);
114         this.argument = argument.clone();
115     }
116 
117     /**
118      * Constructs an exception with specified root cause.
119      * Message formatting is delegated to {@link java.text.MessageFormat}.
120      * @param cause  the exception or error that caused this exception to be thrown
121      * @param argument  the failing function argument
122      * @since 1.2
123      */
FunctionEvaluationException(Throwable cause, double argument)124     public FunctionEvaluationException(Throwable cause, double argument) {
125         super(cause);
126         this.argument = new double[] { argument };
127     }
128 
129     /**
130      * Constructs an exception with specified root cause.
131      * Message formatting is delegated to {@link java.text.MessageFormat}.
132      * @param cause  the exception or error that caused this exception to be thrown
133      * @param argument  the failing function argument
134      * @since 2.0
135      */
FunctionEvaluationException(Throwable cause, double[] argument)136     public FunctionEvaluationException(Throwable cause, double[] argument) {
137         super(cause);
138         this.argument = argument.clone();
139     }
140 
141     /**
142      * Constructs an exception with specified formatted detail message and root cause.
143      * Message formatting is delegated to {@link java.text.MessageFormat}.
144      * @param cause  the exception or error that caused this exception to be thrown
145      * @param argument  the failing function argument
146      * @param pattern format specifier
147      * @param arguments format arguments
148      * @since 1.2
149      */
FunctionEvaluationException(Throwable cause, double argument, String pattern, Object ... arguments)150     public FunctionEvaluationException(Throwable cause,
151                                        double argument, String pattern,
152                                        Object ... arguments) {
153         this(cause, argument, new DummyLocalizable(pattern), arguments);
154     }
155 
156     /**
157      * Constructs an exception with specified formatted detail message and root cause.
158      * Message formatting is delegated to {@link java.text.MessageFormat}.
159      * @param cause  the exception or error that caused this exception to be thrown
160      * @param argument  the failing function argument
161      * @param pattern format specifier
162      * @param arguments format arguments
163      * @since 2.2
164      */
FunctionEvaluationException(Throwable cause, double argument, Localizable pattern, Object ... arguments)165     public FunctionEvaluationException(Throwable cause,
166                                        double argument, Localizable pattern,
167                                        Object ... arguments) {
168         super(cause, pattern, arguments);
169         this.argument = new double[] { argument };
170     }
171 
172     /**
173      * Constructs an exception with specified formatted detail message and root cause.
174      * Message formatting is delegated to {@link java.text.MessageFormat}.
175      * @param cause  the exception or error that caused this exception to be thrown
176      * @param argument  the failing function argument
177      * @param pattern format specifier
178      * @param arguments format arguments
179      * @since 2.0
180      */
FunctionEvaluationException(Throwable cause, double[] argument, String pattern, Object ... arguments)181     public FunctionEvaluationException(Throwable cause,
182                                        double[] argument, String pattern,
183                                        Object ... arguments) {
184         this(cause, argument, new DummyLocalizable(pattern), arguments);
185     }
186 
187     /**
188      * Constructs an exception with specified formatted detail message and root cause.
189      * Message formatting is delegated to {@link java.text.MessageFormat}.
190      * @param cause  the exception or error that caused this exception to be thrown
191      * @param argument  the failing function argument
192      * @param pattern format specifier
193      * @param arguments format arguments
194      * @since 2.2
195      */
FunctionEvaluationException(Throwable cause, double[] argument, Localizable pattern, Object ... arguments)196     public FunctionEvaluationException(Throwable cause,
197                                        double[] argument, Localizable pattern,
198                                        Object ... arguments) {
199         super(cause, pattern, arguments);
200         this.argument = argument.clone();
201     }
202 
203     /**
204      * Returns the function argument that caused this exception.
205      *
206      * @return  argument that caused function evaluation to fail
207      */
getArgument()208     public double[] getArgument() {
209         return argument.clone();
210     }
211 }
212