001/* The pattern for visitors to a UnitEquation.
002
003 Copyright (c) 2003-2013 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 java.util.Iterator;
030
031import ptolemy.kernel.util.IllegalActionException;
032
033///////////////////////////////////////////////////////////////////
034//// EquationVisitor
035
036/**
037 An Abstract class that specifies all the necessary aspects of visitors to a
038 UnitEquation.
039
040 <p> The generic version of the method to visit a UnitEquation,
041 UnitExpr, and UnitTerm are specified here. To do a specific kind of
042 visit create a class that extends EquationVisitor that overrides
043 some one or more of these methods.  These methods are specified as
044 throwing an IllegalActionException to make it possible for the
045 overridden methods in a subclass to throw IllegalActionException.
046
047 @author Rowland R Johnson
048 @version $Id$
049 @since Ptolemy II 8.0
050 @Pt.ProposedRating Red (rowland)
051 @Pt.AcceptedRating Red (rowland)
052 */
053public abstract class EquationVisitor {
054    ///////////////////////////////////////////////////////////////////
055    ////                         protected methods                 ////
056
057    /** Visit a UnitEquation by visiting the left and right sides of the
058     * equation.
059     * @param uEquation The UnitEquation to visit.
060     * @return Null, can be overridden in a concrete visitor.
061     * @exception IllegalActionException Not thrown in this base class.
062     */
063    protected Object _visitUnitEquation(UnitEquation uEquation)
064            throws IllegalActionException {
065        _visitUnitExpr(uEquation.getLhs());
066        _visitUnitExpr(uEquation.getRhs());
067        return null;
068    }
069
070    /** Visit a UnitExpr by visiting the UnitTerms.
071     * @param unitExpr The UnitExpr to visit.
072     * @return Null, can be overridden in a concrete visitor.
073     * @exception IllegalActionException Not thrown in this base class.
074     */
075    protected Object _visitUnitExpr(UnitExpr unitExpr)
076            throws IllegalActionException {
077        Iterator iter = unitExpr.getUTerms().iterator();
078
079        while (iter.hasNext()) {
080            UnitTerm term = (UnitTerm) iter.next();
081            term.visit(this);
082        }
083
084        return null;
085    }
086
087    /** Visit a UnitTerm by visiting the UnitExpr if there is one. This method
088     * will almost certainly be overridden in a subclass.
089     * @param uTerm The UnitTerm to visit.
090     * @return Null, can be overridden in a concrete visitor.
091     * @exception IllegalActionException Not thrown in this base class.
092     */
093    protected Object _visitUnitTerm(UnitTerm uTerm)
094            throws IllegalActionException {
095        if (uTerm.isUnitExpr()) {
096            UnitExpr uExpr = uTerm.getUnitExpr();
097
098            if (uExpr != null) {
099                _visitUnitExpr(uExpr);
100            }
101        }
102
103        return null;
104    }
105}