001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.sdm.spa;
031
032import ptolemy.actor.lib.Transformer;
033import ptolemy.data.StringToken;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039//////////////////////////////////////////////////////////////////////////
040//// StringReverse
041/**
042 * Output the reverse of a string provided at the input.
043 * 
044 * @author xiaowen
045 * @version $Id: StringReverse.java 24234 2010-05-06 05:21:26Z welker $
046 */
047
048public class StringReverse extends Transformer {
049
050        /**
051         * Construct an actor with the given container and name.
052         * 
053         * @param container
054         *            The container.
055         * @param name
056         *            The name of this actor.
057         * @exception IllegalActionException
058         *                If the actor cannot be contained by the proposed
059         *                container.
060         * @exception NameDuplicationException
061         *                If the container already has an actor with this name.
062         */
063        public StringReverse(CompositeEntity container, String name)
064                        throws NameDuplicationException, IllegalActionException {
065                super(container, name);
066
067                // Set the types of the ports.
068                input.setTypeEquals(BaseType.STRING);
069                output.setTypeEquals(BaseType.STRING);
070        }
071
072        // /////////////////////////////////////////////////////////////////
073        // // public methods ////
074
075        /**
076         * Clone the actor into the specified workspace.
077         * 
078         * @param workspace
079         *            The workspace for the new object.
080         * @return A new actor.
081         * @exception CloneNotSupportedException
082         *                If a derived class contains an attribute that cannot be
083         *                cloned.
084         */
085        /*
086         * public Object clone(Workspace workspace) throws
087         * CloneNotSupportedException { StringReverse newObject = (StringReverse)
088         * super.clone(workspace);
089         * 
090         * // Set the type constraints.
091         * newObject.input.setTypeEquals(BaseType.STRING);
092         * newObject.output.setTypeEquals(BaseType.STRING);
093         * 
094         * return newObject; }
095         */
096
097        /**
098         * If there is an input string, reverse it and produce that at the output.
099         * If there is no input, do nothing.
100         * 
101         * @exception IllegalActionException
102         *                If the superclass throws it, or if it is thrown reading
103         *                the input port or writing to the output port.
104         */
105        public void fire() throws IllegalActionException {
106                super.fire();
107                if (input.hasToken(0)) {
108                        StringToken inputToken = (StringToken) input.get(0);
109                        String value = inputToken.stringValue();
110                        String strReverse = (new StringBuffer(value)).reverse().toString();
111                        output.send(0, new StringToken(strReverse));
112                }
113        }
114}
115
116// vim: sw=4 ts=4 et