Skip to content
Snippets Groups Projects
Commit cadaee22 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

list comparator implemeted

parent 3830059d
No related branches found
No related tags found
2 merge requests!630WIP: Resolve "The privileges of a new user are not saved in some cases",!581Resolve "export to sbml layout creates partial lines for operators"
package lcsb.mapviewer.common.comparator;
import java.util.Comparator;
import java.util.List;
import org.apache.log4j.Logger;
/**
* Comparator used for comparing lists of objects.
*
* @author Piotr Gawron
*
*/
public class ListComparator<T> implements Comparator<List<T>> {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(ListComparator.class);
private Comparator<T> objectComparator;
public ListComparator(Comparator<T> objectComparator) {
this.objectComparator = objectComparator;
}
@Override
public int compare(List<T> arg0, List<T> arg1) {
if (arg0 == null) {
if (arg1 == null) {
return 0;
} else {
return 1;
}
} else if (arg1 == null) {
return -1;
}
if (arg0.size() != arg1.size()) {
return new IntegerComparator().compare(arg0.size(), arg1.size());
}
for (int i = 0; i < arg0.size(); i++) {
if (objectComparator.compare(arg0.get(i), arg1.get(i)) != 0) {
return objectComparator.compare(arg0.get(i), arg1.get(i));
}
}
return 0;
}
}
...@@ -10,6 +10,7 @@ import org.junit.runners.Suite.SuiteClasses; ...@@ -10,6 +10,7 @@ import org.junit.runners.Suite.SuiteClasses;
DoubleComparatorTest.class, DoubleComparatorTest.class,
EnumComparatorTest.class, EnumComparatorTest.class,
IntegerComparatorTest.class, IntegerComparatorTest.class,
ListComparatorTest.class,
PointComparatorTest.class, PointComparatorTest.class,
StringComparatorTest.class, StringComparatorTest.class,
StringListComparatorTest.class, StringListComparatorTest.class,
......
package lcsb.mapviewer.common.comparator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.junit.Test;
public class ListComparatorTest {
@Test
public void testEqual() {
List<Color> set1 = new ArrayList<>();
List<Color> set2 = new ArrayList<>();
set1.add(Color.BLACK);
set2.add(Color.BLACK);
ListComparator<Color> comparator = new ListComparator<>(new ColorComparator());
assertEquals(0, comparator.compare(set1, set2));
}
@Test
public void testDifferent() {
List<Color> set1 = new ArrayList<>();
List<Color> set2 = new ArrayList<>();
set1.add(Color.BLACK);
set2.add(Color.BLUE);
ListComparator<Color> comparator = new ListComparator<>(new ColorComparator());
assertTrue(comparator.compare(set1, set2) != 0);
}
@Test
public void testEqualWithCustomClass() {
class Data {
int x;
Data(int x) {
this.x = x;
}
}
class DataComparator implements Comparator<Data> {
@Override
public int compare(Data o1, Data o2) {
return o1.x - o2.x;
}
}
List<Data> set1 = new ArrayList<>();
List<Data> set2 = new ArrayList<>();
set1.add(new Data(1));
set2.add(new Data(1));
ListComparator<Data> comparator = new ListComparator<>(new DataComparator());
assertEquals(0, comparator.compare(set1, set2));
set2.add(new Data(2));
assertTrue(0 != comparator.compare(set1, set2));
assertTrue(0 != comparator.compare(set2, set1));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment