Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.example.hoge;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
class HogeService {
private <T extends UpdatableRecord> List<T> getUpdatableRecords(
List<T> existingList,
List<T> addedList,
LocalDateTime endDate
) {
var existingUniqueIdToMap = existingList.stream().collect(Collectors.toMap(T::getUniqueKey, data -> data));
var updatableRecords = new ArrayList<T>();
addedList.forEach(data -> {
var existingData = existingUniqueIdToMap.get(data.getUniqueKey());
if (existingData == null) {
updatableRecords.add(data);
} else {
if (!existingData.isSame(data)) {
existingData.setEndDate(endDate);
updatableRecords.add(data);
updatableRecords.add(existingData);
}
}
});
return updatableRecords;
}
}
package com.example.hoge;
import java.time.LocalDateTime;
public interface UpdatableRecord {
String getUniqueKey();
<T extends UpdatableRecord> Boolean isSame(T other);
void setEndDate(LocalDateTime endDate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment