Class JniYText
- All Implemented Interfaces:
AutoCloseable,net.carcdr.ycrdt.YText
YText provides efficient collaborative text editing with automatic conflict resolution. Multiple users can edit the same text 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 YDoc();
YText text = doc.getText("mytext")) {
text.insert(0, "Hello");
text.push(" World");
System.out.println(text.toString()); // "Hello World"
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this YText and releases native resources.voiddelete(int index, int length) Deletes a range of text (creates implicit transaction).voiddelete(net.carcdr.ycrdt.YTransaction txn, int index, int length) Deletes a range of text within an existing transaction.voidInserts text at the specified index (creates implicit transaction).voidInserts text at the specified index within an existing transaction.booleanisClosed()Checks if this YText has been closed.intlength()Returns the length of the text.intlength(net.carcdr.ycrdt.YTransaction txn) Returns the length of the text.net.carcdr.ycrdt.YSubscriptionobserve(net.carcdr.ycrdt.YObserver observer) Registers an observer to be notified of changes to this text.voidAppends text to the end (creates implicit transaction).voidAppends text to the end within an existing transaction.toString()Returns the text content as a string.voidunobserveById(long subscriptionId) Package-private method to unobserve by subscription ID.
-
Method Details
-
length
public int length()Returns the length of the text.- Specified by:
lengthin interfacenet.carcdr.ycrdt.YText- Returns:
- The number of characters in the text
- Throws:
IllegalStateException- if the text has been closed
-
length
public int length(net.carcdr.ycrdt.YTransaction txn) Returns the length of the text.- Specified by:
lengthin interfacenet.carcdr.ycrdt.YText- Parameters:
txn- The transaction to use for this operation- Returns:
- The number of characters in the text
- Throws:
IllegalStateException- if the text has been closed
-
toString
Returns the text content as a string.- Specified by:
toStringin interfacenet.carcdr.ycrdt.YText- Overrides:
toStringin classObject- Returns:
- The current text content
- Throws:
IllegalStateException- if the text has been closed
-
insert
Inserts text at the specified index within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { text.insert(txn, 0, "Hello"); text.insert(txn, 5, " World"); }- Specified by:
insertin interfacenet.carcdr.ycrdt.YText- Parameters:
txn- The transaction to use for this operationindex- The position at which to insert the text (0-based)chunk- The text to insert- Throws:
IllegalArgumentException- if txn or chunk is nullIllegalStateException- if the text has been closedIndexOutOfBoundsException- if index is negative or greater than the current length
-
insert
Inserts text at the specified index (creates implicit transaction).- Specified by:
insertin interfacenet.carcdr.ycrdt.YText- Parameters:
index- The position at which to insert the text (0-based)chunk- The text to insert- Throws:
IllegalArgumentException- if chunk is nullIllegalStateException- if the text has been closedIndexOutOfBoundsException- if index is negative or greater than the current length
-
push
Appends text to the end within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { text.push(txn, "Hello"); text.push(txn, " World"); }- Specified by:
pushin interfacenet.carcdr.ycrdt.YText- Parameters:
txn- The transaction to use for this operationchunk- The text to append- Throws:
IllegalArgumentException- if txn or chunk is nullIllegalStateException- if the text has been closed
-
push
Appends text to the end (creates implicit transaction).- Specified by:
pushin interfacenet.carcdr.ycrdt.YText- Parameters:
chunk- The text to append- Throws:
IllegalArgumentException- if chunk is nullIllegalStateException- if the text has been closed
-
delete
public void delete(net.carcdr.ycrdt.YTransaction txn, int index, int length) Deletes a range of text within an existing transaction.Use this method to batch multiple operations:
try (JniYTransaction txn = doc.beginTransaction()) { text.insert(txn, 0, "Hello World"); text.delete(txn, 5, 6); // Delete " World" }- Specified by:
deletein interfacenet.carcdr.ycrdt.YText- Parameters:
txn- The transaction to use for this operationindex- The starting position (0-based)length- The number of characters to delete- Throws:
IllegalArgumentException- if txn is nullIllegalStateException- if the text has been closedIndexOutOfBoundsException- if the range is invalid
-
delete
public void delete(int index, int length) Deletes a range of text (creates implicit transaction).- Specified by:
deletein interfacenet.carcdr.ycrdt.YText- Parameters:
index- The starting position (0-based)length- The number of characters to delete- Throws:
IllegalStateException- if the text has been closedIndexOutOfBoundsException- if the range is invalid
-
isClosed
public boolean isClosed()Checks if this YText has been closed.- Specified by:
isClosedin interfacenet.carcdr.ycrdt.YText- Returns:
- true if this YText has been closed, false otherwise
-
observe
public net.carcdr.ycrdt.YSubscription observe(net.carcdr.ycrdt.YObserver observer) Registers an observer to be notified of changes to this text.The observer will be called whenever this text is modified. Multiple observers can be registered on the same text.
Example:
try (YSubscription sub = text.observe(event -> { System.out.println("Text changed!"); for (YChange change : event.getChanges()) { System.out.println(" " + change); } })) { text.insert(0, "Hello"); // Triggers observer }- Specified by:
observein interfacenet.carcdr.ycrdt.YText- Parameters:
observer- the observer to register- Returns:
- a subscription handle that can be used to unobserve
- Throws:
IllegalArgumentException- if observer is nullIllegalStateException- if this text has been closed
-
unobserveById
public void unobserveById(long subscriptionId) Package-private method to unobserve by subscription ID. Called by YSubscription.close().- Parameters:
subscriptionId- the subscription ID to remove
-
close
public void close()Closes this YText and releases native resources.After calling this method, any operations on this YText 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.YText
-