Package org.jruby.embed.variable

Examples of org.jruby.embed.variable.BiVariable


                                {"baseball", "hiking", "soccer", "ski"},
                                {"snow", "sleet", "drizzle", "rain"},
                                {"cypress", "hemlock", "spruce"}};
        result = instance.getVariables();
        for (int i=0; i<result.size(); i++) {
            BiVariable var = result.get(i);
            assertArrayEquals(extResult[i], (String[]) var.getJavaObject());
        }
        assertTrue(result.size() == 5);

        container = new ScriptingContainer(LocalContextScope.SINGLETHREAD, LocalVariableBehavior.GLOBAL);
        instance = container.getVarMap();
        container.put("ARGV", new String[] {"spring", "fall"});
        container.put("SEASON", new String[] {"summer", "winter"});
        container.put("$sports", new String[] {"baseball", "hiking", "soccer", "ski"});
        container.put("@weather", new String[] {"snow", "sleet", "drizzle", "rain"});
        container.put("trees", new String[] {"cypress", "hemlock", "spruce"});
        String[][] extResult2 = {{"spring", "fall"},
                                 {"summer", "winter"},
                                 {"cypress", "hemlock", "spruce"}};
        result = instance.getVariables();
        for (int i=0; i<result.size(); i++) {
            BiVariable var = result.get(i);
            assertArrayEquals(extResult2[i], (String[]) var.getJavaObject());
        }     
        assertTrue(result.size() == 3);
    }
View Full Code Here


     * specified value.
     */
    public boolean containsValue(Object value) {
        Iterator itr = variables.iterator();
        while (itr.hasNext()) {
            BiVariable v = (BiVariable)itr.next();
            if (value == v.getJavaObject()) {
                return true;
            }
        }
        return false;
    }
View Full Code Here

    public V get(Object receiver, Object key) {
        checkKey(key);
        RubyObject robj = getReceiverObject(receiver);
        // attemps to retrieve global variables
        if (lazy) VariableInterceptor.tryLazyRetrieval(provider.getLocalVariableBehavior(), this, robj, key);
        BiVariable var = getVariable(robj, (String)key);
        if (var == null) return null;
        else return (V) var.getJavaObject();
    }
View Full Code Here

     *         {@code null} if this map contains no mapping for the key
     */
    public BiVariable getVariable(RubyObject receiver, String key) {
        for (int i=0; i<varNames.size(); i++) {
            if (key.equals(varNames.get(i))) {
                BiVariable var = null;
                while (var == null) {
                    try {
                        var = variables.get(i);
                    } catch (Exception e) {
                        var = null;
                    }
                }
                if (var.isReceiverIdentical(receiver)) {
                    return var;
                }
            }
        }
        return null;
View Full Code Here

    public void setVariable(RubyObject receiver, BiVariable var) {
        if (var == null) {
            return;
        }
        String key = var.getName();
        BiVariable old = getVariable(receiver, key);
        if (old != null) {
            // updates the value of an existing key-value pair
            old.setJavaObject(receiver.getRuntime(), var.getJavaObject());
        } else {
            update(key, var);
        }
    }
View Full Code Here

     */
    public V put (Object receiver, K key, V value) {
        checkKey(key);
        RubyObject robj = getReceiverObject(receiver);
        String name = ((String)key).intern();
        BiVariable v = getVariable(robj, name);
        Object oldValue = null;
        if (v != null) {
            // updates
            oldValue = v.getJavaObject();
            v.setJavaObject(robj.getRuntime(), value);
        } else {
            // creates new value
            v = VariableInterceptor.getVariableInstance(provider.getLocalVariableBehavior(), robj, name, value);
            if (v != null) {
                update(name, v);
View Full Code Here

        checkKey(key);
        RubyObject robj = getReceiverObject(receiver);
        String name = ((String)key).intern();
        for (int i=0; i<varNames.size(); i++) {
            if (name.equals(varNames.get(i))) {
                BiVariable var = variables.get(i);
                if (var.getReceiver() == robj) {
                    varNames.remove(i);
                    BiVariable v = variables.remove(i);
                    v.remove();
                    return (V)v.getJavaObject();
                }
            }
        }
        return null;
    }
View Full Code Here

        }
        if (!name.startsWith("$")) {
            name = "$".concat(name);
        }
        RubyObject receiver = (RubyObject)container.getProvider().getRuntime().getTopSelf();
        BiVariable v =
            VariableInterceptor.getVariableInstance(LocalVariableBehavior.BSF,receiver, name, bean.bean, bean.type);
        container.getVarMap().setVariable(receiver, v);
    }
View Full Code Here

    public IRubyObject setInstanceVariable(IRubyObject obj, String variableName, IRubyObject value) {
        BiVariableMap map = container.getVarMap();
        synchronized (map) {
            if (map.containsKey(variableName)) {
                BiVariable bv = map.getVariable((RubyObject)container.getProvider().getRuntime().getTopSelf(), variableName);
                bv.setRubyObject(value);
            } else {
                InstanceVariable iv = new InstanceVariable(obj, variableName, value);
                map.update(variableName, iv);
            }
        }
View Full Code Here

    public IRubyObject getInstanceVariable(IRubyObject obj, String variableName) {
        BiVariableMap map = container.getVarMap();
        synchronized (map) {
            if (map.containsKey(variableName)) {
                BiVariable bv = map.getVariable((RubyObject)container.getProvider().getRuntime().getTopSelf(), variableName);
                return bv.getRubyObject();
            }
        }
        return null;
    }
View Full Code Here

TOP

Related Classes of org.jruby.embed.variable.BiVariable

Copyright © 2018 www.massapicom. 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.