001/* An InequalityTerm that encapsulates a constant type.
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
028 */
029package ptolemy.data.type;
030
031import ptolemy.graph.InequalityTerm;
032import ptolemy.kernel.util.IllegalActionException;
033
034///////////////////////////////////////////////////////////////////
035//// TypeConstant
036
037/**
038 An InequalityTerm that encapsulates a constant type. The constant type
039 is specified in the constructor.
040 This class represents a constant term in an inequality constraint for
041 type resolution.
042
043 @author Yuhong Xiong
044 @version $Id$
045 @since Ptolemy II 0.4
046 @Pt.ProposedRating Red (yuhong)
047 @Pt.AcceptedRating Red (cxh)
048 @see ptolemy.graph.InequalityTerm
049 */
050public class TypeConstant implements InequalityTerm {
051    /** Construct a TypeConstant.
052     *  @param type An instance of Type.
053     */
054    public TypeConstant(Type type) {
055        _type = type;
056    }
057
058    ///////////////////////////////////////////////////////////////////
059    ////                         public methods                    ////
060
061    /** Override to return true if the type is the same in this object
062     *  as the specified object.
063     *  @param object The object to compare against.
064     *  @return true if the argument is equal to this argument.
065     */
066    @Override
067    public boolean equals(Object object) {
068        if (object instanceof TypeConstant) {
069            if (_type == null) {
070                return ((TypeConstant) object)._type == null;
071            }
072            return _type.equals(((TypeConstant) object)._type);
073        }
074        return false;
075    }
076
077    /** Return null.
078     *  @return null.
079     */
080    @Override
081    public Object getAssociatedObject() {
082        return null;
083    }
084
085    /** Return the constant type represented by this term.
086     *  @return A Type.
087     *  @see #setValue(Object)
088     */
089    @Override
090    public Object getValue() {
091        return _type;
092    }
093
094    /** Return an array of size zero.
095     * @return An array of InequalityTerm of size 0.
096     */
097    @Override
098    public InequalityTerm[] getVariables() {
099        return new InequalityTerm[0];
100    }
101
102    /** Return the hashCode of the type. This ensures that if equals()
103     *  returns true then the two objects return the same hashCode.
104     *  @return The hashCode of this object.
105     */
106    @Override
107    public int hashCode() {
108        if (_type != null) {
109            return _type.hashCode();
110        }
111        return 0;
112    }
113
114    /** Throw an Exception since type constant cannot be initialized.
115     *  @exception IllegalActionException Always thrown.
116     */
117    @Override
118    public void initialize(Object e) throws IllegalActionException {
119        throw new IllegalActionException("TypeConstant.initialize: "
120                + "Type constant cannot be initialized.");
121    }
122
123    /** Return false since this term represent a constant.
124     *  @return false.
125     */
126    @Override
127    public boolean isSettable() {
128        return false;
129    }
130
131    /** Check whether the current type of this term is acceptable,
132     *  and return true if it is.  A type is acceptable
133     *  if it represents an instantiable object.
134     *  @return True if the current type is acceptable.
135     */
136    @Override
137    public boolean isValueAcceptable() {
138        if (_type.isInstantiable()) {
139            return true;
140        }
141
142        return false;
143    }
144
145    /** Throw IllegalActionException since the value of this term
146     *  cannot be changed.
147     *  @exception IllegalActionException Always thrown.
148     *  @see #getValue()
149     */
150    @Override
151    public void setValue(Object e) throws IllegalActionException {
152        throw new IllegalActionException("TypeConstant.setValue: Cannot set "
153                + "the value of a type constant.");
154    }
155
156    /** Return a string representation of this term.
157     *  @return A String.
158     */
159    @Override
160    public String toString() {
161        return "(TypeConstant, " + getValue() + ")";
162    }
163
164    ///////////////////////////////////////////////////////////////////
165    ////                         private variable                  ////
166    private Type _type = null;
167}