001/* Output the length of a string provided at the input.
002
003 Copyright (c) 2003-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.string;
029
030import ptolemy.actor.lib.Transformer;
031import ptolemy.data.IntToken;
032import ptolemy.data.StringToken;
033import ptolemy.data.type.BaseType;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037
038///////////////////////////////////////////////////////////////////
039//// StringLength
040
041/**
042 Output the length of a string provided at the input.
043
044 @author Edward A. Lee
045 @version $Id$
046 @since Ptolemy II 4.0
047 @Pt.ProposedRating Green (eal)
048 @Pt.AcceptedRating Green (net)
049 */
050public class StringLength extends Transformer {
051    /** Construct an actor with the given container and name.
052     *  @param container The container.
053     *  @param name The name of this actor.
054     *  @exception IllegalActionException If the actor cannot be contained
055     *   by the proposed container.
056     *  @exception NameDuplicationException If the container already has an
057     *   actor with this name.
058     */
059    public StringLength(CompositeEntity container, String name)
060            throws NameDuplicationException, IllegalActionException {
061        super(container, name);
062
063        // Set the types of the ports.
064        input.setTypeEquals(BaseType.STRING);
065        output.setTypeEquals(BaseType.INT);
066    }
067
068    ///////////////////////////////////////////////////////////////////
069    ////                         public methods                    ////
070
071    /** If there is an input string, find its length and produce the
072     *  length at the output. If there is no input, do nothing.
073     *  @exception IllegalActionException If the superclass throws it, or
074     *   if it is thrown reading the input port or writing to the
075     *   output port.
076     */
077    @Override
078    public void fire() throws IllegalActionException {
079        super.fire();
080
081        if (input.hasToken(0)) {
082            StringToken inputToken = (StringToken) input.get(0);
083            String value = inputToken.stringValue();
084            int length = value.length();
085            output.send(0, new IntToken(length));
086        }
087    }
088}