Class BiVariableMap

java.lang.Object
org.jruby.embed.internal.BiVariableMap
All Implemented Interfaces:
Map<String,Object>

public class BiVariableMap extends Object implements Map<String,Object>
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 <[email protected]>
  • Constructor Details

    • BiVariableMap

      public BiVariableMap(LocalContextProvider provider, boolean lazy)
      Constructs an empty map. Users do not instantiate this map. The map is created internally.
      Parameters:
      provider -
      lazy -
  • Method Details

    • getNames

      public List<String> getNames()
      Returns a list of all names in this map.
      Returns:
      a List of all names.
    • getVariables

      public List<BiVariable> getVariables()
      Returns a list of all values in this map.
      Returns:
      a List of all values.
    • getRuntime

      public Ruby getRuntime()
    • getLocalVariableBehavior

      public LocalVariableBehavior getLocalVariableBehavior()
      Returns a local variable behavior
      Returns:
      a local variable behavior
    • getMap

      public Map<String,Object> 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 Map<String,Object>
      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 Map<String,Object>
      Returns:
      true if this map contains no key-value mappings
    • containsKey

      public boolean containsKey(Object key)
      Returns true if this map contains a mapping for the specified key.
      Specified by:
      containsKey in interface Map<String,Object>
      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(Object value)
      Returns true if this map maps one or more keys to the specified value.
      Specified by:
      containsValue in interface Map<String,Object>
      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 Object get(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 Map<String,Object>
      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 Object get(Object receiver, 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

      public BiVariable getVariable(String name)
      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:
      name - 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, String name)
      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
      name - 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

      public void setVariable(BiVariable var)
    • setVariable

      public void setVariable(RubyObject receiver, BiVariable var)
    • put

      public Object put(String key, Object 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 Map<String,Object>
      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 Object put(Object receiver, String key, Object 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 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 Object remove(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 Map<String,Object>
      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.
    • removeFrom

      public Object removeFrom(Object receiver, 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(Map<? extends String,? extends Object> map)
      Copies all of the mappings from the specified map to this map.
      Specified by:
      putAll in interface Map<String,Object>
      Parameters:
      map - 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 Map<String,Object>
    • keySet

      public Set<String> 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 Map<String,Object>
      Returns:
      a set view of the keys contained in this map
    • values

      public Collection<Object> 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 Map<String,Object>
      Returns:
      a collection view of the values contained in this map
    • entrySet

      public Set<Map.Entry<String,Object>> 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 Map<String,Object>
      Returns:
      an entry set of a map
    • update

      public void update(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
    • updateVariable

      public void updateVariable(RubyObject receiver, String name, IRubyObject value, Class<? extends BiVariable> type)
    • isLazy

      public boolean isLazy()
      Returns true when eager retrieval is required or false when eager retrieval is unnecessary.
      Returns:
      true for eager retrieve, false for on-demand retrieval
    • toString

      public String toString()
      Overrides:
      toString in class Object