001/* An actor that converts a LongToken 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.LongToken;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038//// LongToDouble
039
040/**
041 <p>This actor converts a LongToken into a DoubleToken.</p>
042 <p>
043 Note that a double cannot be losslessly converted to a long, and vice
044 versa, as both have 64 bit representations in Java.</p>
045
046 @author Christopher Hylands
047 @version $Id$
048 @since Ptolemy II 2.0
049 @Pt.ProposedRating Green (cxh)
050 @Pt.AcceptedRating Green (cxh)
051
052 @see ptolemy.data.DoubleToken
053 @see ptolemy.data.LongToken
054 */
055public class LongToDouble extends Converter {
056    /** Construct an actor with the given container and name.
057     *  @param container The container.
058     *  @param name The name of this actor.
059     *  @exception IllegalActionException If the actor cannot be contained
060     *   by the proposed container.
061     *  @exception NameDuplicationException If the container already has an
062     *   actor with this name.
063     */
064    public LongToDouble(CompositeEntity container, String name)
065            throws NameDuplicationException, IllegalActionException {
066        super(container, name);
067        input.setTypeEquals(BaseType.LONG);
068        output.setTypeEquals(BaseType.DOUBLE);
069    }
070
071    ///////////////////////////////////////////////////////////////////
072    ////                         public methods                    ////
073
074    /** Read exactly one token from the input and output the converted
075     *  value.
076     *  @exception IllegalActionException If there is no director.
077     */
078    @Override
079    public void fire() throws IllegalActionException {
080        super.fire();
081        LongToken inputToken = (LongToken) input.get(0);
082        Long inputValue = Long.valueOf(inputToken.longValue());
083        DoubleToken result = new DoubleToken(inputValue.doubleValue());
084
085        output.send(0, result);
086    }
087
088    /** Return false if the input port has no token, otherwise return
089     *  what the superclass returns (presumably true).
090     *  @exception IllegalActionException If there is no director.
091     */
092    @Override
093    public boolean prefire() throws IllegalActionException {
094        if (!input.hasToken(0)) {
095            return false;
096        }
097
098        return super.prefire();
099    }
100}