Skip to content

Commit

Permalink
Made the comparison of attribute lists order-independent.
Browse files Browse the repository at this point in the history
  • Loading branch information
csrster committed Feb 11, 2016
1 parent e3657dd commit 4b0c630
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -88,14 +90,23 @@ public List<AttributeTypeBase> getAttributeTypes(int tree_id) {
/**
* Handy class to pair an attribute and its type.
*/
public static class AttributeAndType {
public static class AttributeAndType implements Comparable<AttributeAndType> {
public AttributeTypeBase attributeType;
public AttributeBase attribute;
public AttributeAndType(AttributeTypeBase attributeType, AttributeBase attribute) {
this.attributeType = attributeType;
this.attribute = attribute;
}
}

/**
* Comparator allows a list to be sorted so attributes appear in a reproducible order.
* @param o
* @return
*/
@Override public int compareTo(AttributeAndType o) {
return attributeType.id - o.attributeType.id;
}
}

/**
* Returns a list of attributes and their type for a given entity id and tree id.
Expand Down Expand Up @@ -141,6 +152,8 @@ public List<AttributeAndType> getAttributesAndTypes(int tree_id, int entity_id)
* @return the result of comparing two lists containing attributes and their types
*/
public static int compare(List<AttributeAndType> antList1, List<AttributeAndType> antList2) {
Collections.sort(antList1);
Collections.sort(antList2);
int res;
AttributeAndType ant1;
AttributeAndType ant2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dk.netarkivet.harvester.datamodel.eav;

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

import org.junit.Assert;
Expand Down Expand Up @@ -37,6 +38,7 @@ public void testEAV() {

antList1.add(aat);


Assert.assertEquals(0, EAV.compare(antList1, antList2));
Assert.assertEquals(0, EAV.compare(antList2, antList1));

Expand All @@ -59,6 +61,7 @@ public void testEAV() {
aat.attribute.setInteger(1);
antList2.add(aat);


Assert.assertEquals(1, EAV.compare(antList1, antList2));
Assert.assertEquals(-1, EAV.compare(antList2, antList1));

Expand All @@ -81,7 +84,9 @@ public void testEAV() {
aat = new AttributeAndType(at, new ContentAttribute_Generic(at));
aat.attribute.setInteger(1);
antList1.add(aat);
antList2.add(aat);
//Insert the attributes in antList2 in a different order - should still be identical
antList2.add(0, aat);


Assert.assertEquals(0, EAV.compare(antList1, antList2));
Assert.assertEquals(0, EAV.compare(antList2, antList1));
Expand Down

0 comments on commit 4b0c630

Please sign in to comment.