001    /*
002     * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003     *
004     * This file is part of
005     * CleanSheets - a spreadsheet application for the Java platform.
006     *
007     * CleanSheets is free software; you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation; either version 2 of the License, or
010     * (at your option) any later version.
011     *
012     * CleanSheets is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with CleanSheets; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020     */
021    package csheets.core.formula.compiler;
022    
023    import csheets.core.formula.Expression;
024    import csheets.core.formula.Function;
025    import csheets.core.formula.FunctionParameter;
026    
027    /**
028     * An exception that is thrown if an function call with illegal argument count
029     * or argument types is encountered during parsing.
030     * @author Einar Pehrson
031     */
032    public class IllegalFunctionCallException extends FormulaCompilationException {
033    
034            /** The serialVersionUID of the IllegalFunctionCallException.java */
035            private static final long serialVersionUID = 1960534028676316836L;
036    
037            /** The function call */
038            private Function function;
039    
040            /** The parameter that was not matched */
041            private FunctionParameter parameter;
042    
043            /** The argument that failed to match */
044            private Expression argument;
045    
046            /**
047             * Creates a new function call exception.
048             * @param function the function that was called
049             * @param parameter the parameter that was not matched
050             * @param argument the argument that failed to match
051             */
052            public IllegalFunctionCallException(Function function,
053                            FunctionParameter parameter, Expression argument) {
054                    this.function = function;
055                    this.parameter = parameter;
056                    this.argument = argument;
057            }
058    
059            /**
060             * Returns the function that was called.
061             * @return the function that was called
062             */
063            public Function getFunction() {
064                    return function;
065            }
066    
067            /**
068             * Returns the parameter that was not matched.
069             * @return the parameter that was not matched
070             */
071            public FunctionParameter getParameter() {
072                    return parameter;
073            }
074    
075            /**
076             * Returns the argument that failed to match.
077             * @return the argument that failed to match
078             */
079            public Expression getArgument() {
080                    return argument;
081            }
082    
083            /**
084             * Returns a message describing the exception.
085             * @return a message describing the exception
086             */
087            public String getMessage() {
088                    String s = "Illegal function call: ";
089                    if (parameter == null)
090                            s += "too many arguments (" + argument + " did not match).";
091                    else if (argument == null)
092                            s += "missing argument " + parameter.getName() + ".";
093                    return s;
094            }
095    
096            /**
097             * Returns a string representation of the exception.
098             * @return a string representation of the exception
099             */
100            public String toString() {
101                    return getMessage();
102            }
103    }