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: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $
028     */
029    public class FunctionEvaluationException extends MathException  {
030        
031        /** Serializable version identifier. */
032        private static final long serialVersionUID = -4305020489115478365L;
033    
034        /** Argument causing function evaluation failure */
035        private double[] argument;
036        
037        /**
038         * Construct an exception indicating the argument value
039         * that caused the function evaluation to fail.
040         * 
041         * @param argument  the failing function argument 
042         */
043        public FunctionEvaluationException(double argument) {
044            super("evaluation failed for argument = {0}", argument);
045            this.argument = new double[] { argument };
046        }
047        
048        /**
049         * Construct an exception indicating the argument value
050         * that caused the function evaluation to fail.
051         * 
052         * @param argument  the failing function argument 
053         * @since 2.0
054         */
055        public FunctionEvaluationException(double[] argument) {
056            super("evaluation failed for argument = {0}", new ArrayRealVector(argument));
057            this.argument = argument.clone();
058        }
059        
060        /**
061         * Constructs an exception with specified formatted detail message.
062         * Message formatting is delegated to {@link java.text.MessageFormat}.
063         * @param argument  the failing function argument 
064         * @param pattern format specifier
065         * @param arguments format arguments
066         * @since 1.2
067         */
068        public FunctionEvaluationException(double argument,
069                                           String pattern, Object ... arguments) {
070            super(pattern, arguments);
071            this.argument = new double[] { argument };
072        }
073    
074        /**
075         * Constructs an exception with specified formatted detail message.
076         * Message formatting is delegated to {@link java.text.MessageFormat}.
077         * @param argument  the failing function argument 
078         * @param pattern format specifier
079         * @param arguments format arguments
080         * @since 2.0
081         */
082        public FunctionEvaluationException(double[] argument,
083                                           String pattern, Object ... arguments) {
084            super(pattern, arguments);
085            this.argument = argument.clone();
086        }
087    
088        /**
089         * Constructs an exception with specified root cause.
090         * Message formatting is delegated to {@link java.text.MessageFormat}.
091         * @param cause  the exception or error that caused this exception to be thrown
092         * @param argument  the failing function argument 
093         * @since 1.2
094         */
095        public FunctionEvaluationException(Throwable cause, double argument) {
096            super(cause);
097            this.argument = new double[] { argument };
098        }
099    
100        /**
101         * Constructs an exception with specified root cause.
102         * Message formatting is delegated to {@link java.text.MessageFormat}.
103         * @param cause  the exception or error that caused this exception to be thrown
104         * @param argument  the failing function argument 
105         * @since 2.0
106         */
107        public FunctionEvaluationException(Throwable cause, double[] argument) {
108            super(cause);
109            this.argument = argument.clone();
110        }
111    
112        /**
113         * Constructs an exception with specified formatted detail message and root cause.
114         * Message formatting is delegated to {@link java.text.MessageFormat}.
115         * @param cause  the exception or error that caused this exception to be thrown
116         * @param argument  the failing function argument 
117         * @param pattern format specifier
118         * @param arguments format arguments
119         * @since 1.2
120         */
121        public FunctionEvaluationException(Throwable cause,
122                                           double argument, String pattern,
123                                           Object ... arguments) {
124            super(cause, pattern, arguments);
125            this.argument = new double[] { argument };
126        }
127    
128        /**
129         * Constructs an exception with specified formatted detail message and root cause.
130         * Message formatting is delegated to {@link java.text.MessageFormat}.
131         * @param cause  the exception or error that caused this exception to be thrown
132         * @param argument  the failing function argument 
133         * @param pattern format specifier
134         * @param arguments format arguments
135         * @since 2.0
136         */
137        public FunctionEvaluationException(Throwable cause,
138                                           double[] argument, String pattern,
139                                           Object ... arguments) {
140            super(cause, pattern, arguments);
141            this.argument = argument.clone();
142        }
143    
144        /**
145         * Returns the function argument that caused this exception.
146         * 
147         * @return  argument that caused function evaluation to fail
148         */
149        public double[] getArgument() {
150            return argument.clone();
151        }
152    
153    }