001/* Convert a date to a string.
002
003 Copyright (c) 2014-2018 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.DateToken;
031import ptolemy.data.StringToken;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038/// DateToString
039
040/**
041 Convert a date to a string.
042
043 @author Patricia Derler
044 @version $Id$
045 @since Ptolemy II 11.0
046 @Pt.ProposedRating Red (pd)
047 @Pt.AcceptedRating Red (pd)
048 */
049public class DateToString extends Converter {
050    /** Construct an actor with the given container and name.
051     *  @param container The container.
052     *  @param name The name of this actor.
053     *  @exception IllegalActionException If the actor cannot be contained
054     *   by the proposed container.
055     *  @exception NameDuplicationException If the container already has an
056     *   actor with this name.
057     */
058    public DateToString(CompositeEntity container, String name)
059            throws NameDuplicationException, IllegalActionException {
060        super(container, name);
061
062        input.setTypeEquals(BaseType.DATE);
063
064        output.setTypeEquals(BaseType.STRING);
065    }
066
067    ///////////////////////////////////////////////////////////////////
068    ////                         public methods                    ////
069
070    /** Consume one StringToken on the input port and output a new
071     * Datetoken.
072     *
073     *  @exception IllegalActionException If thrown while getting
074     *  or sending a token.
075     */
076    @Override
077    public void fire() throws IllegalActionException {
078        super.fire();
079        for (int i = 0; i < input.getWidth(); i++) {
080            if (input.hasToken(i)) {
081                output.send(i, new StringToken(
082                        ((DateToken) input.get(i)).stringValue()));
083            }
084        }
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}