001/** A variable InequalityTerm.
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.graph.test;
029
030import ptolemy.graph.GraphActionException;
031import ptolemy.graph.InequalityTerm;
032
033///////////////////////////////////////////////////////////////////
034//// TestVariable
035
036/**
037 A variable InequalityTerm.
038 This class is for testing inequality related classes.
039 The value of this InequalityTerm is a String.
040 This term has name, which is used for printing test result.
041
042 @author Yuhong Xiong
043 @version $Id$
044 @since Ptolemy II 0.2
045 @Pt.ProposedRating Red (cxh)
046 @Pt.AcceptedRating Red (cxh)
047 */
048public class TestVariable implements InequalityTerm {
049    /** Construct a variable InequalityTerm with a null initial value.
050     */
051    public TestVariable() {
052    }
053
054    /** Construct a variable InequalityTerm with the specified
055     *  initial value.
056     *  @param value A String
057     */
058    public TestVariable(String value) {
059        _value = value;
060    }
061
062    ///////////////////////////////////////////////////////////////////
063    ////                         public methods                    ////
064
065    /** Disallow the value of this term to be set.
066     */
067    public void fixValue() {
068        _valueFixed = true;
069    }
070
071    /** Return the string value.
072     *  @return A String
073     */
074    @Override
075    public Object getAssociatedObject() {
076        return _value;
077    }
078
079    /** Return the information of this term. The information is a
080     *  String of the form: <i>name</i>(variable)_<i>value</i>.
081     *  @return A String
082     */
083    public String getInfo() {
084        return _name + "(variable)_" + getValue();
085    }
086
087    /** Return the String value of this term.
088     *  @return a String
089     *  @see #setValue(Object)
090     */
091    @Override
092    public Object getValue() {
093        return _value;
094    }
095
096    /** Return an array of size one. The element of the array is
097     *  the this reference.
098     *  @return an array of InequalityTerms
099     */
100    @Override
101    public InequalityTerm[] getVariables() {
102        if (isSettable()) {
103            InequalityTerm[] variable = new InequalityTerm[1];
104            variable[0] = this;
105            return variable;
106        } else {
107            return new InequalityTerm[0];
108        }
109    }
110
111    /** Set the value of this variable to the specified String.
112     *  @param e a String
113     *  @exception GraphActionException not thrown
114     */
115    @Override
116    public void initialize(Object e) throws GraphActionException {
117        if (isSettable()) {
118            _value = (String) e;
119        } else {
120            throw new GraphActionException(
121                    "TestVariable.initialize: " + "This term is not settable.");
122        }
123    }
124
125    /** Return true.
126     *  @return true
127     */
128    @Override
129    public boolean isSettable() {
130        return !_valueFixed;
131    }
132
133    /** Check whether the current value of this term is acceptable,
134     *  and return true if it is.  In this class, a value is always
135     *  acceptable.
136     *  @return True.
137     */
138    @Override
139    public boolean isValueAcceptable() {
140        return true;
141    }
142
143    /** Set the name of this variable. If the specified String is null,
144     *  Set the name to an empty String.
145     *  @param name The name of this variable.
146     */
147    public void setName(String name) {
148        if (name != null) {
149            _name = name;
150        } else {
151            _name = "";
152        }
153    }
154
155    /** Set the value of this variable to the specified String.
156     *  @param e a String
157     *  @exception GraphActionException not thrown
158     *  @see #getValue()
159     */
160    @Override
161    public void setValue(Object e) throws GraphActionException {
162        if (isSettable()) {
163            _value = (String) e;
164        } else {
165            throw new GraphActionException(
166                    "TestVariable.isSettable: " + "value is not settable.");
167        }
168    }
169
170    /** Override the base class to describe the variable.
171     *  @return A string describing the variable.
172     */
173    @Override
174    public String toString() {
175        return getClass().getName() + getInfo();
176    }
177
178    /** Allow the value of this term to be changed.
179     */
180    public void unfixValue() {
181        _valueFixed = false;
182    }
183
184    ///////////////////////////////////////////////////////////////////
185    ////                         private methods                   ////
186    private String _name = "";
187
188    private String _value = null;
189
190    private boolean _valueFixed = false;
191}