001/* A parameter that is a singleton. 002 003 Copyright (c) 2003-2016 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 028 */ 029package ptolemy.data.expr; 030 031import ptolemy.kernel.util.Attribute; 032import ptolemy.kernel.util.IllegalActionException; 033import ptolemy.kernel.util.NameDuplicationException; 034import ptolemy.kernel.util.NamedObj; 035 036/////////////////////////////////////////////////////////////////// 037//// SingletonParameter 038 039/** 040 This subclass of Parameter is identical to Parameter except that it 041 is a singleton, meaning that when its container is set, if the container 042 already contains an attribute with the same name, then that attribute 043 is first removed. 044 045 @author Edward A. Lee 046 @version $Id$ 047 @since Ptolemy II 4.0 048 @Pt.ProposedRating Yellow (eal) 049 @Pt.AcceptedRating Red (cxh) 050 */ 051public class SingletonParameter extends Parameter { 052 /** Construct a parameter with the given name contained by the specified 053 * entity. The container argument must not be null, or a 054 * NullPointerException will be thrown. This parameter will use the 055 * workspace of the container for synchronization and version counts. 056 * If the name argument is null, then the name is set to the empty string. 057 * The object is not added to the list of objects in the workspace 058 * unless the container is null. 059 * Increment the version of the workspace. 060 * @param container The container. 061 * @param name The name of the parameter. 062 * @exception IllegalActionException If the parameter is not of an 063 * acceptable class for the container. 064 * @exception NameDuplicationException If the name coincides with 065 * a parameter already in the container. 066 */ 067 public SingletonParameter(NamedObj container, String name) 068 throws IllegalActionException, NameDuplicationException { 069 super(container, name); 070 } 071 072 /** Construct a Parameter with the given container, name, and Token. 073 * The container argument must not be null, or a 074 * NullPointerException will be thrown. This parameter will use the 075 * workspace of the container for synchronization and version counts. 076 * If the name argument is null, then the name is set to the empty string. 077 * The object is not added to the list of objects in the workspace 078 * unless the container is null. 079 * Increment the version of the workspace. 080 * If the name argument is null, then the name is set to the empty 081 * string. 082 * @param container The container. 083 * @param name The name. 084 * @param token The Token contained by this Parameter. 085 * @exception IllegalActionException If the parameter is not of an 086 * acceptable class for the container. 087 * @exception NameDuplicationException If the name coincides with 088 * an parameter already in the container. 089 */ 090 public SingletonParameter(NamedObj container, String name, 091 ptolemy.data.Token token) 092 throws IllegalActionException, NameDuplicationException { 093 super(container, name, token); 094 } 095 096 /////////////////////////////////////////////////////////////////// 097 //// public methods //// 098 099 /** Remove any previous attribute in the container that has the same 100 * name as this attribute, and then call the base class method to set 101 * the container. If the container is not in the same workspace as 102 * this attribute, throw an exception. 103 * If this attribute is already contained by the NamedObj, do nothing. 104 * If the attribute already has a container, remove 105 * this attribute from its attribute list first. Otherwise, remove 106 * it from the directory of the workspace, if it is there. 107 * If the argument is null, then remove this attribute from its 108 * container. It is not added to the workspace directory, so this 109 * could result in this object being garbage collected. 110 * <p> 111 * This method is write-synchronized on the 112 * workspace and increments its version number. 113 * @param container The container to attach this attribute to. 114 * @exception IllegalActionException If this attribute is not of the 115 * expected class for the container, or it has no name, 116 * or the attribute and container are not in the same workspace, or 117 * the proposed container would result in recursive containment. 118 * @exception NameDuplicationException If the container already has 119 * an attribute with the name of this attribute that is of class 120 * SingletonConfigurableAttribute. 121 */ 122 @Override 123 public void setContainer(NamedObj container) 124 throws IllegalActionException, NameDuplicationException { 125 Attribute previous = null; 126 127 if (container != null) { 128 previous = container.getAttribute(getName()); 129 130 if (previous != null) { 131 previous.setContainer(null); 132 } 133 } 134 135 try { 136 super.setContainer(container); 137 } catch (IllegalActionException ex) { 138 // Restore previous. 139 if (previous != null) { 140 previous.setContainer(container); 141 } 142 143 throw ex; 144 } catch (NameDuplicationException ex) { 145 // Restore previous. 146 if (previous != null) { 147 previous.setContainer(container); 148 } 149 150 throw ex; 151 } 152 } 153}