Class JniYArray
- All Implemented Interfaces:
AutoCloseable,net.carcdr.ycrdt.YArray
YArray provides efficient collaborative array operations with automatic conflict resolution. Multiple users can modify the same array 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();
YArray array = doc.getArray("myarray")) {
array.pushString("Hello");
array.pushDouble(42.0);
array.insertString(1, "World");
System.out.println(array.toJson()); // ["Hello","World",42.0]
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this YArray and releases native resources.getDoc(int index) Gets a YDoc subdocument at the specified index.getDoc(net.carcdr.ycrdt.YTransaction txn, int index) Gets a YDoc subdocument at the specified index using an existing transaction.doublegetDouble(int index) Gets a double value at the specified index.doublegetDouble(net.carcdr.ycrdt.YTransaction txn, int index) Gets a double value at the specified index using an existing transaction.getString(int index) Gets a string value at the specified index.getString(net.carcdr.ycrdt.YTransaction txn, int index) Gets a string value at the specified index using an existing transaction.voidinsertDoc(int index, net.carcdr.ycrdt.YDoc subdoc) Inserts a YDoc subdocument at the specified index (creates implicit transaction).voidinsertDoc(net.carcdr.ycrdt.YTransaction txn, int index, net.carcdr.ycrdt.YDoc subdoc) Inserts a YDoc subdocument at the specified index within an existing transaction.voidinsertDouble(int index, double value) Inserts a double value at the specified index (creates implicit transaction).voidinsertDouble(net.carcdr.ycrdt.YTransaction txn, int index, double value) Inserts a double value at the specified index within an existing transaction.voidinsertString(int index, String value) Inserts a string value at the specified index (creates implicit transaction).voidinsertString(net.carcdr.ycrdt.YTransaction txn, int index, String value) Inserts a string value at the specified index within an existing transaction.booleanisClosed()Checks if this YArray has been closed.intlength()Returns the length of the array.intlength(net.carcdr.ycrdt.YTransaction txn) Returns the length of the array using an existing transaction.net.carcdr.ycrdt.YSubscriptionobserve(net.carcdr.ycrdt.YObserver observer) Registers an observer to be notified when this array changes.voidpushDoc(net.carcdr.ycrdt.YDoc subdoc) Appends a YDoc subdocument to the end of the array (creates implicit transaction).voidpushDoc(net.carcdr.ycrdt.YTransaction txn, net.carcdr.ycrdt.YDoc subdoc) Appends a YDoc subdocument to the end of the array within an existing transaction.voidpushDouble(double value) Appends a double value to the end of the array (creates implicit transaction).voidpushDouble(net.carcdr.ycrdt.YTransaction txn, double value) Appends a double value to the end of the array within an existing transaction.voidpushString(String value) Appends a string value to the end of the array (creates implicit transaction).voidpushString(net.carcdr.ycrdt.YTransaction txn, String value) Appends a string value to the end of the array within an existing transaction.voidremove(int index, int length) Removes a range of elements from the array (creates implicit transaction).voidremove(net.carcdr.ycrdt.YTransaction txn, int index, int length) Removes a range of elements from the array within an existing transaction.toJson()Returns a JSON string representation of the array.toJson(net.carcdr.ycrdt.YTransaction txn) Returns a JSON string representation of the array using an existing transaction.voidunobserveById(long subscriptionId) Unregisters an observer by its subscription ID.
-
Method Details
-
length
public int length()Returns the length of the array.- Specified by:
lengthin interfacenet.carcdr.ycrdt.YArray- Returns:
- The number of elements in the array
- Throws:
IllegalStateException- if the array has been closed
-
length
public int length(net.carcdr.ycrdt.YTransaction txn) Returns the length of the array using an existing transaction.- Specified by:
lengthin interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operation- Returns:
- The number of elements in the array
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the array has been closed
-
getString
Gets a string value at the specified index.- Specified by:
getStringin interfacenet.carcdr.ycrdt.YArray- Parameters:
index- The index (0-based)- Returns:
- The string value, or null if index is out of bounds or value is not a string
- Throws:
IllegalStateException- if the array has been closed
-
getString
Gets a string value at the specified index using an existing transaction.- Specified by:
getStringin interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operationindex- The index (0-based)- Returns:
- The string value, or null if index is out of bounds or value is not a string
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the array has been closed
-
getDouble
public double getDouble(int index) Gets a double value at the specified index.- Specified by:
getDoublein interfacenet.carcdr.ycrdt.YArray- Parameters:
index- The index (0-based)- Returns:
- The double value, or 0.0 if index is out of bounds or value is not a number
- Throws:
IllegalStateException- if the array has been closed
-
getDouble
public double getDouble(net.carcdr.ycrdt.YTransaction txn, int index) Gets a double value at the specified index using an existing transaction.- Specified by:
getDoublein interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operationindex- The index (0-based)- Returns:
- The double value, or 0.0 if index is out of bounds or value is not a number
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the array has been closed
-
insertString
Inserts a string value at the specified index within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { array.insertString(txn, 0, "First"); array.insertString(txn, 1, "Second"); }- Specified by:
insertStringin interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operationindex- The position at which to insert (0-based)value- The string value to insert- Throws:
IllegalArgumentException- if txn or value is nullIllegalStateException- if the array has been closedIndexOutOfBoundsException- if index is negative or greater than the current length
-
insertString
Inserts a string value at the specified index (creates implicit transaction).- Specified by:
insertStringin interfacenet.carcdr.ycrdt.YArray- Parameters:
index- The position at which to insert (0-based)value- The string value to insert- Throws:
IllegalArgumentException- if value is nullIllegalStateException- if the array has been closedIndexOutOfBoundsException- if index is negative or greater than the current length
-
insertDouble
public void insertDouble(net.carcdr.ycrdt.YTransaction txn, int index, double value) Inserts a double value at the specified index within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { array.insertDouble(txn, 0, 1.0); array.insertDouble(txn, 1, 2.0); }- Specified by:
insertDoublein interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operationindex- The position at which to insert (0-based)value- The double value to insert- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the array has been closedIndexOutOfBoundsException- if index is negative or greater than the current length
-
insertDouble
public void insertDouble(int index, double value) Inserts a double value at the specified index (creates implicit transaction).- Specified by:
insertDoublein interfacenet.carcdr.ycrdt.YArray- Parameters:
index- The position at which to insert (0-based)value- The double value to insert- Throws:
IllegalStateException- if the array has been closedIndexOutOfBoundsException- if index is negative or greater than the current length
-
pushString
Appends a string value to the end of the array within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { array.pushString(txn, "First"); array.pushString(txn, "Second"); }- Specified by:
pushStringin interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operationvalue- The string value to append- Throws:
IllegalArgumentException- if txn or value is nullIllegalStateException- if the array has been closed
-
pushString
Appends a string value to the end of the array (creates implicit transaction).- Specified by:
pushStringin interfacenet.carcdr.ycrdt.YArray- Parameters:
value- The string value to append- Throws:
IllegalArgumentException- if value is nullIllegalStateException- if the array has been closed
-
pushDouble
public void pushDouble(net.carcdr.ycrdt.YTransaction txn, double value) Appends a double value to the end of the array within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { array.pushDouble(txn, 1.0); array.pushDouble(txn, 2.0); }- Specified by:
pushDoublein interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operationvalue- The double value to append- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the array has been closed
-
pushDouble
public void pushDouble(double value) Appends a double value to the end of the array (creates implicit transaction).- Specified by:
pushDoublein interfacenet.carcdr.ycrdt.YArray- Parameters:
value- The double value to append- Throws:
IllegalStateException- if the array has been closed
-
remove
public void remove(net.carcdr.ycrdt.YTransaction txn, int index, int length) Removes a range of elements from the array within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { array.pushString(txn, "A"); array.pushString(txn, "B"); array.remove(txn, 0, 1); // Remove "A" }- Specified by:
removein interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operationindex- The starting position (0-based)length- The number of elements to remove- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the array has been closedIndexOutOfBoundsException- if the range is invalid
-
remove
public void remove(int index, int length) Removes a range of elements from the array (creates implicit transaction).- Specified by:
removein interfacenet.carcdr.ycrdt.YArray- Parameters:
index- The starting position (0-based)length- The number of elements to remove- Throws:
IllegalStateException- if the array has been closedIndexOutOfBoundsException- if the range is invalid
-
insertDoc
public void insertDoc(net.carcdr.ycrdt.YTransaction txn, int index, net.carcdr.ycrdt.YDoc subdoc) Inserts a YDoc subdocument at the specified index within an existing transaction.This allows embedding one YDoc inside another, enabling hierarchical document structures and composition.
Example:
try (YDoc parent = new JniYDoc(); YDoc child = new JniYDoc(); YArray array = parent.getArray("myarray"); YTransaction txn = parent.beginTransaction()) { array.insertDoc(txn, 0, child); }- Specified by:
insertDocin interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operationindex- The position at which to insert (0-based)subdoc- The YDoc subdocument to insert- Throws:
IllegalArgumentException- if txn or subdoc is nullIllegalStateException- if the array has been closedIndexOutOfBoundsException- if index is negative or greater than the current length
-
insertDoc
public void insertDoc(int index, net.carcdr.ycrdt.YDoc subdoc) Inserts a YDoc subdocument at the specified index (creates implicit transaction).This allows embedding one YDoc inside another, enabling hierarchical document structures and composition.
Example:
try (YDoc parent = new JniYDoc(); YDoc child = new JniYDoc(); YArray array = parent.getArray("myarray")) { array.insertDoc(0, child); }- Specified by:
insertDocin interfacenet.carcdr.ycrdt.YArray- Parameters:
index- The position at which to insert (0-based)subdoc- The YDoc subdocument to insert- Throws:
IllegalArgumentException- if subdoc is nullIllegalStateException- if the array has been closedIndexOutOfBoundsException- if index is negative or greater than the current length
-
pushDoc
public void pushDoc(net.carcdr.ycrdt.YTransaction txn, net.carcdr.ycrdt.YDoc subdoc) Appends a YDoc subdocument to the end of the array within an existing transaction.This allows embedding one YDoc inside another, enabling hierarchical document structures and composition.
Example:
try (YDoc parent = new JniYDoc(); YDoc child = new JniYDoc(); YArray array = parent.getArray("myarray"); YTransaction txn = parent.beginTransaction()) { array.pushDoc(txn, child); }- Specified by:
pushDocin interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operationsubdoc- The YDoc subdocument to append- Throws:
IllegalArgumentException- if txn or subdoc is nullIllegalStateException- if the array has been closed
-
pushDoc
public void pushDoc(net.carcdr.ycrdt.YDoc subdoc) Appends a YDoc subdocument to the end of the array (creates implicit transaction).This allows embedding one YDoc inside another, enabling hierarchical document structures and composition.
Example:
try (YDoc parent = new JniYDoc(); YDoc child = new JniYDoc(); YArray array = parent.getArray("myarray")) { array.pushDoc(child); }- Specified by:
pushDocin interfacenet.carcdr.ycrdt.YArray- Parameters:
subdoc- The YDoc subdocument to append- Throws:
IllegalArgumentException- if subdoc is nullIllegalStateException- if the array has been closed
-
getDoc
Gets a YDoc subdocument at the specified index.The returned YDoc must be closed by the caller when no longer needed.
Example:
try (YDoc parent = new JniYDoc(); YArray array = parent.getArray("myarray")) { array.pushDoc(new JniYDoc()); try (YDoc retrieved = array.getDoc(0)) { // Use the subdocument } }- Specified by:
getDocin interfacenet.carcdr.ycrdt.YArray- Parameters:
index- The index (0-based)- Returns:
- The YDoc subdocument, or null if index is out of bounds or value is not a Doc
- Throws:
IllegalStateException- if the array has been closed
-
getDoc
Gets a YDoc subdocument at the specified index using an existing transaction.The returned YDoc must be closed by the caller when no longer needed.
- Specified by:
getDocin interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operationindex- The index (0-based)- Returns:
- The YDoc subdocument, or null if index is out of bounds or value is not a Doc
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the array has been closed
-
toJson
Returns a JSON string representation of the array.- Specified by:
toJsonin interfacenet.carcdr.ycrdt.YArray- Returns:
- A JSON string representation
- Throws:
IllegalStateException- if the array has been closed
-
toJson
Returns a JSON string representation of the array using an existing transaction.- Specified by:
toJsonin interfacenet.carcdr.ycrdt.YArray- Parameters:
txn- The transaction to use for this operation- Returns:
- A JSON string representation
- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the array has been closed
-
observe
public net.carcdr.ycrdt.YSubscription observe(net.carcdr.ycrdt.YObserver observer) Registers an observer to be notified when this array changes.The observer will be called whenever elements are added, removed, or modified in this array. The observer receives a
YEventcontaining details about the changes.Example:
try (YDoc doc = new JniYDoc(); YArray array = doc.getArray("myarray"); YSubscription sub = array.observe(event -> { System.out.println("Array changed!"); for (YChange change : event.getChanges()) { // Handle change } })) { array.pushString("Hello"); // Observer is called }- Specified by:
observein interfacenet.carcdr.ycrdt.YArray- 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 array 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 YArray has been closed.- Specified by:
isClosedin interfacenet.carcdr.ycrdt.YArray- Returns:
- true if this YArray has been closed, false otherwise
-
close
public void close()Closes this YArray and releases native resources.After calling this method, any operations on this YArray 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.YArray
-