Package org.syrup.functions

Source Code of org.syrup.functions.Flip

package org.syrup.functions;

import org.syrup.Context;
import org.syrup.Data;
import org.syrup.Function;
import org.syrup.Result;
import org.syrup.helpers.DataImpl;
import org.syrup.helpers.ResultImpl;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.logging.Logger;

/**
* Flips the first input in such a way that if the first input is 0, the first
* output will be 1. Vice versa, when the first input is 1, the first output
* will be 0.
*
* @author Robbert van Dalen
*/
public class Flip implements Function
{
    static final String COPYRIGHT = "Copyright 2005 Robbert van Dalen."
        + "At your option, you may copy, distribute, or make derivative works under "
        + "the terms of The Artistic License. This License may be found at "
        + "http://www.opensource.org/licenses/artistic-license.php. "
        + "THERE IS NO WARRANTY; USE THIS PRODUCT AT YOUR OWN RISK.";

    private final static Logger logger = Logger.getLogger("org.syrup.functions.Flip");

    private final static Data _0 = new DataImpl(new String("0").getBytes());

    private final static Data _1 = new DataImpl(new String("1").getBytes());

    /**
     */
    public Result execute(Context context)
    {
        try
        {
            if (context.in_1_link() != null)
            {
                Data in1 = context.in_1_link().content();
                String d = new String(in1.bytes());
                Data result = _0;

                if (d.equals("0"))
                {
                    result = _1;
                }

                return new ResultImpl(context, true, true, result, null);
            }
            else
            {
                throw new Exception("in-1 is mandatory");
            }
        }
        catch (Throwable e)
        {
            // Not a number?
            return new ResultImpl(context, true, true, null, org.syrup.helpers.Utils.manageError(logger, e, "Flip error"));
        }
    }
}
TOP

Related Classes of org.syrup.functions.Flip

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.