Created
October 15, 2019 06:14
-
-
Save yukihirai0505/11ba086b02df2bb488c8865e0fbd7a80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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