MFBRecordMerger implements IRecordMerger { protected String createMergeValue(String leftSource, String rightSource, String parameter, long leftTimeStamp, long rightTimeStamp, SurvivorShipAlgorithmEnum survivorShipAlgorithmEnum, String leftValue, String rightValue, String mergedValue, AttributeValues<String> mergedValues) { BigDecimal leftNumberValue; BigDecimal rightNumberValue; if (leftValue == null) { return rightValue; } else if (rightValue == null) { return leftValue; } long leftValueLength = leftValue.codePoints().count(); long rightValueLength = rightValue.codePoints().count(); switch (survivorShipAlgorithmEnum) { case CONCATENATE: if (StringUtils.isEmpty(parameter)) { return leftValue + rightValue; } else { return leftValue + parameter + rightValue; } case LARGEST: leftNumberValue = parseNumberValue(leftValue); rightNumberValue = parseNumberValue(rightValue); if (leftNumberValue == null) { return rightValue; } if (rightNumberValue == null) { return leftValue; } if (leftNumberValue.compareTo(rightNumberValue) >= 0) { return leftValue; } else { return rightValue; } case SMALLEST: leftNumberValue = parseNumberValue(leftValue); rightNumberValue = parseNumberValue(rightValue); if (leftNumberValue == null) { return rightValue; } if (rightNumberValue == null) { return leftValue; } if (leftNumberValue.compareTo(rightNumberValue) <= 0) { return leftValue; } else { return rightValue; } case MOST_RECENT: if (leftTimeStamp >= rightTimeStamp) { return leftValue; } else { return rightValue; } case MOST_ANCIENT: if (leftTimeStamp <= rightTimeStamp) { return leftValue; } else { return rightValue; } case PREFER_TRUE: if (Boolean.parseBoolean(leftValue) || Boolean.parseBoolean(rightValue)) { return "true"; } else { return "false"; } case PREFER_FALSE: if (Boolean.parseBoolean(leftValue) && Boolean.parseBoolean(rightValue)) { return "true"; } else { return "false"; } case MOST_COMMON: return mergedValues.mostCommon(); case LONGEST: if (leftValueLength >= rightValueLength) { return leftValue; } else { return rightValue; } case SHORTEST: if (leftValueLength <= rightValueLength) { return leftValue; } else { return rightValue; } case MOST_TRUSTED_SOURCE: String mostTrustedSourceName = parameter; if (mostTrustedSourceName == null) { throw new IllegalStateException("Survivorship 'most trusted source' must specify a trusted source."); } if (mostTrustedSourceName.equals(rightSource)) { return rightValue; } else { return leftValue; } } return null; } MFBRecordMerger(String mergedRecordSource, String[] parameters, SurvivorShipAlgorithmEnum[] typeMergeTable); @Override Record merge(Record record1, Record record2); void setColumnDatePatternMap(Map<String, String> columnMap);  }
@Test public void testCreateMergeValue_CaseMostRecient() { MFBRecordMerger mFBRecordMerger = new MFBRecordMerger("", new String[0], new SurvivorShipAlgorithmEnum[] { SurvivorShipAlgorithmEnum.MOST_RECENT }); String left = "05-05-2005"; String right = "06-07-2019"; String mergeValue = mFBRecordMerger.createMergeValue("MFB", "MFB", null, 1, 2, SurvivorShipAlgorithmEnum.MOST_RECENT, left, right, null, null); assertEquals(right, mergeValue); left = "06-07-2019"; right = "05-05-2005"; mergeValue = mFBRecordMerger.createMergeValue("MFB", "MFB", null, 2, 1, SurvivorShipAlgorithmEnum.MOST_RECENT, left, right, null, null); assertEquals(left, mergeValue); }
@Test public void testCreateMergeValue_CaseMostAncient() { MFBRecordMerger mFBRecordMerger = new MFBRecordMerger("", new String[0], new SurvivorShipAlgorithmEnum[] { SurvivorShipAlgorithmEnum.MOST_RECENT }); String left = "05-05-2005"; String right = "06-07-2019"; String mergeValue = mFBRecordMerger.createMergeValue("MFB", "MFB", null, 1, 2, SurvivorShipAlgorithmEnum.MOST_ANCIENT, left, right, null, null); assertEquals(left, mergeValue); left = "06-07-2019"; right = "05-05-2005"; mergeValue = mFBRecordMerger.createMergeValue("MFB", "MFB", null, 2, 1, SurvivorShipAlgorithmEnum.MOST_ANCIENT, left, right, null, null); assertEquals(right, mergeValue); }
@Test public void testCreateMergeValue_CasePreferTrue() { MFBRecordMerger mFBRecordMerger = new MFBRecordMerger("", new String[0], new SurvivorShipAlgorithmEnum[] { SurvivorShipAlgorithmEnum.MOST_RECENT }); String left = ""; String right = "true"; String mergeValue = mFBRecordMerger.createMergeValue("MFB", "MFB", null, 1, 2, SurvivorShipAlgorithmEnum.PREFER_TRUE, left, right, null, null); assertEquals("true", mergeValue); left = "false"; right = "false"; mergeValue = mFBRecordMerger.createMergeValue("MFB", "MFB", null, 2, 1, SurvivorShipAlgorithmEnum.PREFER_TRUE, left, right, null, null); assertEquals("false", mergeValue); }
@Test public void testCreateMergeValue_CasePreferFasle() { MFBRecordMerger mFBRecordMerger = new MFBRecordMerger("", new String[0], new SurvivorShipAlgorithmEnum[] { SurvivorShipAlgorithmEnum.MOST_RECENT }); String left = "true"; String right = "true"; String mergeValue = mFBRecordMerger.createMergeValue("MFB", "MFB", null, 1, 2, SurvivorShipAlgorithmEnum.PREFER_FALSE, left, right, null, null); assertEquals("true", mergeValue); left = "true"; right = "false"; mergeValue = mFBRecordMerger.createMergeValue("MFB", "MFB", null, 2, 1, SurvivorShipAlgorithmEnum.PREFER_FALSE, left, right, null, null); assertEquals("false", mergeValue); }
@Test public void testCreateMergeValue_CaseMostTrustSource() { MFBRecordMerger mFBRecordMerger = new MFBRecordMerger("", new String[0], new SurvivorShipAlgorithmEnum[] { SurvivorShipAlgorithmEnum.MOST_RECENT }); String left = "false"; String right = "true"; String mergeValue = null; try { mFBRecordMerger.createMergeValue("MFB", "MFB", null, 1, 2, SurvivorShipAlgorithmEnum.MOST_TRUSTED_SOURCE, left, right, null, null); } catch (IllegalStateException e) { Assert.assertEquals("Survivorship 'most trusted source' must specify a trusted source.", e.getMessage()); } mergeValue = mFBRecordMerger.createMergeValue("MFB", "MFB", "MFB", 2, 1, SurvivorShipAlgorithmEnum.MOST_TRUSTED_SOURCE, left, right, null, null); assertEquals("true", mergeValue); mergeValue = mFBRecordMerger.createMergeValue("MFB", "MFB", "test", 2, 1, SurvivorShipAlgorithmEnum.MOST_TRUSTED_SOURCE, left, right, null, null); assertEquals("false", mergeValue); }