001/* UnitTerm
002
003 Copyright (c) 2003-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_3
025 COPYRIGHTENDKEY
026 */
027package ptolemy.moml.unit;
028
029import ptolemy.kernel.util.IllegalActionException;
030
031///////////////////////////////////////////////////////////////////
032//// UnitTerm
033
034/** UnitTerm represents a term in a Unit Expression.
035 A UnitTerm has 1) an exponent and 2) an element.
036 The element
037 can be either 1) a Unit, 2) a variable, or 3) a
038 Unit Expression. These choices for element are mutually exclusive.
039 UnitTerms are usually constructed as a result of parsing a Unit Expression.
040
041 @author Rowland R Johnson
042 @version $Id$
043 @since Ptolemy II 8.0
044 @Pt.ProposedRating Red (rowland)
045 @Pt.AcceptedRating Red (rowland)
046 */
047public class UnitTerm implements UnitPresentation {
048    /**
049     * Construct a UnitTerm with no element.
050     *
051     */
052    public UnitTerm() {
053    }
054
055    /** Construct an instance where the contained element is a Unit.
056     * @param unit The Unit that will be the element of this instance.
057     */
058    public UnitTerm(Unit unit) {
059        _type = _UNIT;
060        _unit = unit;
061    }
062
063    ///////////////////////////////////////////////////////////////////
064    ///////////////////////// public methods //////////////////////////
065
066    /** Make a shallow copy of this UnitTerm. That is, the underlying Unit or
067     *  UnitExpr is not copied.
068     * @return The copy of this UnitTerm
069     */
070    public UnitTerm copy() {
071        UnitTerm retv = new UnitTerm();
072        retv.setVariable(_variable);
073        retv.setExponent(getExponent());
074        retv.setUnit(_unit);
075        retv.setUnitExpr(_unitExpr);
076        retv._setType(_type);
077        return retv;
078    }
079
080    /**
081     * Create a String that is understandable by a human.
082     * @see ptolemy.moml.unit.UnitPresentation#descriptiveForm()
083     */
084    @Override
085    public String descriptiveForm() {
086        String retv = null;
087
088        switch (_type) {
089        case _VARIABLE: {
090            retv = "$" + _variable;
091            break;
092        }
093
094        case _UNIT: {
095            retv = _unit.descriptiveForm();
096            break;
097        }
098
099        case _UNITEXPR: {
100            retv = "(" + _unitExpr.descriptiveForm() + ")";
101            break;
102        }
103        }
104
105        if (getExponent() != 1) {
106            retv += "^" + getExponent();
107        }
108
109        return retv;
110    }
111
112    /**
113     * @return The exponent.
114     */
115    public int getExponent() {
116        return _exponent;
117    }
118
119    /** Get the element if it is a Unit.
120     * @return The Unit if the element is a Unit, otherwise null.
121     */
122    public Unit getUnit() {
123        if (_type == _UNIT) {
124            return _unit;
125        }
126
127        return null;
128    }
129
130    /** Get the element if it is a UnitExpr.
131     * @return The UnitExpr if the element is a UnitExpr, otherwise null.
132     */
133    public UnitExpr getUnitExpr() {
134        if (_type == _UNITEXPR) {
135            return _unitExpr;
136        }
137
138        return null;
139    }
140
141    /** Get the element if it is a variable.
142     * @return The variable if the element is a variable, null otherwise.
143     */
144    public String getVariable() {
145        if (_type == _VARIABLE) {
146            return _variable;
147        }
148
149        return null;
150    }
151
152    /** Invert this UnitTerm.
153     * @return The inverse of this UnitTerm.
154     */
155    public UnitTerm invert() {
156        UnitTerm retv = copy();
157
158        switch (_type) {
159        case _VARIABLE:
160        case _UNIT: {
161            retv.setExponent(-getExponent());
162            break;
163        }
164
165        case _UNITEXPR: {
166            retv.setUnitExpr(_unitExpr.invert());
167            break;
168        }
169        }
170
171        return retv;
172    }
173
174    /** True if this is a Unit.
175     * @return True if this is a Unit.
176     */
177    public boolean isUnit() {
178        return _type == _UNIT;
179    }
180
181    /** True is this is a UnitExpr.
182     * @return True is this is a UnitExpr.
183     */
184    public boolean isUnitExpr() {
185        return _type == _UNITEXPR;
186    }
187
188    /** True if this a variable.
189     * @return True if this a variable.
190     */
191    public boolean isVariable() {
192        return _type == _VARIABLE;
193    }
194
195    /** Set the exponent.
196     * @param exponent The exponent.
197     */
198    public void setExponent(int exponent) {
199        _exponent = exponent;
200    }
201
202    /** Set the element to be a Unit.
203     * @param unit The Unit.
204     */
205    public void setUnit(Unit unit) {
206        _type = _UNIT;
207        _unit = unit;
208    }
209
210    /** Set the element to be a UnitExpr.
211     * @param expr The Unit Expression.
212     */
213    public void setUnitExpr(UnitExpr expr) {
214        _type = _UNITEXPR;
215        _unitExpr = expr;
216    }
217
218    /** Set the element to be a variable
219     * @param v The variable.
220     */
221    public void setVariable(String v) {
222        _type = _VARIABLE;
223        _variable = v;
224    }
225
226    /*
227     * (non-Javadoc)
228     *
229     * @see java.lang.Object#toString()
230     */
231    @Override
232    public String toString() {
233        String retv = null;
234
235        switch (_type) {
236        case _VARIABLE: {
237            retv = _variable;
238            break;
239        }
240
241        case _UNIT: {
242            retv = _unit.toString();
243            break;
244        }
245
246        case _UNITEXPR: {
247            retv = "(" + _unitExpr.toString() + ")";
248            break;
249        }
250        }
251
252        if (getExponent() != 1) {
253            retv += "^" + getExponent();
254        }
255
256        return retv;
257    }
258
259    /** Visit an instance of UnitTerm.
260     * @param visitor The visitor.
261     * @return The result of visiting the UnitTerm.
262     */
263    public Object visit(EquationVisitor visitor) throws IllegalActionException {
264        return visitor._visitUnitTerm(this);
265    }
266
267    ///////////////////////////////////////////////////////////////////
268    ///////////////////////// protected methods //////////////////////////
269
270    /**
271     * @param type The Unit type.
272     */
273    protected void _setType(int type) {
274        _type = type;
275    } ///////////////////////////////////////////////////////////////////
276
277    ////                         private variables                 ////
278    private static final int _UNIT = 1;
279
280    private static final int _UNITEXPR = 2;
281
282    private static final int _VARIABLE = 3;
283
284    private int _exponent = 1;
285
286    private int _type = -1;
287
288    private Unit _unit = null;
289
290    private UnitExpr _unitExpr = null;
291
292    private String _variable = null;
293}