org.jruby.embed.internal
Class BiVariableMap<K,V>

java.lang.Object
  extended by org.jruby.embed.internal.BiVariableMap<K,V>
All Implemented Interfaces:
java.util.Map<K,V>

public class BiVariableMap<K,V>
extends java.lang.Object
implements java.util.Map<K,V>

Ruby-Java bi-directional variable map implementation. Keys of this map should be String, and values should be 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:
Yoko Harada

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
BiVariableMap(LocalContextProvider provider, boolean lazy)
          Constructs an empty map.
 
Method Summary
 void clear()
          Removes all of the mappings from this map.
 boolean containsKey(java.lang.Object key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue(java.lang.Object value)
          Returns true if this map maps one or more keys to the specified value.
 java.util.Set entrySet()
          Returns a Set view of the mappings contained in this map.
 V get(java.lang.Object key)
          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.
 V get(java.lang.Object receiver, java.lang.Object key)
          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.
 LocalVariableBehavior getLocalVariableBehavior()
          Returns a local variable behavior
 java.lang.String[] getLocalVarNames()
          Returns Ruby's local variable names this map has.
 IRubyObject[] getLocalVarValues()
          Returns Ruby's local variable values this map has.
 java.util.Map getMap()
          Returns a map whose value is a Java object not a BiVariable type object.
 java.util.List<java.lang.String> getNames()
          Returns a list of all names in this map.
 BiVariable getVariable(RubyObject receiver, java.lang.String 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.
 BiVariable getVariable(java.lang.String key)
          Deprecated. 
 java.util.List<BiVariable> getVariables()
          Returns a list of all values in this map.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
 boolean isLazy()
          Returns true when eager retrieval is requird or false when eager retrieval is unnecessary.
 java.util.Set keySet()
          Returns a Set view of the keys contained in this map.
 V put(K key, V value)
          Associates the specified value with the specified key in this map.
 V put(java.lang.Object receiver, K key, V value)
          Associates the specified value with the specified key in this map.
 void putAll(java.util.Map<? extends K,? extends V> t)
          Copies all of the mappings from the specified map to this map.
 V remove(java.lang.Object key)
          Removes the mapping for a key from this map if it is present in a top level.
 V remove(java.lang.Object receiver, java.lang.Object key)
          Removes the mapping for a key from this map if it is present in a given receiver.
 void setVariable(BiVariable var)
          Deprecated. 
 void setVariable(RubyObject receiver, BiVariable var)
           
 int size()
          Returns the number of key-value mappings in this map.
 void update(java.lang.String name, BiVariable value)
          Adds a key-value pair of Ruby local variable to double array.
 java.util.Collection values()
          Returns a Collection view of the values contained in this map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

BiVariableMap

public BiVariableMap(LocalContextProvider provider,
                     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.
Method Detail

getNames

public java.util.List<java.lang.String> getNames()
Returns a list of all names in this map.

Returns:
a List of all names.

getVariables

public java.util.List<BiVariable> getVariables()
Returns a list of all values in this map.

Returns:
a List of all values.

getLocalVariableBehavior

public LocalVariableBehavior getLocalVariableBehavior()
Returns a local variable behavior

Returns:
a local variable behavior

getMap

public java.util.Map getMap()
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.

size

public int size()
Returns the number of key-value mappings in this map.

Specified by:
size in interface java.util.Map<K,V>
Returns:
the number of key-value mappings in this map

isEmpty

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

Specified by:
isEmpty in interface java.util.Map<K,V>
Returns:
true if this map contains no key-value mappings

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey in interface java.util.Map<K,V>
Parameters:
key - is a key to be tested its presence
Returns:
true if this map contains a mapping for the specified key

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the specified value.

Specified by:
containsValue in interface java.util.Map<K,V>
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.

get

public V get(java.lang.Object key)
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.

Specified by:
get in interface java.util.Map<K,V>
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

get

public V get(java.lang.Object receiver,
             java.lang.Object key)
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

getVariable

@Deprecated
public BiVariable getVariable(java.lang.String key)
Deprecated. 

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

getVariable

public BiVariable getVariable(RubyObject receiver,
                              java.lang.String 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

setVariable

@Deprecated
public void setVariable(BiVariable var)
Deprecated. 


setVariable

public void setVariable(RubyObject receiver,
                        BiVariable var)

put

public V put(K key,
             V value)
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.

Specified by:
put in interface java.util.Map<K,V>
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.

put

public V put(java.lang.Object receiver,
             K key,
             V value)
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.

getLocalVarNames

public java.lang.String[] getLocalVarNames()
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

getLocalVarValues

public IRubyObject[] getLocalVarValues()
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.

remove

public V remove(java.lang.Object key)
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.

Specified by:
remove in interface java.util.Map<K,V>
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.

remove

public V remove(java.lang.Object receiver,
                java.lang.Object 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.

putAll

public void putAll(java.util.Map<? extends K,? extends V> t)
Copies all of the mappings from the specified map to this map.

Specified by:
putAll in interface java.util.Map<K,V>
Parameters:
t - mappings to be stored in this map

clear

public void clear()
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.

Specified by:
clear in interface java.util.Map<K,V>

keySet

public java.util.Set keySet()
Returns a 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.

Specified by:
keySet in interface java.util.Map<K,V>
Returns:
a set view of the keys contained in this map

values

public java.util.Collection values()
Returns a 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.

Specified by:
values in interface java.util.Map<K,V>
Returns:
a collection view of the values contained in this map

entrySet

public java.util.Set entrySet()
Returns a 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.

Specified by:
entrySet in interface java.util.Map<K,V>
Returns:
an entry set of a map

update

public void update(java.lang.String name,
                   BiVariable value)
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

isLazy

public boolean isLazy()
Returns true when eager retrieval is requird or false when eager retrieval is unnecessary.

Returns:
true for eager retrieve, false for on-demand retrieval


Copyright © 2002-2009 JRuby Team. All Rights Reserved.