Update SharedDocument, Fix generatePositionBetween, Name magic constant

This commit is contained in:
MrLetsplay 2023-12-18 22:34:34 +01:00
parent b7a6de759e
commit 3baa9ce1e2
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
5 changed files with 39 additions and 15 deletions

View File

@ -10,8 +10,8 @@ import me.mrletsplay.shareclientcore.connection.SerializableObject;
public record Char(Identifier[] position, int lamport, char value) implements SerializableObject {
public static final Char START_OF_DOCUMENT = new Char(new Identifier[] { new Identifier(1, 0) }, 0, '^');
public static final Char END_OF_DOCUMENT = new Char(new Identifier[] { new Identifier(Util.BASE - 1, 0) }, 0, '$');
public static final Char START_OF_DOCUMENT = new Char(new Identifier[] { new Identifier(1, Identifier.DEFAULT_SITE) }, 0, '^');
public static final Char END_OF_DOCUMENT = new Char(new Identifier[] { new Identifier(Util.BASE - 1, Identifier.DEFAULT_SITE) }, 0, '$');
@Override
public int hashCode() {

View File

@ -2,6 +2,8 @@ package me.mrletsplay.shareclientcore.document;
public record Identifier(int digit, int site) implements Comparable<Identifier> {
public static final int DEFAULT_SITE = 0;
@Override
public int compareTo(Identifier o) {
int tmp;

View File

@ -21,25 +21,27 @@ public class SharedDocument implements MessageListener {
private int lamport;
private Set<DocumentListener> listeners;
public SharedDocument(RemoteConnection connection, String path) {
public SharedDocument(RemoteConnection connection, String path, String initialContents) {
this.connection = connection;
connection.addListener(this);
this.charBag = new ArrayCharBag();
charBag.add(Char.START_OF_DOCUMENT);
charBag.add(Char.END_OF_DOCUMENT);
this.path = path;
this.site = connection.getSiteID();
this.listeners = new HashSet<>();
connection.addListener(this);
charBag.add(Char.START_OF_DOCUMENT);
charBag.add(Char.END_OF_DOCUMENT);
if(initialContents != null && !initialContents.isEmpty()) {
insert(0, initialContents, Identifier.DEFAULT_SITE);
}
}
/**
* Inserts characters into the document at the specified index
* @param index The index to insert at
* @param str The string to insert
*/
public void localInsert(int index, String str) {
public SharedDocument(RemoteConnection connection, String path) {
this(connection, path, null);
}
private Change[] insert(int index, String str, int site) {
if(index < 0 || index >= charBag.size() - 1) throw new IllegalArgumentException("Index out of bounds");
Char charBefore = charBag.get(index);
@ -56,6 +58,17 @@ public class SharedDocument implements MessageListener {
charBefore = ch;
}
return changes;
}
/**
* Inserts characters into the document at the specified index
* @param index The index to insert at
* @param str The string to insert
*/
public void localInsert(int index, String str) {
Change[] changes = insert(index, str, site);
for(Change c : changes) {
try {
connection.send(new ChangeMessage(c));

View File

@ -23,7 +23,7 @@ public class Util {
List<Identifier> newPosition = new ArrayList<>();
for(int i = 0; i < Math.min(before.length, after.length) + 1; i++) {
for(int i = 0; i < Math.max(before.length, after.length) + 1; i++) {
Identifier c1 = i >= before.length ? new Identifier(0, site) : before[i];
Identifier c2 = i >= after.length ? new Identifier(BASE - 1, site) : after[i];

View File

@ -123,6 +123,15 @@ public class DecimalTest {
assertArrayEquals(expected, newIdent);
}
@Test
public void testGeneratePosition_3() {
Identifier[] a = { new Identifier(1, 0) };
Identifier[] b = { new Identifier(1, 0), new Identifier(0, 0), new Identifier(1, 0) };
Identifier[] newIdent = Util.generatePositionBetween(a, b, 0);
Identifier[] expected = { new Identifier(1, 0), new Identifier(0, 0), new Identifier(0, 0), new Identifier(1, 0) };
assertArrayEquals(expected, newIdent);
}
@Test
public void testGeneratePositionInvalidInputFails() {
Identifier[] a = { new Identifier(1, 0), new Identifier(2, 2) };