EAV { public static int compare(List<AttributeAndType> antList1, List<AttributeAndType> antList2) { if (antList1 == null && antList2 == null) { return 0; } if (antList1 == null) { antList1 = new ArrayList<>(); } if (antList2 == null) { antList2 = new ArrayList<>(); } if (antList1.isEmpty() && antList2.isEmpty()) { return 0; } int size1 = antList1.size(); int size2 = antList2.size(); if (!antList1.isEmpty() && !antList2.isEmpty() && size1 != size2) { throw new UnsupportedOperationException("Haven't though about how to compare attribute lists of different lengths"); } if (antList1.isEmpty() || antList2.isEmpty()) { boolean antList2Empty = antList2.isEmpty(); List<AttributeAndType> antList3 = new ArrayList<AttributeAndType>(); antList3.addAll(antList1); antList3.addAll(antList2); for (AttributeAndType attributeAndType: antList3) { Integer defaultValue = attributeAndType.attributeType.def_int; Integer value = null; if (defaultValue == null) { defaultValue = 0; } if (attributeAndType.attribute != null) { value = attributeAndType.attribute.getInteger(); } if (value == null) { value = defaultValue; } int res = value -defaultValue; if (res != 0L) { int result = res < 0L ? -1 : 1; if (antList2Empty) { result = -result; } return result; } } return 0; } Collections.sort(antList1); Collections.sort(antList2); for (int i = 0; i < size1; i++) { AttributeAndType ant1 = antList1.get(i); AttributeAndType ant2 = antList2.get(i); if (ant1.attributeType.id != ant2.attributeType.id) { return (ant1.attributeType.id - ant2.attributeType.id) < 0 ? -1 : 1; } if (ant1.attributeType.datatype != 1) { throw new UnsupportedOperationException("EAV attribute datatype compare not implemented yet."); } Integer i1 = null; Integer i2 = null; if (ant1.attribute != null) { i1 = ant1.attribute.getInteger(); } if (i1 == null) { i1 = ant1.attributeType.def_int; } if (i1 == null) { i1 = 0; } if (ant2.attribute != null) { i2 = ant2.attribute.getInteger(); } if (i2 == null) { i2 = ant2.attributeType.def_int; } if (i2 == null) { i2 = 0; } int res = i2 - i1; if (res != 0L) { return res < 0L ? -1 : 1; } } return 0; }  static synchronized EAV getInstance(); void insertAttribute(AttributeBase attribute); void saveAttribute(AttributeBase attribute); List<AttributeTypeBase> getAttributeTypes(int tree_id); List<AttributeAndType> getAttributesAndTypes(int tree_id, int entity_id); static int compare(List<AttributeAndType> antList1, List<AttributeAndType> antList2); static Set<String> getAttributeNames(int tree_id); static final int SNAPSHOT_TREE_ID; static final int DOMAIN_TREE_ID; }
@Test public void testEAV() { AttributeAndType aat; AttributeTypeBase at; List<AttributeAndType> antList1 = new ArrayList<AttributeAndType>(); List<AttributeAndType> antList2 = new ArrayList<AttributeAndType>(); Assert.assertEquals(0, EAV.compare(antList1, antList2)); at = new ContentAttrType_Generic(); at.tree_id = 1; at.id = 1; at.datatype = 1; aat = new AttributeAndType(at, new ContentAttribute_Generic(at)); aat.attribute.setInteger(1); antList2.add(aat); Assert.assertEquals(1, EAV.compare(antList1, antList2)); Assert.assertEquals(-1, EAV.compare(antList2, antList1)); antList1.add(aat); Assert.assertEquals(0, EAV.compare(antList1, antList2)); Assert.assertEquals(0, EAV.compare(antList2, antList1)); antList1.clear(); antList2.clear(); at = new ContentAttrType_Generic(); at.tree_id = 1; at.id = 1; at.datatype = 1; aat = new AttributeAndType(at, new ContentAttribute_Generic(at)); aat.attribute.setInteger(1); antList1.add(aat); at = new ContentAttrType_Generic(); at.tree_id = 1; at.id = 2; at.datatype = 1; aat = new AttributeAndType(at, new ContentAttribute_Generic(at)); aat.attribute.setInteger(1); antList2.add(aat); Assert.assertNotEquals(0, EAV.compare(antList1, antList2)); Assert.assertEquals(-1*EAV.compare(antList1, antList2), EAV.compare(antList2, antList1)); antList1.clear(); antList2.clear(); at = new ContentAttrType_Generic(); at.tree_id = 1; at.id = 1; at.datatype = 1; aat = new AttributeAndType(at, new ContentAttribute_Generic(at)); aat.attribute.setInteger(1); antList1.add(aat); antList2.add(aat); at = new ContentAttrType_Generic(); at.tree_id = 1; at.id = 2; at.datatype = 1; aat = new AttributeAndType(at, new ContentAttribute_Generic(at)); aat.attribute.setInteger(1); antList1.add(aat); antList2.add(0, aat); Assert.assertEquals(0, EAV.compare(antList1, antList2)); Assert.assertEquals(0, EAV.compare(antList2, antList1)); antList1.clear(); antList2.clear(); at = new ContentAttrType_Generic(); at.tree_id = 1; at.id = 1; at.datatype = 1; aat = new AttributeAndType(at, new ContentAttribute_Generic(at)); aat.attribute.setInteger(1); antList1.add(aat); at = new ContentAttrType_Generic(); at.tree_id = 1; at.id = 1; at.datatype = 1; aat = new AttributeAndType(at, new ContentAttribute_Generic(at)); aat.attribute.setInteger(2); antList2.add(aat); Assert.assertEquals(1, EAV.compare(antList1, antList2)); Assert.assertEquals(-1, EAV.compare(antList2, antList1)); }