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;
022    
023    /**
024     * An exception that is thrown when an illegal value is encountered.
025     * @author Einar Pehrson
026     */
027    public class IllegalValueTypeException extends Exception {
028    
029            /** The serialVersionUID of the IllegalValueTypeException.java */
030            private static final long serialVersionUID = 1642715350728576100L;
031    
032            /** The value */
033            private Value value;
034    
035            /** The expected type of the value */
036            private Value.Type expectedValueType;
037    
038            /**
039             * Creates a new illegal value type exception.
040             * @param value the value
041             * @param expectedValueType the expected type of the value
042             */
043            public IllegalValueTypeException(Value value, Value.Type expectedValueType) {
044                    this.value = value;
045                    this.expectedValueType = expectedValueType;
046            }
047    
048            /**
049             * Returns the value.
050             * @return the value
051             */
052            public Value getValue() {
053                    return value;
054            }
055    
056            /**
057             * Returns the expected type of the value.
058             * @return the expected type of the value
059             */
060            public Value.Type getExpectedValueType() {
061                    return expectedValueType;
062            }
063    
064            /**
065             * Returns a message describing the exception.
066             * @return a message describing the exception
067             */
068            public String getMessage() {
069                    return "The value '" + value + "' was expected to be of type "
070                            + expectedValueType.toString().toLowerCase() + ".";
071            }
072    
073            /**
074             * Returns a string representation of the exception.
075             * @return a string representation of the exception
076             */
077            public String toString() {
078                    if (value.toAny() == null)
079                            return "#NULL!";
080                    else
081                            return "#TYPE!";
082            }
083    }