GroupingCallBack implements MatchMergeAlgorithm.Callback { @Override public void onMatch(Record record1, Record record2, MatchResult matchResult) { RichRecord richRecord1 = (RichRecord) record1; RichRecord richRecord2 = (RichRecord) record2; richRecord1.setConfidence(richRecord1.getScore()); richRecord2.setConfidence(richRecord2.getScore()); String grpId1 = richRecord1.getGroupId(); String grpId2 = richRecord2.getGroupId(); if (grpId1 == null && grpId2 == null) { String gid = UUID.randomUUID().toString(); richRecord1.setGroupId(gid); richRecord2.setGroupId(gid); richRecord1.setGrpSize(0); richRecord2.setGrpSize(0); richRecord1.setMaster(false); richRecord2.setMaster(false); output(richRecord1); output(richRecord2); } else if (grpId1 != null && grpId2 != null) { richRecord2.setGroupId(grpId1); oldGID2New.put(grpId2, grpId1); List<String> keysOfGID2 = oldGID2New.getKeys(grpId2); if (keysOfGID2 != null) { for (String key : keysOfGID2) { oldGID2New.put(key, grpId1); } } } else if (grpId1 == null) { richRecord1.setGroupId(richRecord2.getGroupId()); richRecord1.setGrpSize(0); richRecord1.setMaster(false); output(richRecord1); } else { richRecord2.setGroupId(richRecord1.getGroupId()); richRecord2.setGrpSize(0); richRecord2.setMaster(false); output(richRecord2); } } GroupingCallBack(BidiMultiMap<String, String> oldGID2New, AbstractRecordGrouping<T> recordGrouping); @Override void onBeginRecord(Record record); @Override void onMatch(Record record1, Record record2, MatchResult matchResult); @Override void onNewMerge(Record record); @Override void onRemoveMerge(Record record); @Override void onDifferent(Record record1, Record record2, MatchResult matchResult); @Override void onEndRecord(Record record); @Override boolean isInterrupted(); @Override void onBeginProcessing(); @Override void onEndProcessing(); @Override void onSynResult(Record newRecord, Record originalRecord, MatchResult matchResult);  }
@Test public void testOnMatch() { GroupingCallBack<Object> groupingCallBack = new GroupingCallBack<>(oldGID2New, recordGrouping); RichRecord record1 = new RichRecord("id1", new Date().getTime(), "source1"); RichRecord record2 = new RichRecord("id2", new Date().getTime(), "source2"); record1.setScore(0.1); record2.setScore(0.2); MatchResult matchResult = new MatchResult(1); record1.setGroupId(null); record2.setGroupId(null); groupingCallBack.onMatch(record1, record2, matchResult); Assert.assertEquals("The size of listResult should be 2", 2, listResult.size()); Assert.assertEquals("The Confidence value of record1 should be 0.1", "0.1", "" + listResult.get(0).getConfidence()); Assert.assertNotNull("The groupid of record1 should not be null", listResult.get(0).getGroupId()); Assert.assertEquals("The group size of record1 should be 0", 0, listResult.get(0).getGrpSize()); Assert.assertFalse("The master of record1 should be false", listResult.get(0).isMaster()); Assert.assertEquals("\"The Confidence value of record2 should be 0.2", "0.2", "" + listResult.get(1).getConfidence()); Assert.assertNotNull("The groupid of record2 should not be null", listResult.get(1).getGroupId()); Assert.assertEquals("The group size of record2 should be 0", 0, listResult.get(1).getGrpSize()); Assert.assertFalse("The master of record2 should be false", listResult.get(1).isMaster()); listResult.clear(); record1.setGroupId("id1"); record2.setGroupId("id2"); oldGID2New.put("id3", "id2"); oldGID2New.put("id4", "id2"); oldGID2New.put("id5", "id2"); oldGID2New.put("id6", "id2"); groupingCallBack.onMatch(record1, record2, matchResult); Assert.assertEquals("The size of listResult should be 0", 0, listResult.size()); Assert.assertEquals("The Confidence value of record1 should be 0.1", "0.1", "" + record1.getConfidence()); Assert.assertEquals("The groupid of record1 should be id1", "id1", record1.getGroupId()); Assert.assertFalse("The master of record1 should be false", record1.isMaster()); Assert.assertEquals("The Confidence value of record2 should be 0.2", "0.2", "" + record2.getConfidence()); Assert.assertEquals("The groupid of record2 should be id1", "id1", record2.getGroupId()); Assert.assertFalse("The master of record2 should be false", record2.isMaster()); Assert.assertEquals("The size of oldGID2New should be 5", 5, oldGID2New.size()); Assert.assertEquals("The size of mapping to id1 should be 5", 5, oldGID2New.getKeys("id1").size()); listResult.clear(); oldGID2New.clear(); record1.setGroupId(null); record1.setGrpSize(1); record1.setMaster(true); record2.setGroupId("id2"); groupingCallBack.onMatch(record1, record2, matchResult); Assert.assertEquals("The size of listResult should be 1", 1, listResult.size()); Assert.assertEquals("The Confidence value of record1 should be 0.1", "0.1", "" + listResult.get(0).getConfidence()); Assert.assertEquals("The groupid of record1 should be id2", "id2", listResult.get(0).getGroupId()); Assert.assertEquals("The group size of record1 should be 0", 0, listResult.get(0).getGrpSize()); Assert.assertFalse("The master of record1 should be false", listResult.get(0).isMaster()); listResult.clear(); oldGID2New.clear(); record1.setGroupId("id1"); record2.setGroupId(null); record2.setGrpSize(1); record2.setMaster(true); groupingCallBack.onMatch(record1, record2, matchResult); Assert.assertEquals("The size of listResult should be 1", 1, listResult.size()); Assert.assertEquals("The Confidence value of record1 should be 0.2", "0.2", "" + listResult.get(0).getConfidence()); Assert.assertEquals("The groupid of record2 should be id1", "id1", listResult.get(0).getGroupId()); Assert.assertEquals("The group size of record2 should be 0", 0, listResult.get(0).getGrpSize()); Assert.assertFalse("The master of record2 should be false", listResult.get(0).isMaster()); }