Skip to content

Commit

Permalink
Add List comparison classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpace committed Sep 5, 2017
1 parent 946ec96 commit b099ac9
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/main/java/org/incava/diffj/util/ListComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.incava.diffj.util;

import java.util.ArrayList;
import java.util.List;

public class ListComparator<Type> {
private final List<Type> from;
private final List<Type> to;

public ListComparator(List<Type> from, List<Type> to) {
this.from = new ArrayList<Type>(from);
this.to = new ArrayList<Type>(to);
}

public ListComparison getComparison() {
ListComparison comp = new ListComparison();
for (int idx = 0; idx < from.size(); ++idx) {
Integer paramMatch = getListMatch(idx);
if (paramMatch == null) {
continue;
}
else if (paramMatch == idx) {
comp.addExactMatch(idx);
}
else {
comp.addMisorderedMatch(idx, paramMatch);
}
}
return comp;
}

public void clearMatchList(int fromIndex, int toIndex) {
from.set(fromIndex, null);
to.set(toIndex, null);
}

public Integer getListMatch(int fromIndex) {
int fromSize = from.size();
Type fromVal = fromIndex < fromSize ? from.get(fromIndex) : null;

if (fromVal == null) {
return null;
}

int toSize = to.size();
Type toVal = fromIndex < toSize ? to.get(fromIndex) : null;

if (fromVal.equals(toVal)) {
clearMatchList(fromIndex, fromIndex);
return fromIndex;
}

for (int toIdx = 0; toIdx < toSize; ++toIdx) {
toVal = to.get(toIdx);
if (fromVal.equals(toVal)) {
clearMatchList(fromIndex, toIdx);
return toIdx;
}
}
return null;
}
}
32 changes: 32 additions & 0 deletions src/main/java/org/incava/diffj/util/ListComparison.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.incava.diffj.util;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

public class ListComparison {
private final List<Integer> exactMatches;
private final Map<Integer, Integer> misorderedMatches;

public ListComparison() {
exactMatches = new ArrayList<Integer>();
misorderedMatches = new HashMap<Integer, Integer>();
}

public void addExactMatch(Integer idx) {
exactMatches.add(idx);
}

public void addMisorderedMatch(Integer fromIdx, Integer toIdx) {
misorderedMatches.put(fromIdx, toIdx);
}

public List<Integer> getExactMatches() {
return exactMatches;
}

public Map<Integer, Integer> getMisorderedMatches() {
return misorderedMatches;
}
}
52 changes: 52 additions & 0 deletions src/test/java/org/incava/diffj/util/ListComparatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.incava.diffj.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junitparams.Parameters;
import junitparams.naming.TestCaseName;
import org.incava.ijdk.util.IUtil;
import org.incava.attest.Parameterized;
import org.junit.Test;

import static org.incava.attest.Assertions.assertEqual;
import static org.incava.attest.Assertions.message;

public class ListComparatorTest extends Parameterized {
public Map<Integer, Integer> map(Integer ... vals) {
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
Integer k = null;
for (Integer val : vals) {
if (k == null) {
k = val;
}
else {
m.put(k, val);
k = null;
}
}
return m;
}

@Test
@Parameters
@TestCaseName("{method} {index} {params}")
public <T> void test(List<Integer> expExactMatches, Map<Integer, Integer> expMisorderedMatches, List<T> from, List<T> to) {
ListComparator<T> lc = new ListComparator<T>(from, to);
ListComparison comp = lc.getComparison();
assertEqual(expExactMatches, comp.getExactMatches(), message("from", from, "to", to));
assertEqual(expMisorderedMatches, comp.getMisorderedMatches(), message("from", from, "to", to));
}

private List<Object[]> parametersForTest() {
return paramsList(params(IUtil.<Integer>list(), map(), IUtil.<String>list(), IUtil.<String>list()),
params(IUtil.list(0), map(), IUtil.list("x"), IUtil.list("x")),
params(IUtil.list(0, 1), map(), IUtil.list("x", "y"), IUtil.list("x", "y")),
params(IUtil.list(0, 1), map(), IUtil.list("x", "y", "z"), IUtil.list("x", "y", "a")),
params(IUtil.<Integer>list(), map(0, 1, 1, 0), IUtil.list("x", "y"), IUtil.list("y", "x")),
params(IUtil.<Integer>list(), map(0, 1, 1, 2, 2, 0), IUtil.list("x", "y", "z"), IUtil.list("z", "x", "y")),
params(IUtil.<Integer>list(), map(0, 1, 1, 2, 2, 0), IUtil.list("x", "y", "z"), IUtil.list("z", "x", "y", "a")));
}
}

0 comments on commit b099ac9

Please sign in to comment.