Skip to content

Instantly share code, notes, and snippets.

@yukihirai0505
Created October 15, 2019 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukihirai0505/11ba086b02df2bb488c8865e0fbd7a80 to your computer and use it in GitHub Desktop.
Save yukihirai0505/11ba086b02df2bb488c8865e0fbd7a80 to your computer and use it in GitHub Desktop.
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