Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
**** BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2009-2013 Yoko Harada <yokolet@gmail.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the EPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the EPL, the GPL or the LGPL. **** END LICENSE BLOCK *****
 
 package org.jruby.embed.internal;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
Ruby-Java bi-directional variable map implementation. Keys of this map should be String, and values should be org.jruby.embed.variable.BiVariable type object. This map does not permit null or empty key. Each operation of this class is not synchronized; however, concurrent access to values are synchronized. When ScriptingContainer is shared by multiple threads, specify thread safe for a local context scope, which makes a map thread local. Usage example:
         ScriptingContainer container = new ScriptingContainer();
         Map map = container.getVarMap();
         map.put("@coefficient", new Float(3.14));
or, using a shortcut method: *
         ScriptingContainer container = new ScriptingContainer();
         container.put("@coefficient", new Float(3.14));

Author(s):
Yoko Harada <yokolet@gmail.com>
 
 public class BiVariableMap<K, V> implements Map<K, V> {
     private final LocalContextProvider provider;
     private List<StringvarNames = null;
     private List<BiVariablevariables = null;
     private boolean lazy;

    
Constructs an empty map. Users do not instantiate this map. The map is created internally.

Parameters:
runtime is environment where variables are used to execute Ruby scripts.
behavior is one of variable behaviors defined in VariableBehavior.
 
     public BiVariableMap(LocalContextProvider providerboolean lazy) {
         this. = provider;
         this. = lazy;
     }

    
Returns a list of all names in this map.

Returns:
a List of all names.
 
     public List<StringgetNames() {
         return ;
     }

    
Returns a list of all values in this map.

Returns:
a List of all values.
 
     public List<BiVariablegetVariables() {
        return ;
    }

    
Returns a local variable behavior

Returns:
a local variable behavior
        return .getLocalVariableBehavior();
    }

    
Returns a map whose value is a Java object not a BiVariable type object.

Returns:
a Map of key and value pair, in which values are simple Java objects.
    public Map getMap() {
        Map m = new HashMap();
        for (BiVariable v : ) {
            m.put(v.getName(), v.getJavaObject());
        }
        return m;
    }

    
Returns the number of key-value mappings in this map.

