001/* Exception of accessing graph elements in wrong ways.
002
003 Copyright (c) 2002-2014 The University of Maryland.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 MARYLAND HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 */
025package ptolemy.graph;
026
027///////////////////////////////////////////////////////////////////
028//// GraphElementException
029
030/**
031 The exception of accessing graph elements in wrong ways. This exception
032 could be caused by accessing nonexistent elements or elements with incorrect
033 association values.
034
035 @author Mingyung Ko, Shuvra S. Bhattacharyya
036 @version $Id$
037 @since Ptolemy II 2.1
038 @Pt.ProposedRating Red (myko)
039 @Pt.AcceptedRating Red (ssb)
040 */
041@SuppressWarnings("serial")
042public class GraphElementException extends GraphException {
043    /** Constructor for a given message.
044     *  @param message The message.
045     */
046    public GraphElementException(String message) {
047        super(message);
048    }
049
050    /** Constructor with arguments of element, graph, and a message.
051     *  @param element The invalid element.
052     *  @param graph The graph accessed.
053     *  @param message The exception message.
054     */
055    public GraphElementException(Element element, Graph graph, String message) {
056        super(_argumentsToString(element, graph, message));
057    }
058
059    ///////////////////////////////////////////////////////////////////
060    ////                         public methods                    ////
061
062    /** Verify that a node is in the container graph.
063     *
064     *  @param node The node to verify.
065     *  @param graph The container graph.
066     *  @exception IllegalArgumentException If the node is not in the graph.
067     */
068    static public void checkNode(Node node, Graph graph) {
069        if (!graph.containsNode(node)) {
070            throw new GraphElementException("Reference to a node that is "
071                    + "not in the graph.\n" + elementDump(node, graph));
072        }
073    }
074
075    /** Verify that an edge is in the container graph.
076     *
077     *  @param edge The edge to verify.
078     *  @param graph The container graph.
079     *  @exception IllegalArgumentException If the edge is not in the graph.
080     */
081    static public void checkEdge(Edge edge, Graph graph) {
082        if (!graph.containsEdge(edge)) {
083            throw new GraphElementException("Reference to an edge that is "
084                    + "not in the graph.\n" + elementDump(edge, graph));
085        }
086    }
087
088    ///////////////////////////////////////////////////////////////////
089    ////                         private methods                   ////
090
091    /*  A method for generating proper string dumps.
092     *  @param element The element the invalid weight tries to associate.
093     *  @param graph The graph to access.
094     *  @param message The exception message given by users.
095     *  @return The desired exception message.
096     */
097    static private String _argumentsToString(Element element, Graph graph,
098            String message) {
099        return message + elementDump(element, graph);
100    }
101}