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 TypeMethodDescriptionvoidclear()Removes all elements from this map.booleanRemoves the mapping for the specifiedkeyfrom this map.<T> TReturns the value associated with the specifiedkeyor an emptyOptionalif there is no such key in this map.booleanReturns true if this map contains a mapping for the specifiedkey.Associates the specifiedkeywith the specifiedvalue.longsize()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, hasProperty, ownPropertyNames, ownPropertySymbols, property, property, propertyNames, putProperty, putProperty, removeProperty, removeProperty
-
Method Details
-
set
Associates the specifiedkeywith 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 | | BigInteger | BigInt | | String | String | | Boolean | Boolean | | JsSymbol | Symbol | | 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
JsAccessibleannotation or via theJsAccessibleTypesclass.Java collections that are not made accessible to JavaScript using the
JsAccessibleannotation or via theJsAccessibleTypesclass 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
JsAccessibleannotation or via theJsAccessibleTypesclass 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 thevaluewithvalue- the value to be associated with thekey- Returns:
- this map
-
get
Returns the value associated with the specifiedkeyor an emptyOptionalif there is no such key in this map.The type mapping rules are the following:
| JavaScript | Java | |--------------------|----------------| | Number | Double | | BigInt | BigInteger | | String | String | | Boolean | Boolean | | Symbol | JsSymbol | | 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
keyor an emptyOptionalif 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:
trueif 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 specifiedkeyfrom this map.- Parameters:
key- the key whose mapping is deleted from this map- Returns:
trueif a mapping for the specifiedkeyexisted and has been removed,falseif 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 | | BigInt | BigInteger | | String | String | | Boolean | Boolean | | Symbol | JsSymbol | | 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 | | BigInt | BigInteger | | String | String | | Boolean | Boolean | | Symbol | JsSymbol | | 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
Kand values must be of a type convertible to the typeVaccording 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
-