Returns:
the number of key-value mappings in this map
    public int size() {
        if ( == nullreturn 0;
        return .size();
    }

    
Returns true if this map contains no key-value mappings.

Returns:
true if this map contains no key-value mappings
    public boolean isEmpty() {
        return  == null || .isEmpty();
    }
    private void checkKey(Object key) {
        if (key == null) {
            throw new NullPointerException("key is null");
        }
        if (!(key instanceof String)) {
            throw new ClassCastException("key is NOT String");
        }
        if (((String)key).length() == 0) {
            throw new IllegalArgumentException("key is empty");
        }
    }

    
Returns true if this map contains a mapping for the specified key.

Parameters:
key is a key to be tested its presence
Returns:
true if this map contains a mapping for the specified key
    public boolean containsKey(Object key) {
        if ( == nullreturn false;
        checkKey(key);
        return .contains((String)key);
    }

    
Returns true if this map maps one or more keys to the specified value.

Parameters:
value is a Java object to be tested it presence
Returns:
Returns true if this map maps one or more keys to the specified value.
    public boolean containsValue(Object value) {
        Iterator itr = .iterator();
        while (itr.hasNext()) {
            BiVariable v = (BiVariable)itr.next();
            if (value == v.getJavaObject()) {
                return true;
            }
        }
        return false;
    }

    
Returns the value in simple Java object to which the specified key is mapped, or null if this map contains no mapping for the key.

Parameters:
key is the key whose associated value is to be returned
Returns:
the value in simple Java object to which the specified key is mapped, or null if this map contains no mapping for the key
    public V get(Object key) {
        return get(nullkey);
    }

    
Returns the value in simple Java object to which the specified receiver and key is mapped, or null if this map contains no mapping for the key in a given receiver.

Parameters:
receiver is a receiver object to get the value from
key is the key whose associated value is to be returned
Returns:
the value in simple Java object to which the specified key is mapped, or null if this map contains no mapping for the key
    public V get(Object receiverObject key) {
        checkKey(key);
        RubyObject robj = getReceiverObject(receiver);
        // attemps to retrieve global variables
        if () VariableInterceptor.tryLazyRetrieval(.getLocalVariableBehavior(), thisrobjkey);
        BiVariable var = getVariable(robj, (String)key);
        if (var == nullreturn null;
        else return (V) var.getJavaObject();
    }
    private RubyObject getReceiverObject(Object receiver) {
        if (receiver == null || !(receiver instanceof IRubyObject)) return (RubyObject).getRuntime().getTopSelf();
        else if (receiver instanceof RubyObjectreturn (RubyObject)receiver;
        else return (RubyObject)((IRubyObject)receiver).getRuntime().getTopSelf();
    }

    
Returns the value in BiVariable type to which the specified key is mapped, or null if this map contains no mapping for the key.

Parameters:
key is the key whose associated BiVariable object is to be returned
Returns:
the BiVariable type object to which the specified key is mapped, or null if this map contains no mapping for the key
    @Deprecated
    public BiVariable getVariable(String key) {
        return getVariable((RubyObject).getRuntime().getTopSelf(), key);
    }

    
Returns the value in BiVariable type to which the specified key is mapped, or null if this map contains no mapping for the key.

Parameters:
receiver is a receiver object to get key-value pair from
key is the key whose associated BiVariable object is to be returned
Returns:
the BiVariable type object to which the specified key is mapped, or null if this map contains no mapping for the key
    public BiVariable getVariable(RubyObject receiverString key) {
        if ( == nullreturn null;
        for (int i=0; i<.size(); i++) {
            if (key.equals(.get(i))) {
                BiVariable var = null;
                while (var == null) {
                    try {
                        var = .get(i);
                    } catch (Exception e) {
                        var = null;
                    }
                }
                if (var.isReceiverIdentical(receiver)) {
                    return var;
                }
            }
        }
        return null;
    }
    @Deprecated
    public void setVariable(BiVariable var) {
    }
    public void setVariable(RubyObject receiverBiVariable var) {
        if (var == null) {
            return;
        }
        String key = var.getName();
        BiVariable old = getVariable(receiverkey);
        if (old != null) {
            // updates the value of an existing key-value pair
            old.setJavaObject(receiver.getRuntime(), var.getJavaObject());
        } else {
            update(keyvar);
        }
    }

    
Associates the specified value with the specified key in this map. The values is a simple Java object. If the map previously contained a mapping for the key, the old value is replaced by the specified value.

Parameters:
key the key with which the specified value is to be associated
value a simple Java object to be associated with the specified key
Returns:
the previous value associated with key, or null if there was no mapping for key.
    public V put (K key, V value) {
        return put(nullkeyvalue);
    }

    
Associates the specified value with the specified key in this map. The values is a simple Java object. If the map previously contained a mapping for the key, the old value is replaced by the specified value.

Parameters:
receiver a receiver object to associate a given key-value pair with
key the key with which the specified value is to be associated
value a simple Java object to be associated with the specified key
Returns:
the previous value associated with key, or null if there was no mapping for key.
    public V put (Object receiver, K key, V value) {
        checkKey(key);
        RubyObject robj = getReceiverObject(receiver);
        String name = ((String)key).intern();
        BiVariable v = getVariable(robjname);
        Object oldValue = null;
        if (v != null) {
            // updates
            oldValue = v.getJavaObject();
            v.setJavaObject(robj.getRuntime(), value);
        } else {
            // creates new value
            v = VariableInterceptor.getVariableInstance(.getLocalVariableBehavior(), robjnamevalue);
            if (v != null) {
                update(namev);
            }
        }
        return (V)oldValue;
    }

    
Returns Ruby's local variable names this map has. The returned array is mainly used to inject local variables to Ruby scripts while parsing.

Returns:
String array of Ruby's local variable names
    public String[] getLocalVarNames() {
        if ( == nullreturn null;
        List<StringlocalVarNames = new ArrayList<String>();
        for (BiVariable v : ) {
            if (v.getType() == ..) {
                localVarNames.add(v.getName());
            }
        }
        if (localVarNames.size() > 0) {
            return localVarNames.toArray(new String[localVarNames.size()]);
        }
        return null;
    }

    
Returns Ruby's local variable values this map has. The returned array is mainly used to inject local variables to Ruby scripts while evaluating.

Returns:
IRubyObject array of Ruby's local variable names.
    public IRubyObject[] getLocalVarValues() {
        if ( == nullreturn null;
        List<IRubyObjectlocalVarValues = new ArrayList<IRubyObject>();
        for (BiVariable v : ) {
            if (v.getType() == ..) {
                localVarValues.add(v.getRubyObject());
            }
        }
        if (localVarValues.size() > 0) {
            return localVarValues.toArray(new IRubyObject[localVarValues.size()]);
        }
        return null;
    }
    void inject(ManyVarsDynamicScope scopeint depthIRubyObject receiver) {
        VariableInterceptor.inject(this.getRuntime(), scopedepthreceiver);
    }
    void retrieve(IRubyObject receiver) {
        RubyObject robj = getReceiverObject(receiver);
        VariableInterceptor.retrieve(.getLocalVariableBehavior(), thisrobj);
    }
    void terminate() {
    }

    
Removes the mapping for a key from this map if it is present in a top level.

Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.

Parameters:
key the key whose mapping is to be removed from the map
Returns:
the previous value associated with key, or null if there was no mapping for key.
    public V remove(Object key) {
        return removeFrom(.getRuntime().getTopSelf(), key);
    }

    
Removes the mapping for a key from this map if it is present in a given receiver.

Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.

Parameters:
key the key whose mapping is to be removed from the map
Returns:
the previous value associated with key, or null if there was no mapping for key.
    public V removeFrom(Object receiverObject key) {
        if ( == nullreturn null;
        checkKey(key);
        RubyObject robj = getReceiverObject(receiver);
        String name = ((String)key).intern();
        for (int i=0; i<.size(); i++) {
            if (name.equals(.get(i))) {
                BiVariable var = .get(i);
                if (var.getReceiver() == robj) {
                    .remove(i);
                    BiVariable v = .remove(i);
                    v.remove();
                    return (V)v.getJavaObject();
                }
            }
        }
        return null;
    }

    
Copies all of the mappings from the specified map to this map.

Parameters:
t mappings to be stored in this map
    public void putAll(Map<? extends K, ? extends V> t) {
        if (t == null) {
            throw new NullPointerException("map is null");
        }
        if (t.isEmpty()) {
            throw new IllegalArgumentException("map is empty");
        }
        Set set = t.entrySet();
        Iterator itr = set.iterator();
        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry)itr.next();
            if (entry.getKey() instanceof String) {
                K key = (K)entry.getKey();
                V value = (V)entry.getValue();
                put(keyvalue);
            } else {
                throw new ClassCastException("key is NOT String");
            }
        }
    }

    
Removes all of the mappings from this map. The map will be empty after this call returns. Ruby variables are also removed from Ruby instance. However, Ruby instance keep having global variable names with null value.
    public void clear() {
        if ( == nullreturn;
        boolean argv_presence = false;
        if (.contains("ARGV")) argv_presence = true;
        .clear();
        if (argv_presence.add("ARGV");
        BiVariable argv_object = null;
        for (BiVariable v : ) {
            if (v != null) {
                if ("ARGV".equals(v.getName())) {
                    argv_object = v;
                } else {
                    v.remove();
                }
            }
        }
        .clear();
        if (argv_object != null.add(argv_object);
    }

    
Returns a java.util.Set view of the keys contained in this map. The set is backed by the map, so changes to the map should be reflected in the set, and vice-versa. However, the implementation does not reflect changes currently.

Returns:
a set view of the keys contained in this map
    public Set keySet() {
        if (isEmpty()) return null;
        Set s = new HashSet();
        for (String name : ) {
            s.add(name);
        }
        return s;
    }

    
Returns a java.util.Collection view of the values contained in this map. The collection is backed by the map, so changes to the map should be reflected in the collection, and vice-versa. However, the implementation does not reflect changes currently.

Returns:
a collection view of the values contained in this map
    public Collection values() {
        if (isEmpty()) return null;
        List l = new ArrayList();
        for (BiVariable v : ) {
            l.add(v.getJavaObject());
        }
        return l;
    }

    
Returns a java.util.Set view of the mappings contained in this map. The set is backed by the map, so changes to the map should be reflected in the set, and vice-versa. However, the implementation does not reflect changes currently.

Returns:
an entry set of a map
    public Set entrySet() {
        if (isEmpty()) return null;
        return getMap().entrySet();
    }

    
Adds a key-value pair of Ruby local variable to double array.

Parameters:
name is a Ruby's local variable name
value is BiVariable type object corresponding to the name
    public void update(String nameBiVariable value) {
        if ( == null) {
             = new ArrayList<String>();
             = new ArrayList<BiVariable>();
        }
        .add(name);
        .add(value);
    }

    
Returns true when eager retrieval is requird or false when eager retrieval is unnecessary.

Returns:
true for eager retrieve, false for on-demand retrieval
    public boolean isLazy() {
        return ;
    }
New to GrepCode? Check out our FAQ X