001/* Exception thrown on detecting type conflicts.
002
003 Copyright (c) 1997-2014 The Regents of the University of California.
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 CALIFORNIA 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 CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA 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 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor;
029
030import java.util.Collections;
031import java.util.Iterator;
032import java.util.LinkedList;
033import java.util.List;
034
035import ptolemy.kernel.util.KernelException;
036
037///////////////////////////////////////////////////////////////////
038//// TypeConflictException
039
040/**
041 Thrown on detecting type conflicts.
042 This class contains all the instances of Inequality where type conflicts
043 occurred. These inequalities are either not satisfied after type resolution,
044 or contain terms with unacceptable value, such as BaseType.UNKNOWN.
045
046 @author Yuhong Xiong
047 @version $Id$
048 @since Ptolemy II 0.2
049 @Pt.ProposedRating Green (yuhong)
050 @Pt.AcceptedRating Green (liuxj)
051 */
052@SuppressWarnings("serial")
053public class TypeConflictException extends KernelException {
054    /** Construct an Exception with a list of Inequality instances where
055     *  type conflicts occurred.
056     *  The detailed message of this Exception will be the string
057     *  "Type conflicts occurred at the following inequalities:",
058     *  followed by the list of inequalities. The string describing the
059     *  inequalities are generated by the toString() method of the class
060     *  Inequality.
061     *  @param inequalities a list of Inequality instances.
062     *  @see ptolemy.graph.Inequality#toString
063     */
064    public TypeConflictException(List inequalities) {
065        this(inequalities,
066                "Type conflicts occurred on the following inequalities:\n");
067    }
068
069    /** Construct an Exception with a list of Inequality instances where
070     *  type conflicts occurred.
071     *  The detailed message of this Exception will be the specified message,
072     *  followed by a list of inequalities. The string describing the
073     *  inequalities are generated by the toString() method of the class
074     *  Inequality.
075     *  @param inequalities A list of Inequality instances.
076     *  @param detail A message.
077     *  @see ptolemy.graph.Inequality#toString
078     */
079    public TypeConflictException(List inequalities, String detail) {
080        _inequalities.addAll(inequalities);
081        _setMessage(detail + "\n" + _listInequalities());
082    }
083
084    ///////////////////////////////////////////////////////////////////
085    ////                         public methods                    ////
086
087    /** Return a list of Inequality or InequalityTerm
088     *  instances where type conflicts occurred.
089     *  @return A List of instances where type conflicts occurred.
090     */
091    public List inequalityList() {
092        return _inequalities;
093    }
094
095    ///////////////////////////////////////////////////////////////////
096    ////                         private methods                   ////
097    /** Create a string listing all the inequalities in _inequalities.
098     * The inequalities are sorted according to their natural order
099     * so as to preserve uniformity in error messages.
100     * Each inequality takes one line, and each line starts
101     * with 2 white spaces to make the String more readable.
102     */
103    private String _listInequalities() {
104        // Create a List of string descriptions of the inequalities
105        List inequalities = new LinkedList();
106        Iterator iterator = inequalityList().iterator();
107        while (iterator.hasNext()) {
108            Object inequality = iterator.next();
109            inequalities.add("  " + inequality.toString() + "\n");
110        }
111
112        // Sort the list of string descriptions
113        Collections.sort(inequalities);
114
115        // Create a string buffer of the sorted string descriptions.
116        StringBuffer results = new StringBuffer();
117        iterator = inequalities.iterator();
118        while (iterator.hasNext()) {
119            Object inequality = iterator.next();
120            results.append(inequality);
121        }
122
123        return results.toString();
124    }
125
126    ///////////////////////////////////////////////////////////////////
127    ////                         private variables                 ////
128    private List _inequalities = new LinkedList();
129}