Update Document

This commit is contained in:
MrLetsplay 2023-11-23 20:00:03 +01:00
parent aa701961c8
commit 788ae0f364
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
6 changed files with 100 additions and 3 deletions

View File

@ -26,6 +26,16 @@ public class ArrayCharBag implements CharBag {
return i;
}
@Override
public Char get(int index) {
return chars.get(index);
}
@Override
public int size() {
return chars.size();
}
@Override
public String toString() {
return chars.stream()

View File

@ -1,3 +1,8 @@
package me.mrletsplay.shareclientcore.document;
public record Char(Identifier[] position, int lamport, String value) {}
public record Char(Identifier[] position, int lamport, char value) {
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, '$');
}

View File

@ -20,4 +20,24 @@ public interface CharBag {
*/
public int remove(Char character);
/**
* Finds the character at the given index and returns it
* @param index The index of the character
* @return The character
*/
public Char get(int index);
/**
* Returns the number of chars in this bag
* @return The number of chars
*/
public int size();
/**
* Collects the chars in this bag ordered by their position into a string
* @return A string containing the chars
*/
@Override
public String toString();
}

View File

@ -3,8 +3,39 @@ package me.mrletsplay.shareclientcore.document;
public class Document {
private CharBag charBag;
private int site;
private int lamport;
public Document() {
public Document(int site) {
this.charBag = new ArrayCharBag();
charBag.add(Char.START_OF_DOCUMENT);
charBag.add(Char.END_OF_DOCUMENT);
this.site = site;
}
public void localInsert(int index, String str) {
if(index < 0 || index >= charBag.size() - 1) throw new IllegalArgumentException("Index out of bounds");
Char charBefore = charBag.get(index);
Char charAfter = charBag.get(index +1);
for(char c : str.toCharArray()) {
Identifier[] newPos = Util.generatePositionBetween(charBefore.position(), charAfter.position(), site);
lamport++;
Char ch = new Char(newPos, lamport, c);
charBag.add(ch);
charBefore = ch;
}
}
public CharBag getCharBag() {
return charBag;
}
public String getContents() {
String contents = charBag.toString();
return contents.substring(1, contents.length() - 1);
}
}

View File

@ -47,7 +47,7 @@ public class Util {
throw new RuntimeException("Invalid site order");
}
return null;
return newPosition.toArray(Identifier[]::new);
}
public static int[] getIncremented(Identifier[] before, Identifier[] after, int offset) {

View File

@ -0,0 +1,31 @@
package me.mrletsplay.shareclientcore;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import me.mrletsplay.shareclientcore.document.Document;
public class DocumentTest {
@Test
public void testLocalInsert() {
Document doc = new Document(1);
doc.localInsert(0, "Hello");
assertEquals("Hello", doc.getContents());
doc.localInsert(5, " World");
assertEquals("Hello World", doc.getContents());
doc.localInsert(5, " Test");
assertEquals("Hello Test World", doc.getContents());
}
@Test
public void testLocalInsertInvalidIndexFails() {
Document doc = new Document(1);
doc.localInsert(0, "Hello");
assertThrows(IllegalArgumentException.class, () -> doc.localInsert(-1, "Test"));
assertThrows(IllegalArgumentException.class, () -> doc.localInsert(6, "Test"));
}
}