Update CompareOrUpdateResult with NULL Check
Like CompareResult, add in null check for input objects. Test: updated unit test Change-Id: Iea63752ff23a5bfb1cc5cb716ec579bec6c403bf
This commit is contained in:
parent
8cd3a6c287
commit
65e8b66875
2 changed files with 26 additions and 13 deletions
|
|
@ -91,23 +91,26 @@ public final class LinkPropertiesUtils {
|
|||
Function<T, K> keyCalculator) {
|
||||
HashMap<K, T> updateTracker = new HashMap<>();
|
||||
|
||||
for (T oldItem : oldItems) {
|
||||
updateTracker.put(keyCalculator.apply(oldItem), oldItem);
|
||||
}
|
||||
|
||||
for (T newItem : newItems) {
|
||||
T oldItem = updateTracker.remove(keyCalculator.apply(newItem));
|
||||
if (oldItem != null) {
|
||||
if (!oldItem.equals(newItem)) {
|
||||
// Update of existing item.
|
||||
updated.add(newItem);
|
||||
}
|
||||
} else {
|
||||
// New item.
|
||||
added.add(newItem);
|
||||
if (oldItems != null) {
|
||||
for (T oldItem : oldItems) {
|
||||
updateTracker.put(keyCalculator.apply(oldItem), oldItem);
|
||||
}
|
||||
}
|
||||
|
||||
if (newItems != null) {
|
||||
for (T newItem : newItems) {
|
||||
T oldItem = updateTracker.remove(keyCalculator.apply(newItem));
|
||||
if (oldItem != null) {
|
||||
if (!oldItem.equals(newItem)) {
|
||||
// Update of existing item.
|
||||
updated.add(newItem);
|
||||
}
|
||||
} else {
|
||||
// New item.
|
||||
added.add(newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
removed.addAll(updateTracker.values());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -229,7 +229,7 @@ public final class LinkPropertiesUtilsTest {
|
|||
CompareOrUpdateResult<String, String> result =
|
||||
new CompareOrUpdateResult<String, String>(oldItems, emptyList, extractPrefix);
|
||||
assertCompareOrUpdateResult(result,
|
||||
emptyList, strArray("hello123", "howareyou669", "goodbye5678"), emptyList);
|
||||
emptyList, strArray("hello123", "howareyou669", "goodbye5678"), emptyList);
|
||||
|
||||
// Empty -> items: everything added.
|
||||
result = new CompareOrUpdateResult<String, String>(emptyList, newItems, extractPrefix);
|
||||
|
|
@ -244,5 +244,15 @@ public final class LinkPropertiesUtilsTest {
|
|||
result = new CompareOrUpdateResult<>(oldItems, newItems, extractPrefix);
|
||||
assertCompareOrUpdateResult(result,
|
||||
strArray("verywell"), strArray("howareyou669"), strArray("goodbye000"));
|
||||
|
||||
// Null -> items: everything added.
|
||||
result = new CompareOrUpdateResult<String, String>(null, newItems, extractPrefix);
|
||||
assertCompareOrUpdateResult(result,
|
||||
strArray("hello123", "goodbye000", "verywell"), emptyList, emptyList);
|
||||
|
||||
// Items -> null: everything removed.
|
||||
result = new CompareOrUpdateResult<String, String>(oldItems, null, extractPrefix);
|
||||
assertCompareOrUpdateResult(result,
|
||||
emptyList, strArray("hello123", "howareyou669", "goodbye5678"), emptyList);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue