001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math;
018    
019    import org.apache.commons.math.linear.ArrayRealVector;
020    
021    /**
022     * Exception thrown when an error occurs evaluating a function.
023     * <p>
024     * Maintains an <code>argument</code> property holding the input value that
025     * caused the function evaluation to fail.
026     *
027     * @version $Revision: 885278 $ $Date: 2009-11-29 16:47:51 -0500 (Sun, 29 Nov 2009) $
028     */
029    public class FunctionEvaluationException extends MathException  {
030    
031        /** Serializable version identifier. */
032        private static final long serialVersionUID = -4305020489115478365L;
033    
034        /** Message for failed evaluation. */
035        private static final String FAILED_EVALUATION_MESSAGE =
036            "evaluation failed for argument = {0}";
037    
038        /** Argument causing function evaluation failure */
039        private double[] argument;
040    
041        /**
042         * Construct an exception indicating the argument value
043         * that caused the function evaluation to fail.
044         *
045         * @param argument  the failing function argument
046         */
047        public FunctionEvaluationException(double argument) {
048            super(FAILED_EVALUATION_MESSAGE, argument);
049            this.argument = new double[] { argument };
050        }
051    
052        /**
053         * Construct an exception indicating the argument value
054         * that caused the function evaluation to fail.
055         *
056         * @param argument  the failing function argument
057         * @since 2.0
058         */
059        public FunctionEvaluationException(double[] argument) {
060            super(FAILED_EVALUATION_MESSAGE, new ArrayRealVector(argument));
061            this.argument = argument.clone();
062        }
063    
064        /**
065         * Constructs an exception with specified formatted detail message.
066         * Message formatting is delegated to {@link java.text.MessageFormat}.
067         * @param argument  the failing function argument
068         * @param pattern format specifier
069         * @param arguments format arguments
070         * @since 1.2
071         */
072        public FunctionEvaluationException(double argument,
073                                           String pattern, Object ... arguments) {
074            super(pattern, arguments);
075            this.argument = new double[] { argument };
076        }
077    
078        /**
079         * Constructs an exception with specified formatted detail message.
080         * Message formatting is delegated to {@link java.text.MessageFormat}.
081         * @param argument  the failing function argument
082         * @param pattern format specifier
083         * @param arguments format arguments
084         * @since 2.0
085         */
086        public FunctionEvaluationException(double[] argument,
087                                           String pattern, Object ... arguments) {
088            super(pattern, arguments);
089            this.argument = argument.clone();
090        }
091    
092        /**
093         * Constructs an exception with specified root cause.
094         * Message formatting is delegated to {@link java.text.MessageFormat}.
095         * @param cause  the exception or error that caused this exception to be thrown
096         * @param argument  the failing function argument
097         * @since 1.2
098         */
099        public FunctionEvaluationException(Throwable cause, double argument) {
100            super(cause);
101            this.argument = new double[] { argument };
102        }
103    
104        /**
105         * Constructs an exception with specified root cause.
106         * Message formatting is delegated to {@link java.text.MessageFormat}.
107         * @param cause  the exception or error that caused this exception to be thrown
108         * @param argument  the failing function argument
109         * @since 2.0
110         */
111        public FunctionEvaluationException(Throwable cause, double[] argument) {
112            super(cause);
113            this.argument = argument.clone();
114        }
115    
116        /**
117         * Constructs an exception with specified formatted detail message and root cause.
118         * Message formatting is delegated to {@link java.text.MessageFormat}.
119         * @param cause  the exception or error that caused this exception to be thrown
120         * @param argument  the failing function argument
121         * @param pattern format specifier
122         * @param arguments format arguments
123         * @since 1.2
124         */
125        public FunctionEvaluationException(Throwable cause,
126                                           double argument, String pattern,
127                                           Object ... arguments) {
128            super(cause, pattern, arguments);
129            this.argument = new double[] { argument };
130        }
131    
132        /**
133         * Constructs an exception with specified formatted detail message and root cause.
134         * Message formatting is delegated to {@link java.text.MessageFormat}.
135         * @param cause  the exception or error that caused this exception to be thrown
136         * @param argument  the failing function argument
137         * @param pattern format specifier
138         * @param arguments format arguments
139         * @since 2.0
140         */
141        public FunctionEvaluationException(Throwable cause,
142                                           double[] argument, String pattern,
143                                           Object ... arguments) {
144            super(cause, pattern, arguments);
145            this.argument = argument.clone();
146        }
147    
148        /**
149         * Returns the function argument that caused this exception.
150         *
151         * @return  argument that caused function evaluation to fail
152         */
153        public double[] getArgument() {
154            return argument.clone();
155        }
156    
157    }