001/* An actor that converts a FixToken into a DoubleToken.
002
003 Copyright (c) 1998-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.actor.lib.conversions;
029
030import ptolemy.data.DoubleToken;
031import ptolemy.data.FixToken;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036import ptolemy.math.FixPoint;
037
038///////////////////////////////////////////////////////////////////
039//// FixToDouble
040
041/**
042 This actor converts a FixToken into a DoubleToken. This conversion is
043 explicitly provided because there is no lossless conversion
044 between these two types in the type lattice, and thus the type system
045 does not do this conversion automatically.  Although the conversion
046 might be lossless, a FixToken can, in principle, have
047 arbitrary precision, and thus can exceed the capabilities of a double.
048
049 @author Bart Kienhuis and Edward A. Lee
050 @version $Id$
051 @since Ptolemy II 0.4
052 @Pt.ProposedRating Green (pwhitake)
053 @Pt.AcceptedRating Green (pwhitake)
054 @see ptolemy.data.FixToken
055 */
056public class FixToDouble extends Converter {
057    /** Construct an actor with the given container and name.
058     *  @param container The container.
059     *  @param name The name of this actor.
060     *  @exception IllegalActionException If the actor cannot be contained
061     *   by the proposed container.
062     *  @exception NameDuplicationException If the container already has an
063     *   actor with this name.
064     */
065    public FixToDouble(CompositeEntity container, String name)
066            throws NameDuplicationException, IllegalActionException {
067        super(container, name);
068        input.setTypeEquals(BaseType.UNSIZED_FIX);
069        output.setTypeEquals(BaseType.DOUBLE);
070    }
071
072    ///////////////////////////////////////////////////////////////////
073    ////                         public methods                    ////
074
075    /** Read one FixToken from the input and output the converted
076     *  DoubleToken to the output port.
077     * @exception IllegalActionException If there is no director.
078     */
079    @Override
080    public void fire() throws IllegalActionException {
081        super.fire();
082        FixToken in = (FixToken) input.get(0);
083        FixPoint value = in.fixValue();
084        output.send(0, new DoubleToken(value.doubleValue()));
085    }
086
087    /** Return false if the input port has no token, otherwise return
088     *  what the superclass returns (presumably true).
089     *  @exception IllegalActionException If there is no director.
090     */
091    @Override
092    public boolean prefire() throws IllegalActionException {
093        if (!input.hasToken(0)) {
094            return false;
095        }
096
097        return super.prefire();
098    }
099}