Package net.fortytwo.ripple.libs.data

Source Code of net.fortytwo.ripple.libs.data.Compare

package net.fortytwo.ripple.libs.data;

import net.fortytwo.flow.Sink;
import net.fortytwo.ripple.RippleException;
import net.fortytwo.ripple.libs.graph.GraphLibrary;
import net.fortytwo.ripple.model.ModelConnection;
import net.fortytwo.ripple.model.PrimitiveStackMapping;
import net.fortytwo.ripple.model.RippleList;
import net.fortytwo.ripple.model.RippleValue;

/**
* A primitive which consumes two resources and produces a comparison value
* according to their data type.
*
* @author Joshua Shinavier (http://fortytwo.net)
*/
public class Compare extends PrimitiveStackMapping {
    public String[] getIdentifiers() {
        return new String[]{
                DataLibrary.NS_2013_03 + "compare",
                GraphLibrary.NS_2008_08 + "compare",
                GraphLibrary.NS_2007_08 + "compare",
                GraphLibrary.NS_2007_05 + "compare"};
    }

    public Compare()
            throws RippleException {
        super();
    }

    public Parameter[] getParameters() {
        return new Parameter[]{
                new Parameter("x", null, true),
                new Parameter("x", null, true)};
    }

    public String getComment() {
        return "x y  =>  i  -- where i is -1 if x < y, 0 if x = y, and 1 if x > y";
    }

    public void apply(final RippleList arg,
                      final Sink<RippleList> solutions,
                      final ModelConnection mc) throws RippleException {

        RippleList stack = arg;

        RippleValue y, x;

        y = stack.getFirst();
        stack = stack.getRest();
        x = stack.getFirst();
        stack = stack.getRest();

        int result = mc.getComparator().compare(x, y);

        // Constrain the result to three possible values.
        result = (result < 0) ? -1 : (result > 0) ? 1 : 0;

        solutions.put(stack.push(mc.valueOf(result)));
    }
}
TOP

Related Classes of net.fortytwo.ripple.libs.data.Compare

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.