Package net.fortytwo.ripple.model.keyval

Source Code of net.fortytwo.ripple.model.keyval.KeyValueMapping

package net.fortytwo.ripple.model.keyval;

import net.fortytwo.flow.Sink;
import net.fortytwo.ripple.RippleException;
import net.fortytwo.ripple.model.ModelConnection;
import net.fortytwo.ripple.model.NullStackMapping;
import net.fortytwo.ripple.model.RippleList;
import net.fortytwo.ripple.model.RippleValue;
import net.fortytwo.ripple.model.StackMapping;

/**
* @author Joshua Shinavier (http://fortytwo.net)
*/
public class KeyValueMapping implements StackMapping {
    private final String key;

    public KeyValueMapping(final String key) {
        this.key = key;
    }

    public int arity() {
        return 1;
    }

    public StackMapping getInverse() throws RippleException {
        return new NullStackMapping();
    }

    public boolean isTransparent() {
        return true;
    }

    public void apply(final RippleList arg,
                      final Sink<RippleList> solutions,
                      final ModelConnection mc) throws RippleException {
        RippleValue x;
        RippleList stack = arg;

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

        if (x instanceof KeyValueValue) {
            RippleValue result = ((KeyValueValue) x).getValue(key, mc);
            solutions.put(
                    stack.push(result));
        } else {
            throw new RippleException("argument is not a JSON value: " + x);
        }
    }
}
TOP

Related Classes of net.fortytwo.ripple.model.keyval.KeyValueMapping

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.