Interface JsMap

All Superinterfaces:
JsObject

public interface JsMap extends JsObject
A JavaScript map.

A map object can be passed between Java and JavaScript as a method argument or a return value. The object lifetime is bound to the lifetime of the frame this object belongs to. When the owner frame is unloaded, all the JavaScript objects are automatically disposed.

An attempt to access a disposed JavaScript object will result in IllegalStateException.

Since:
7.20
  • Method Details

    • set

      JsMap set(Object key, Object value)
      Associates the specified key with the specified value. If this map contains a value associated with the key, it will be replaced.

      The type mapping rules are the following:

      
       | Java               | JavaScript                    |
       |--------------------|-------------------------------|
       | Double             | Number                        |
       | String             | String                        |
       | Boolean            | Boolean                       |
       | null               | null                          |
       | JsObject           | Object                        |
       | Node               | Node                          |
       | List<?>            | Array or Proxy Object         |
       | Set<?>             | Set or Proxy Object           |
       | Map<?,?>           | Map or Proxy Object           |
       | byte[]             | ArrayBuffer                   |
       | Object             | Proxy Object                  |
       

      If you pass a non-primitive Java object to JavaScript, it will be converted into a "proxy" JavaScript object. Method and property calls to this object will be delegated to the Java object. For security reasons, JavaScript can access only those methods and fields of the injected Java object that are explicitly marked as accessible either using the JsAccessible annotation or via the JsAccessibleTypes class.

      Java collections that are not made accessible to JavaScript using the JsAccessible annotation or via the JsAccessibleTypes class are converted to JavaScript collections. The content of the converted collection is a deep copy of the Java collection. Modifications of the converted collection in JavaScript do not affect the collection in Java.

      Java collections that are made accessible to JavaScript using the JsAccessible annotation or via the JsAccessibleTypes class are wrapped into a JavaScript proxy object. Such proxy objects can be used to modify the collection in Java.

      Parameters:
      key - the key to associate the value with
      value - the value to be associated with the key
      Returns:
      this map
    • get

      <T> T get(Object key)
      Returns the value associated with the specified key or an empty Optional if there is no such key in this map.

      The type mapping rules are the following:

       | JavaScript         | Java           |
       |--------------------|----------------|
       | Number             | Double         |
       | String             | String         |
       | Boolean            | Boolean        |
       | null and undefined | null           |
       | Node               | JsObject, Node |
       | ArrayBuffer        | JsArrayBuffer  |
       | Array              | JsArray        |
       | Set                | JsSet          |
       | Map                | JsMap          |
       | Object             | JsObject       |
       | Proxy Object       | Object         |
       

      For proxy objects, the corresponding injected Java object is returned.

      Type Parameters:
      T - the type of the returned value
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the value associated with the specified key or an empty Optional if there is no such key in this map.
      Throws:
      ObjectClosedException - when the JavaScript object is already disposed or invalid
    • has

      boolean has(Object key)
      Returns true if this map contains a mapping for the specified key.
      Parameters:
      key - the key whose presence in this map is tested
      Returns:
      true if this map contains a mapping for the specified key
      Throws:
      ObjectClosedException - when the JavaScript object is already disposed or invalid
    • delete

      boolean delete(Object key)
      Removes the mapping for the specified key from this map.
      Parameters:
      key - the key whose mapping is deleted from this map
      Returns:
      true if a mapping for the specified key existed and has been removed, false if the mapping does not exist
      Throws:
      ObjectClosedException - when the JavaScript object is already disposed or invalid
    • size

      long size()
      Returns the number of key-value mappings in this map.
    • clear

      void clear()
      Removes all elements from this map. Does nothing if the map is empty.
    • toMap

      Map<Object,Object> toMap()
      Converts this map to a Map.

      The type mapping rules for the map elements are the following:

      
       | JavaScript         | Java           |
       |--------------------|----------------|
       | Number             | Double         |
       | String             | String         |
       | Boolean            | Boolean        |
       | null and undefined | null           |
       | Node               | Node, JsObject |
       | ArrayBuffer        | byte[]         |
       | Array              | List<?>        |
       | Set                | Set<?>         |
       | Map                | Map<?,?>       |
       | Object             | JsObject       |
       | Proxy Object       | Object         |
       

      Proxy objects are mapped to the corresponding injected Java object.

      Returns:
      a map containing the converted key-value pairs from this map
      Throws:
      ObjectClosedException - when the JavaScript object is already disposed or invalid
    • toMap

      <K, V> Map<K,V> toMap(Class<K> keyType, Class<V> valueType)
      Converts this map to a Map.

      The type mapping rules for the map elements are the following:

      
       | JavaScript         | Java           |
       |--------------------|----------------|
       | Number             | Double         |
       | String             | String         |
       | Boolean            | Boolean        |
       | null and undefined | null           |
       | Node               | Node, JsObject |
       | ArrayBuffer        | byte[]         |
       | Array              | List<?>        |
       | Set                | Set<?>         |
       | Map                | Map<?,?>       |
       | Object             | JsObject       |
       | Proxy Object       | Object         |
       

      Proxy objects are mapped to the corresponding injected Java object.

      All keys in this map must be of a type convertible to the type K and values must be of a type convertible to the type V according to the mapping rules.

      Type Parameters:
      K - the type of the converted keys
      V - the type of the converted values
      Parameters:
      keyType - the Java type to convert the keys in this map to, according to the mapping rules
      valueType - the Java type to convert the values in this map to, according to the mapping rules
      Returns:
      a map containing the converted key-value pairs from this map
      Throws:
      ObjectClosedException - when the JavaScript object is already disposed or invalid