target) {
+ return this.mapSegments(
+ s -> s.mapWithPosition(source::get),
+ t -> t.mapWithPosition(target::get)
+ );
+ }
+
/**
* Merges this edit with the supplied other, creating a new edit.
*
diff --git a/src/main/java/io/languagetoys/aligner/edit/EditShift.java b/src/main/java/io/languagetoys/aligner/edit/EditShift.java
new file mode 100644
index 0000000..8acb172
--- /dev/null
+++ b/src/main/java/io/languagetoys/aligner/edit/EditShift.java
@@ -0,0 +1,50 @@
+package io.languagetoys.aligner.edit;
+
+import java.util.function.Function;
+
+public class EditShift {
+
+ private final EditVisitor.Builder visitor;
+ private final Edit edit;
+
+ EditShift(Edit edit, Function, R> initial) {
+ this.edit = edit;
+ EditVisitor.Builder builder = EditVisitor.builder();
+ this.visitor = builder
+ .onDelete(initial)
+ .onSubstitute(initial)
+ .onEqual(initial)
+ .onInsert(initial)
+ .onTranspose(initial);
+ }
+
+ public EditShift equal(Function, R> visitor) {
+ this.visitor.onEqual(visitor);
+ return this;
+ }
+
+ public EditShift insert(Function, R> visitor) {
+ this.visitor.onInsert(visitor);
+ return this;
+ }
+
+ public EditShift delete(Function, R> visitor) {
+ this.visitor.onDelete(visitor);
+ return this;
+ }
+
+ public EditShift substitute(Function, R> visitor) {
+ this.visitor.onSubstitute(visitor);
+ return this;
+ }
+
+ public EditShift transpose(Function, R> visitor) {
+ this.visitor.onTranspose(visitor);
+ return this;
+ }
+
+ public R get() {
+ return edit.accept(visitor.build());
+ }
+
+}
diff --git a/src/main/java/io/languagetoys/aligner/edit/Segment.java b/src/main/java/io/languagetoys/aligner/edit/Segment.java
index ebc1526..281f129 100644
--- a/src/main/java/io/languagetoys/aligner/edit/Segment.java
+++ b/src/main/java/io/languagetoys/aligner/edit/Segment.java
@@ -80,7 +80,7 @@ public final Segment map(Function super T, ? extends R> teFunction) {
);
}
- public final Segment mapWithIndex(Function mapper) {
+ public final Segment mapWithPosition(Function mapper) {
List collect = IntStream
.range(position, position + size())
.mapToObj(mapper::apply)
@@ -106,7 +106,7 @@ public final IntStream indices() {
*/
@Deprecated
public final Segment select(List items) {
- return mapWithIndex(items::get);
+ return mapWithPosition(items::get);
}
/**
diff --git a/src/test/java/io/languagetoys/aligner/edit/EditTest.java b/src/test/java/io/languagetoys/aligner/edit/EditTest.java
index 805b882..6814cf5 100644
--- a/src/test/java/io/languagetoys/aligner/edit/EditTest.java
+++ b/src/test/java/io/languagetoys/aligner/edit/EditTest.java
@@ -3,6 +3,8 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import java.util.List;
+
public class EditTest {
@Test
@@ -24,4 +26,41 @@ void mergeIsSymmetrical() {
Assertions.assertEquals(expected, left.mergeWith(right));
Assertions.assertEquals(expected, right.mergeWith(left));
}
+
+ @Test
+ void testProject() {
+ List source = List.of(1, 2, 3);
+ List target = List.of(4, 5, 6);
+
+ Edit edit = Edit.builder()
+ .substitute(2, 3)
+ .with(5)
+ .atPosition(1, 1);
+
+ Edit expected = Edit.builder()
+ .substitute("2", "3")
+ .with("5")
+ .atPosition(1, 1);
+
+ Edit actual = edit.project(List.of("1", "2", "3"), List.of("4", "5", "6"));
+
+ Assertions.assertEquals(expected, actual);
+ }
+
+ @Test
+ void testShift() {
+
+// Edit edit = Edit.builder()
+// .substitute(2, 3)
+// .with(5)
+// .atPosition(1, 1);
+//
+// String name = edit
+// .shift(e -> "")
+// .substitute(e -> "substitute")
+// .delete(e -> "delete")
+// .get();
+//
+// Assertions.assertEquals("substitute", name);
+ }
}