From 46bc4f7da1e0440bb3ff450b4888ec986cd72c3d Mon Sep 17 00:00:00 2001 From: Guy Manzurola Date: Sun, 11 Jul 2021 21:49:36 +0300 Subject: [PATCH] 0.2.0 (#11) * add project and shift methods to Edit - initial dev, tests pass * bump to 0.2.0 * project method --- pom.xml | 2 +- .../io/languagetoys/aligner/edit/Edit.java | 18 +++++++ .../languagetoys/aligner/edit/EditShift.java | 50 +++++++++++++++++++ .../io/languagetoys/aligner/edit/Segment.java | 4 +- .../languagetoys/aligner/edit/EditTest.java | 39 +++++++++++++++ 5 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 src/main/java/io/languagetoys/aligner/edit/EditShift.java diff --git a/pom.xml b/pom.xml index e327391..880a75d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.languagetoys aligner - 0.1.0 + 0.2.0 UTF-8 diff --git a/src/main/java/io/languagetoys/aligner/edit/Edit.java b/src/main/java/io/languagetoys/aligner/edit/Edit.java index 7b26d51..6057123 100644 --- a/src/main/java/io/languagetoys/aligner/edit/Edit.java +++ b/src/main/java/io/languagetoys/aligner/edit/Edit.java @@ -97,6 +97,24 @@ public Optional> filter(Predicate> predicate) return Optional.of(this).filter(predicate); } + /** + * Create a new Edit R with the elements from source and target based on the index positions of the segments of this + * Edit. Equal to: + *
+     * {@code return this.mapSegments(
+     *                      s -> s.mapWithIndex(source::get),
+     *                      t -> t.mapWithIndex(target::get));
+     * }
+     * 
+ * + */ + public final Edit project(List source, List 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 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); + } }