Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ public interface CommandVisitor<T> {
*/
void visitDeleteCommand(T object);

/**
* Method called once all commands of the script have been visited.
* <p>
* A visitor that groups consecutive commands must override this method to process the group that is still pending
* when the script ends. The default implementation does nothing.
* </p>
*
* @since 4.6.1
*/
default void visitEndOfScript() {
// empty
}

/**
* Method called when an insert command is encountered.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public void visit(final CommandVisitor<T> visitor) {
for (final EditCommand<T> command : commands) {
command.accept(visitor);
}
visitor.visitEndOfScript();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ public ReplacementsFinder(final ReplacementsHandler<T> handler) {
this.handler = handler;
}

/**
* Hands the pending insertions and deletions to the user handler as subsequences.
*/
private void handlePendingReplacement() {
handler.handleReplacement(skipped, pendingDeletions, pendingInsertions);
pendingDeletions.clear();
pendingInsertions.clear();
}

/**
* Add an object to the pending deletions set.
*
Expand All @@ -80,6 +89,18 @@ public void visitDeleteCommand(final T object) {
pendingDeletions.add(object);
}

/**
* Handle the trailing replacement, if the script ended on insertions or deletions rather than on a
* synchronization object.
*/
@Override
public void visitEndOfScript() {
if (!pendingDeletions.isEmpty() || !pendingInsertions.isEmpty()) {
handlePendingReplacement();
skipped = 0;
}
}

/**
* Add an object to the pending insertions set.
*
Expand All @@ -103,9 +124,7 @@ public void visitKeepCommand(final T object) {
if (pendingDeletions.isEmpty() && pendingInsertions.isEmpty()) {
++skipped;
} else {
handler.handleReplacement(skipped, pendingDeletions, pendingInsertions);
pendingDeletions.clear();
pendingInsertions.clear();
handlePendingReplacement();
skipped = 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4.sequence;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class ReplacementsFinderTest {

static Stream<Arguments> sequences() {
return Stream.of(
Arguments.of("abcd", "abcd", ""),
Arguments.of("xabc", "yabc", "0:[x]->[y]"),
Arguments.of("abxcd", "abycd", "2:[x]->[y]"),
Arguments.of("axbxc", "aybyc", "1:[x]->[y] 1:[x]->[y]"),
Arguments.of("abc", "abd", "2:[c]->[d]"),
Arguments.of("abcd", "abc", "3:[d]->[]"),
Arguments.of("abc", "abcd", "3:[]->[d]"),
Arguments.of("abc", "", "0:[a, b, c]->[]"),
Arguments.of("", "xyz", "0:[]->[x, y, z]"),
Arguments.of("a", "b", "0:[a]->[b]"));
}

private static List<Character> sequence(final String string) {
final List<Character> list = new ArrayList<>(string.length());
for (int i = 0; i < string.length(); i++) {
list.add(Character.valueOf(string.charAt(i)));
}
return list;
}

/**
* The replacements reported for a whole script must transform the first sequence into the second one.
*/
@ParameterizedTest
@MethodSource("sequences")
void testReplacementsSpanWholeScript(final String left, final String right, final String expected) {
final List<Character> first = sequence(left);
final List<String> reported = new ArrayList<>();
final List<Character> rebuilt = new ArrayList<>();
final int[] cursor = {0};
new SequencesComparator<>(first, sequence(right)).getScript().visit(new ReplacementsFinder<Character>(
(skipped, from, to) -> {
reported.add(skipped + ":" + from + "->" + to);
rebuilt.addAll(first.subList(cursor[0], cursor[0] + skipped));
cursor[0] += skipped + from.size();
rebuilt.addAll(to);
}));
rebuilt.addAll(first.subList(cursor[0], first.size()));
assertEquals(expected, String.join(" ", reported));
assertEquals(sequence(right), rebuilt);
}

}