Interface JsMap
- All Superinterfaces:
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
.
- Since:
- 7.20
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all elements from this map.boolean
Removes the mapping for the specifiedkey
from this map.<T> T
Returns the value associated with the specifiedkey
or an emptyOptional
if there is no such key in this map.boolean
Returns true if this map contains a mapping for the specifiedkey
.Associates the specifiedkey
with the specifiedvalue
.long
size()
Returns the number of key-value mappings in this map.toMap()
Converts this map to aMap
.<K,
V> Map<K, V> Converts this map to aMap
.Methods inherited from interface com.teamdev.jxbrowser.js.JsObject
call, close, frame, hasProperty, ownPropertyNames, property, propertyNames, putProperty, removeProperty
-
Method Details
-
set
Associates the specifiedkey
with the specifiedvalue
. If this map contains a value associated with thekey
, 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 theJsAccessibleTypes
class.Java collections that are not made accessible to JavaScript using the
JsAccessible
annotation or via theJsAccessibleTypes
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 theJsAccessibleTypes
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 thevalue
withvalue
- the value to be associated with thekey
- Returns:
- this map
-
get
Returns the value associated with the specifiedkey
or an emptyOptional
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 emptyOptional
if there is no such key in this map. - Throws:
ObjectClosedException
- when the JavaScript object is already disposed or invalid
-
has
Returns true if this map contains a mapping for the specifiedkey
.- Parameters:
key
- the key whose presence in this map is tested- Returns:
true
if this map contains a mapping for the specifiedkey
- Throws:
ObjectClosedException
- when the JavaScript object is already disposed or invalid
-
delete
Removes the mapping for the specifiedkey
from this map.- Parameters:
key
- the key whose mapping is deleted from this map- Returns:
true
if a mapping for the specifiedkey
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
Converts this map to aMap
.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
Converts this map to aMap
.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 typeV
according to the mapping rules.- Type Parameters:
K
- the type of the converted keysV
- the type of the converted values- Parameters:
keyType
- 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 rules- Returns:
- a map containing the converted key-value pairs from this map
- Throws:
ObjectClosedException
- when the JavaScript object is already disposed or invalid
-