public interface JsMap extends JsObject
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
.
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all elements from this map.
|
boolean |
delete(java.lang.Object key)
Removes the mapping for the specified
key from this map. |
<T> T |
get(java.lang.Object key)
Returns the value associated with the specified
key or an empty Optional
if there is no such key in this map. |
boolean |
has(java.lang.Object key)
Returns true if this map contains a mapping for the specified
key . |
JsMap |
set(java.lang.Object key,
java.lang.Object value)
Associates the specified
key with the specified value . |
long |
size()
Returns the number of key-value mappings in this map.
|
java.util.Map<java.lang.Object,java.lang.Object> |
toMap()
Converts this map to a
Map . |
<K,V> java.util.Map<K,V> |
toMap(java.lang.Class<K> keyType,
java.lang.Class<V> valueType)
Converts this map to a
Map . |
call, close, frame, hasProperty, ownPropertyNames, property, propertyNames, putProperty, removeProperty
JsMap set(java.lang.Object key, java.lang.Object value)
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.
key
- the key to associate the value
withvalue
- the value to be associated with the key
<T> T get(java.lang.Object key)
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.
T
- the type of the returned valuekey
- the key whose associated value is to be returnedkey
or an empty Optional
if
there is no such key in this map.ObjectClosedException
- when the JavaScript object is already disposed or invalidboolean has(java.lang.Object key)
key
.key
- the key whose presence in this map is testedtrue
if this map contains a mapping for the specified key
ObjectClosedException
- when the JavaScript object is already disposed or invalidboolean delete(java.lang.Object key)
key
from this map.key
- the key whose mapping is deleted from this maptrue
if a mapping for the specified key
existed and has been removed,
false
if the mapping does not existObjectClosedException
- when the JavaScript object is already disposed or invalidlong size()
void clear()
java.util.Map<java.lang.Object,java.lang.Object> toMap()
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.
ObjectClosedException
- when the JavaScript object is already disposed or invalid<K,V> java.util.Map<K,V> toMap(java.lang.Class<K> keyType, java.lang.Class<V> valueType)
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.
K
- the type of the converted keysV
- the type of the converted valueskeyType
- the Java type to convert the keys in this map to, according to the mapping rulesvalueType
- the Java type to convert the values in this map to, according to the mapping rulesObjectClosedException
- when the JavaScript object is already disposed or invalid