001/* An actor that converts any token to a double token
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.type.BaseType;
032import ptolemy.kernel.CompositeEntity;
033import ptolemy.kernel.util.IllegalActionException;
034import ptolemy.kernel.util.NameDuplicationException;
035
036///////////////////////////////////////////////////////////////////
037//// AnythingToDouble
038
039/**
040 This actor converts an input token to a double.  If the input is
041 a double, it acts as an identity.  Otherwise, it outputs a double
042 token equal to NaN.  This is useful if you wish to force an output
043 from a generic actor to always output doubles.
044
045 <p>If you want to convert a string to a double, see the
046 {@link ptolemy.actor.lib.conversions.ExpressionToToken} actor.
047 or the  {@link ptolemy.actor.lib.conversions.StringToDouble} actor.</p>
048
049 @author Adam Cataldo
050 @version $Id$
051 @Pt.ProposedRating Green (eal)
052 @Pt.AcceptedRating Red (cxh)
053 @deprecated Use ptolemy.actor.lib.conversions.ExpressionToToken or StringToDouble.
054 */
055@Deprecated
056public class AnythingToDouble 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 AnythingToDouble(CompositeEntity container, String name)
066            throws NameDuplicationException, IllegalActionException {
067        super(container, name);
068
069        output.setTypeEquals(BaseType.DOUBLE);
070    }
071
072    ///////////////////////////////////////////////////////////////////
073    ////                         public methods                    ////
074
075    /** Read exactly one token from the input and output the token
076     *  if it is double or a NaN token.
077     *  @exception IllegalActionException If there is no director.
078     */
079    @Override
080    public void fire() throws IllegalActionException {
081        super.fire();
082        DoubleToken inputToken;
083        try {
084            inputToken = (DoubleToken) input.get(0);
085        } catch (ClassCastException e) {
086            inputToken = new DoubleToken(Double.NaN);
087        }
088        output.send(0, inputToken);
089    }
090
091    /** Return false if the input port has no token, otherwise return
092     *  what the superclass returns (presumably true).
093     *  @exception IllegalActionException If there is no director.
094     */
095    @Override
096    public boolean prefire() throws IllegalActionException {
097        if (!input.hasToken(0)) {
098            return false;
099        }
100        return super.prefire();
101    }
102}