https://github.com/automerge/automerge-java
https://github.com/automerge/automerge-java
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/automerge/automerge-java
- Owner: automerge
- Created: 2022-12-05T16:43:44.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-18T22:45:34.000Z (over 2 years ago)
- Last Synced: 2024-05-10T00:18:29.423Z (about 2 years ago)
- Language: Java
- Size: 185 KB
- Stars: 15
- Watchers: 2
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome-java - Automerge Java
README
# Automerge for Java
This is a java implmentation of automerge. It is implemented by wrapping the
[Rust automerge implementation](https://github.com/automerge/automerge) but you
shouldn't have to think about that. If you _are_ interested take a look at
[HACKING.md](./HACKING.md)
Documentation is mostly just [the API docs](https://www.javadoc.io/doc/org.automerge/automerge).
## Installation
### Maven
```xml
org.automerge
automerge
0.0.7
```
### Gradle (including android)
```kotlin
dependencies {
implementation group: 'org.automerge', name: 'automerge', version: "0.0.6"
}
```
### Leiningen
```
:dependencies [[org.automerge/automerge "0.0.7"]]
```
## A quick example
```java
import org.automerge.ChangeHash;
import org.automerge.Document;
import org.automerge.ObjectId;
import org.automerge.ObjectType;
import org.automerge.Transaction;
public class App {
public static void main(String[] args) {
// Create an object
Document doc = new Document();
ObjectId text;
try(Transaction tx = doc.startTransaction()) {
// Create a text object under the "text" key of the root map
text = tx.set(ObjectId.ROOT, "text", ObjectType.TEXT);
tx.spliceText(text, 0, 0, "Hello world");
tx.commit();
}
// save the document
byte[] docBytes = doc.save();
// Load the document
Document doc2 = Document.load(docBytes);
System.out.println(doc2.text(text).get().toString()); // Prints "Hello world"
// Modify the doc in doc2
try(Transaction tx = doc2.startTransaction()) {
tx.spliceText(text, 5, 0, " beautiful");
tx.commit();
}
// Modify the doc in doc1
try(Transaction tx = doc.startTransaction()) {
tx.spliceText(text, 5, 0, " there");
tx.commit();
}
// Merge the changes
doc.merge(doc2);
// Prints either "Hello there beautiful world" or "hello beautiful there world"
// depending on the actor IDs that were generated for each document.
System.out.println(doc.text(text).get().toString());
}
}
```