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

comparator of creation/modification dates added to the model comparator

parent 50d97e1f
No related branches found
No related tags found
1 merge request!717Resolve "process vcard data that can appear in model annotations"
package lcsb.mapviewer.common.comparator;
import java.util.Calendar;
import java.util.Comparator;
/**
* Comparator implementation for {@link Calendar} class.
*
* @author Piotr Gawron
*
*/
public class CalendarComparator implements Comparator<Calendar> {
@Override
public int compare(Calendar arg0, Calendar arg1) {
if (arg0 == null) {
if (arg1 == null) {
return 0;
} else {
return 1;
}
} else if (arg1 == null) {
return -1;
}
return ((Long) arg0.getTimeInMillis()).compareTo(arg1.getTimeInMillis());
}
}
package lcsb.mapviewer.model.map.model;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
......@@ -9,6 +10,7 @@ import org.apache.log4j.Logger;
import lcsb.mapviewer.common.Comparator;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.comparator.CalendarComparator;
import lcsb.mapviewer.common.comparator.DoubleComparator;
import lcsb.mapviewer.common.comparator.IntegerComparator;
import lcsb.mapviewer.common.comparator.ListComparator;
......@@ -170,6 +172,22 @@ public class ModelComparator extends Comparator<Model> {
return status;
}
CalendarComparator calendarComparator = new CalendarComparator();
status = calendarComparator.compare(arg0.getCreationDate(), arg1.getCreationDate());
if (status != 0) {
logger.debug("creation date different");
return status;
}
ListComparator<Calendar> calendarListComparator = new ListComparator<>(calendarComparator);
status = calendarListComparator.compare(arg0.getModificationDates(), arg1.getModificationDates());
if (status != 0) {
logger.debug("modification dates different");
return status;
}
return 0;
}
......
......@@ -5,6 +5,8 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
import java.util.Calendar;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
......@@ -449,4 +451,38 @@ public class ModelComparatorTest {
}
}
@Test
public void testCompareCreationDate() throws Exception {
try {
Model model1 = getModel();
Model model2 = getModel();
model1.setCreationDate(Calendar.getInstance());
assertTrue("Models have different creation dates", comparator.compare(model1, model2) != 0);
assertTrue(comparator.compare(model2, model1) != 0);
model2.setCreationDate(model1.getCreationDate());
assertEquals(0, comparator.compare(model1, model2));
assertEquals(0, comparator.compare(model2, model1));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testCompareModificationDate() throws Exception {
try {
Model model1 = getModel();
Model model2 = getModel();
model1.addModificationDate(Calendar.getInstance());
assertTrue("Models have different modification dates", comparator.compare(model1, model2) != 0);
assertTrue(comparator.compare(model2, model1) != 0);
model2.addModificationDate(model1.getModificationDates().get(0));
assertEquals(0, comparator.compare(model1, model2));
assertEquals(0, comparator.compare(model2, model1));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
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