Class JniYMap
- All Implemented Interfaces:
AutoCloseable,net.carcdr.ycrdt.YMap
YMap provides efficient collaborative map operations with automatic conflict resolution. Multiple users can modify the same map simultaneously, and changes will be merged automatically using CRDT algorithms.
This class implements Closeable and should be used with try-with-resources
to ensure proper cleanup of native resources.
Example usage:
try (YDoc doc = new JniYDoc();
YMap map = doc.getMap("mymap")) {
map.setString("name", "Alice");
map.setDouble("age", 30.0);
System.out.println(map.toJson()); // {"name":"Alice","age":30.0}
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from the map.voidclear(net.carcdr.ycrdt.YTransaction txn) Removes all entries from the map within an existing transaction.voidclose()Closes this YMap and releases native resources.booleancontainsKey(String key) Checks if a key exists in the map.booleancontainsKey(net.carcdr.ycrdt.YTransaction txn, String key) Checks if a key exists in the map using an existing transaction.Gets a YDoc subdocument from the map by key.Gets a YDoc subdocument from the map by key using an existing transaction.doubleGets a double value by key.doubleGets a double value by key using an existing transaction.Gets a string value by key.Gets a string value by key using an existing transaction.booleanisClosed()Checks if this YMap has been closed.booleanisEmpty()Checks if the map is empty.String[]keys()Gets all keys from the map.String[]keys(net.carcdr.ycrdt.YTransaction txn) Gets all keys from the map using an existing transaction.net.carcdr.ycrdt.YSubscriptionobserve(net.carcdr.ycrdt.YObserver observer) Registers an observer to be notified when this map changes.voidRemoves a key from the map.voidRemoves a key from the map within an existing transaction.voidSets a YDoc subdocument value in the map.voidSets a YDoc subdocument value in the map within an existing transaction.voidSets a double value in the map.voidSets a double value in the map within an existing transaction.voidSets a string value in the map.voidSets a string value in the map within an existing transaction.intsize()Returns the number of entries in the map.intsize(net.carcdr.ycrdt.YTransaction txn) Returns the number of entries in the map using an existing transaction.toJson()Returns a JSON string representation of the map.toJson(net.carcdr.ycrdt.YTransaction txn) Returns a JSON string representation of the map using an existing transaction.voidunobserveById(long subscriptionId) Unregisters an observer by its subscription ID.
-
Method Details
-
size
public int size()Returns the number of entries in the map.- Specified by:
sizein interfacenet.carcdr.ycrdt.YMap- Returns:
- The size of the map
- Throws:
IllegalStateException- if the map has been closed
-
size
public int size(net.carcdr.ycrdt.YTransaction txn) Returns the number of entries in the map using an existing transaction.- Specified by:
sizein interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to use for this operation- Returns:
- The size of the map
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the map has been closed
-
isEmpty
public boolean isEmpty()Checks if the map is empty.- Specified by:
isEmptyin interfacenet.carcdr.ycrdt.YMap- Returns:
- true if the map contains no entries, false otherwise
- Throws:
IllegalStateException- if the map has been closed
-
getString
Gets a string value by key.- Specified by:
getStringin interfacenet.carcdr.ycrdt.YMap- Parameters:
key- The key to look up- Returns:
- The string value, or null if key not found or value is not a string
- Throws:
IllegalArgumentException- if key is nullIllegalStateException- if the map has been closed
-
getString
Gets a string value by key using an existing transaction.- Specified by:
getStringin interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to use for this operationkey- The key to look up- Returns:
- The string value, or null if key not found or value is not a string
- Throws:
IllegalArgumentException- if txn or key is nullIllegalStateException- if the map has been closed
-
getDouble
Gets a double value by key.- Specified by:
getDoublein interfacenet.carcdr.ycrdt.YMap- Parameters:
key- The key to look up- Returns:
- The double value, or 0.0 if key not found or value is not a number
- Throws:
IllegalArgumentException- if key is nullIllegalStateException- if the map has been closed
-
getDouble
Gets a double value by key using an existing transaction.- Specified by:
getDoublein interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to use for this operationkey- The key to look up- Returns:
- The double value, or 0.0 if key not found or value is not a number
- Throws:
IllegalArgumentException- if txn or key is nullIllegalStateException- if the map has been closed
-
setString
Sets a string value in the map.- Specified by:
setStringin interfacenet.carcdr.ycrdt.YMap- Parameters:
key- The key to setvalue- The string value to set- Throws:
IllegalArgumentException- if key or value is nullIllegalStateException- if the map has been closed
-
setString
Sets a string value in the map within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { map.setString(txn, "name", "Alice"); map.setString(txn, "city", "NYC"); }- Specified by:
setStringin interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to usekey- The key to setvalue- The string value to set- Throws:
IllegalArgumentException- if txn, key, or value is nullIllegalStateException- if the map or transaction has been closed
-
setDouble
Sets a double value in the map.- Specified by:
setDoublein interfacenet.carcdr.ycrdt.YMap- Parameters:
key- The key to setvalue- The double value to set- Throws:
IllegalArgumentException- if key is nullIllegalStateException- if the map has been closed
-
setDouble
Sets a double value in the map within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { map.setDouble(txn, "age", 30.0); map.setDouble(txn, "height", 165.5); }- Specified by:
setDoublein interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to usekey- The key to setvalue- The double value to set- Throws:
IllegalArgumentException- if txn or key is nullIllegalStateException- if the map or transaction has been closed
-
remove
Removes a key from the map.- Specified by:
removein interfacenet.carcdr.ycrdt.YMap- Parameters:
key- The key to remove- Throws:
IllegalArgumentException- if key is nullIllegalStateException- if the map has been closed
-
remove
Removes a key from the map within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { map.remove(txn, "key1"); map.remove(txn, "key2"); }- Specified by:
removein interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to usekey- The key to remove- Throws:
IllegalArgumentException- if txn or key is nullIllegalStateException- if the map or transaction has been closed
-
containsKey
Checks if a key exists in the map.- Specified by:
containsKeyin interfacenet.carcdr.ycrdt.YMap- Parameters:
key- The key to check- Returns:
- true if the key exists, false otherwise
- Throws:
IllegalArgumentException- if key is nullIllegalStateException- if the map has been closed
-
containsKey
Checks if a key exists in the map using an existing transaction.- Specified by:
containsKeyin interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to use for this operationkey- The key to check- Returns:
- true if the key exists, false otherwise
- Throws:
IllegalArgumentException- if txn or key is nullIllegalStateException- if the map has been closed
-
keys
Gets all keys from the map.- Specified by:
keysin interfacenet.carcdr.ycrdt.YMap- Returns:
- An array of all keys in the map
- Throws:
IllegalStateException- if the map has been closed
-
keys
Gets all keys from the map using an existing transaction.- Specified by:
keysin interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to use for this operation- Returns:
- An array of all keys in the map
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the map has been closed
-
clear
public void clear()Removes all entries from the map.- Specified by:
clearin interfacenet.carcdr.ycrdt.YMap- Throws:
IllegalStateException- if the map has been closed
-
clear
public void clear(net.carcdr.ycrdt.YTransaction txn) Removes all entries from the map within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { map.clear(txn); map.setString(txn, "reset", "true"); }- Specified by:
clearin interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to use- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the map or transaction has been closed
-
setDoc
Sets a YDoc subdocument value in the map.This allows embedding one YDoc inside another, enabling hierarchical document structures and composition.
Example:
try (YDoc parent = new JniYDoc(); YDoc child = new JniYDoc(); YMap map = parent.getMap("mymap")) { map.setDoc("nested", child); }- Specified by:
setDocin interfacenet.carcdr.ycrdt.YMap- Parameters:
key- The key to setsubdoc- The YDoc subdocument to set- Throws:
IllegalArgumentException- if key or subdoc is nullIllegalStateException- if the map has been closed
-
setDoc
Sets a YDoc subdocument value in the map within an existing transaction.This allows embedding one YDoc inside another, enabling hierarchical document structures and composition.
Use this method to batch multiple operations:
try (YDoc parent = new JniYDoc(); YDoc child = new JniYDoc(); YMap map = parent.getMap("mymap"); YTransaction txn = parent.beginTransaction()) { map.setDoc(txn, "nested", child); map.setString(txn, "type", "subdocument"); }- Specified by:
setDocin interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to usekey- The key to setsubdoc- The YDoc subdocument to set- Throws:
IllegalArgumentException- if txn, key, or subdoc is nullIllegalStateException- if the map or transaction has been closed
-
getDoc
Gets a YDoc subdocument from the map by key.The returned YDoc must be closed by the caller when no longer needed.
Example:
try (YDoc parent = new JniYDoc(); YMap map = parent.getMap("mymap")) { map.setDoc("nested", new JniYDoc()); try (YDoc retrieved = map.getDoc("nested")) { // Use the subdocument } }- Specified by:
getDocin interfacenet.carcdr.ycrdt.YMap- Parameters:
key- The key to look up- Returns:
- The YDoc subdocument, or null if key not found or value is not a Doc
- Throws:
IllegalArgumentException- if key is nullIllegalStateException- if the map has been closed
-
getDoc
Gets a YDoc subdocument from the map by key using an existing transaction.The returned YDoc must be closed by the caller when no longer needed.
- Specified by:
getDocin interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to use for this operationkey- The key to look up- Returns:
- The YDoc subdocument, or null if key not found or value is not a Doc
- Throws:
IllegalArgumentException- if txn or key is nullIllegalStateException- if the map has been closed
-
toJson
Returns a JSON string representation of the map.- Specified by:
toJsonin interfacenet.carcdr.ycrdt.YMap- Returns:
- A JSON string representation
- Throws:
IllegalStateException- if the map has been closed
-
toJson
Returns a JSON string representation of the map using an existing transaction.- Specified by:
toJsonin interfacenet.carcdr.ycrdt.YMap- Parameters:
txn- The transaction to use for this operation- Returns:
- A JSON string representation
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the map has been closed
-
observe
public net.carcdr.ycrdt.YSubscription observe(net.carcdr.ycrdt.YObserver observer) Registers an observer to be notified when this map changes.The observer will be called whenever entries are added, removed, or modified in this map. The observer receives a
YEventcontaining details about the changes.Example:
try (YDoc doc = new JniYDoc(); YMap map = doc.getMap("mymap"); YSubscription sub = map.observe(event -> { System.out.println("Map changed!"); for (YChange change : event.getChanges()) { // Handle change } })) { map.setString("key", "value"); // Observer is called }- Specified by:
observein interfacenet.carcdr.ycrdt.YMap- Parameters:
observer- The observer to register (must not be null)- Returns:
- A subscription handle that can be used to unregister the observer
- Throws:
IllegalArgumentException- if observer is nullIllegalStateException- if this map has been closed
-
unobserveById
public void unobserveById(long subscriptionId) Unregisters an observer by its subscription ID.This method is typically called automatically when a
YSubscriptionis closed. Users should prefer using try-with-resources with YSubscription rather than calling this method directly.- Parameters:
subscriptionId- The ID of the subscription to remove
-
isClosed
public boolean isClosed()Checks if this YMap has been closed.- Specified by:
isClosedin interfacenet.carcdr.ycrdt.YMap- Returns:
- true if this YMap has been closed, false otherwise
-
close
public void close()Closes this YMap and releases native resources.After calling this method, any operations on this YMap will throw
IllegalStateException.This method is idempotent - calling it multiple times has no effect after the first call.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfacenet.carcdr.ycrdt.YMap
-