001/* Exception thrown on an attempt to add a named object to a collection that
002 requires unique names, and finding that there already is an object by that
003 name in the collection.
004
005 Copyright (c) 1997-2014 The Regents of the University of California.
006 All rights reserved.
007 Permission is hereby granted, without written agreement and without
008 license or royalty fees, to use, copy, modify, and distribute this
009 software and its documentation for any purpose, provided that the above
010 copyright notice and the following two paragraphs appear in all copies
011 of this software.
012
013 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
014 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
015 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
016 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
017 SUCH DAMAGE.
018
019 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
020 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
021 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
022 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024 ENHANCEMENTS, OR MODIFICATIONS.
025
026 PT_COPYRIGHT_VERSION_2
027 COPYRIGHTENDKEY
028
029 */
030package ptolemy.kernel.util;
031
032//////////////////////////////////////////////////////////////////////////
033//// NameDuplicationException
034
035/** Thrown on an attempt to add a named object to a collection that
036 requires unique names, and finding that there already is an object
037 by that name in the collection.
038 Constructors are provided that take 1 or 2 Nameable references
039 plus an arbitrary String.  The constructors are robust in that null
040 references are ignored.  The preferred constructors are those that
041 take two named objects (the container and the would-be containee),
042 or two named objects and an arbitrary string (which can be used to
043 provide additional information about the error).
044
045 <p>This class has no constructors that take a Throwable cause because
046 no such constructors have been needed, but in principle, such constructors
047 could be added if needed.
048
049 @author John S. Davis II, Edward A. Lee, Christopher Hylands
050 @version $Id$
051 @since Ptolemy II 0.2
052 @Pt.ProposedRating Green (cxh)
053 @Pt.AcceptedRating Green (cxh)
054 */
055@SuppressWarnings("serial")
056public class NameDuplicationException extends KernelException {
057    /** Construct an exception with a detail message that includes the
058     *  name of the first argument.  If one or more of the parameters
059     *  are null, then the message of the exception is adjusted
060     *  accordingly.
061     *  @param container The would be container.
062     *  @param detail The message.
063     */
064    public NameDuplicationException(Nameable container, String detail) {
065        super(container, null, detail);
066    }
067
068    /** Construct an exception with a message that includes the
069     *  name of the would be containee and the would be container.
070     *  If one or more of the parameters are null, then the
071     *  message of the exception is adjusted accordingly.
072     *  @param wouldBeContainee The would be containee.
073     *  @param container The would be container.
074     */
075    public NameDuplicationException(Nameable container,
076            Nameable wouldBeContainee) {
077        this(container, wouldBeContainee, null);
078    }
079
080    /** Construct an exception with a detail message that includes the
081     *  name of the would be containee and the would be container plus
082     *  the third argument string.
083     *  If one or more of the parameters are null, then the
084     *  message of the exception is adjusted accordingly.
085     *  @param wouldBeContainee The would be containee.
086     *  @param container The would be container.
087     *  @param detail A message.
088     */
089    public NameDuplicationException(Nameable container,
090            Nameable wouldBeContainee, String detail) {
091        if (getFullName(container).equals("")) {
092            // Note that if wouldBeContainee is null, then we get
093            // the 'Attempt to insert object named "" into a'.
094            // Note that if wouldBeContainee is the empty string, then we get
095            // the 'Attempt to insert object named "<Unnamed Object>" into a'.
096            _setMessage("Attempt to insert object named \""
097                    + getName(wouldBeContainee)
098                    + "\" into a container that already contains"
099                    + " an object with that name."
100                    + (detail == null ? "" : " " + detail));
101        } else {
102            _setMessage("Attempt to insert object named \""
103                    + getName(wouldBeContainee) + "\" into container named \""
104                    + getFullName(container)
105                    + "\", which already contains an object with that name."
106                    + (detail == null ? "" : " " + detail));
107        }
108    }
109}