-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
src/test/java/org/incava/diffj/util/ListComparatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
} |