Establish buildable modules and shared host-service boundaries
This commit is contained in:
+14
@@ -0,0 +1,14 @@
|
||||
package de.neitzel.fxdesigner.contracts;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Immutable host-owned draft; revision ordering is independent of native undo stamps. */
|
||||
public record DocumentSnapshot(
|
||||
URI document, long revision, String text, boolean writable, boolean dirty) {
|
||||
public DocumentSnapshot {
|
||||
Objects.requireNonNull(document);
|
||||
Objects.requireNonNull(text);
|
||||
if (revision < 0) throw new IllegalArgumentException("Negative revision");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package de.neitzel.fxdesigner.contracts;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/** One named undo transaction. All UTF-16 ranges address the same expected revision. */
|
||||
public record EditBatch(long expectedRevision, String name, List<Replacement> replacements) {
|
||||
public record Replacement(int start, int end, String text) {
|
||||
public Replacement {
|
||||
if (start < 0 || end < start) throw new IllegalArgumentException("Invalid range");
|
||||
Objects.requireNonNull(text);
|
||||
}
|
||||
}
|
||||
|
||||
public EditBatch {
|
||||
if (expectedRevision < 0) throw new IllegalArgumentException("Negative revision");
|
||||
if (name == null || name.isBlank()) throw new IllegalArgumentException("Missing command name");
|
||||
replacements = List.copyOf(replacements);
|
||||
}
|
||||
|
||||
/** Validates every range before computing a result; never partially edits a host draft. */
|
||||
public String applyTo(DocumentSnapshot draft) {
|
||||
if (!draft.writable()) throw new IllegalStateException("Document is read-only");
|
||||
if (draft.revision() != expectedRevision)
|
||||
throw new ConcurrentModificationException("Stale document revision");
|
||||
var ordered = new ArrayList<>(replacements);
|
||||
ordered.sort(Comparator.comparingInt(Replacement::start));
|
||||
int previousEnd = -1;
|
||||
for (var r : ordered) {
|
||||
if (r.end() > draft.text().length() || r.start() < previousEnd)
|
||||
throw new IllegalArgumentException("Overlapping/out-of-bounds edits");
|
||||
previousEnd = r.end();
|
||||
}
|
||||
var result = new StringBuilder(draft.text());
|
||||
for (int i = ordered.size() - 1; i >= 0; i--) {
|
||||
var r = ordered.get(i);
|
||||
result.replace(r.start(), r.end(), r.text());
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user