Skip to content

Commit

Permalink
0.2.0 (#11)
Browse files Browse the repository at this point in the history
* add project and shift methods to Edit - initial dev, tests pass

* bump to 0.2.0

* project method
  • Loading branch information
manzurola authored Jul 11, 2021
1 parent f914da6 commit 46bc4f7
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.languagetoys</groupId>
<artifactId>aligner</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/languagetoys/aligner/edit/Edit.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ public Optional<? extends Edit<T>> filter(Predicate<? super Edit<T>> 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:
* <pre>
* {@code return this.mapSegments(
* s -> s.mapWithIndex(source::get),
* t -> t.mapWithIndex(target::get));
* }
* </pre>
*
*/
public final <R> Edit<R> project(List<R> source, List<R> 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.
* <p>
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/io/languagetoys/aligner/edit/EditShift.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.languagetoys.aligner.edit;

import java.util.function.Function;

public class EditShift<T, R> {

private final EditVisitor.Builder<T, R> visitor;
private final Edit<T> edit;

EditShift(Edit<T> edit, Function<Edit<T>, R> initial) {
this.edit = edit;
EditVisitor.Builder<T, R> builder = EditVisitor.builder();
this.visitor = builder
.onDelete(initial)
.onSubstitute(initial)
.onEqual(initial)
.onInsert(initial)
.onTranspose(initial);
}

public EditShift<T, R> equal(Function<Edit<T>, R> visitor) {
this.visitor.onEqual(visitor);
return this;
}

public EditShift<T, R> insert(Function<Edit<T>, R> visitor) {
this.visitor.onInsert(visitor);
return this;
}

public EditShift<T, R> delete(Function<Edit<T>, R> visitor) {
this.visitor.onDelete(visitor);
return this;
}

public EditShift<T, R> substitute(Function<Edit<T>, R> visitor) {
this.visitor.onSubstitute(visitor);
return this;
}

public EditShift<T, R> transpose(Function<Edit<T>, R> visitor) {
this.visitor.onTranspose(visitor);
return this;
}

public R get() {
return edit.accept(visitor.build());
}

}
4 changes: 2 additions & 2 deletions src/main/java/io/languagetoys/aligner/edit/Segment.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public final <R> Segment<R> map(Function<? super T, ? extends R> teFunction) {
);
}

public final <E> Segment<E> mapWithIndex(Function<Integer, E> mapper) {
public final <E> Segment<E> mapWithPosition(Function<Integer, E> mapper) {
List<E> collect = IntStream
.range(position, position + size())
.mapToObj(mapper::apply)
Expand All @@ -106,7 +106,7 @@ public final IntStream indices() {
*/
@Deprecated
public final <E> Segment<E> select(List<E> items) {
return mapWithIndex(items::get);
return mapWithPosition(items::get);
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/io/languagetoys/aligner/edit/EditTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;

public class EditTest {

@Test
Expand All @@ -24,4 +26,41 @@ void mergeIsSymmetrical() {
Assertions.assertEquals(expected, left.mergeWith(right));
Assertions.assertEquals(expected, right.mergeWith(left));
}

@Test
void testProject() {
List<Integer> source = List.of(1, 2, 3);
List<Integer> target = List.of(4, 5, 6);

Edit<Integer> edit = Edit.builder()
.substitute(2, 3)
.with(5)
.atPosition(1, 1);

Edit<String> expected = Edit.builder()
.substitute("2", "3")
.with("5")
.atPosition(1, 1);

Edit<String> actual = edit.project(List.of("1", "2", "3"), List.of("4", "5", "6"));

Assertions.assertEquals(expected, actual);
}

@Test
void testShift() {

// Edit<Integer> 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);
}
}

0 comments on commit 46bc4f7

Please # to comment.