Last active
October 24, 2020 23:16
-
-
Save MazizEsa/b5c6e0b09ac8e405904d7f8b17d9dd43 to your computer and use it in GitHub Desktop.
Method Abstraction Level
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
public List<SomeDataFull> methodWithNoAbstraction() { | |
// get data from DB for instance | |
final List<SomeData> listOfDataFromDb = getDataFromDb(); // a.1 | |
final List<String> someDataRetrievedIds = listOfDataFromDb.stream().map(SomeData::getId).collect(Collectors.toList()); //a.2 | |
final Map<String, Boolean> someDataIdMapping = getVerificationFromExternalSystem(someDataRetrievedIds) | |
.stream().collect(Collectors.toMap(SomeDataVerification::getId, SomeDataVerification::isOk)); | |
final List<SomeData> verifiedSomeData = listOfDataFromDb.stream().filter(eachData -> someDataIdMapping.getOrDefault(eachData.getId(), false)) //a.3 | |
.collect(Collectors.toList()); | |
final List<String> someDataVerifiedIds = verifiedSomeData.stream().map(SomeData::getId).collect(Collectors.toList()); | |
final Map<String, SomeData2> idAdditionalDataMapping = getAdditionalData(someDataVerifiedIds).stream() | |
.collect(Collectors.toMap(SomeData2::getId, Function.identity())); | |
final List<SomeDataFull> mergedFullData = verifiedSomeData.stream().map(eachData -> { // a.4 | |
final var someData2 = idAdditionalDataMapping.get(eachData.getId()); | |
return SomeDataFull.builder() | |
.id(eachData.getId()) | |
.email(someData2.getEmail()) | |
.extraData(someData2.getExtraData()) | |
.fileNumber(someData2.getFileNumber()) | |
.whateverStuff(eachData.getWhateverStuff()) | |
.build(); | |
}).collect(Collectors.toList()); | |
return mergedFullData; | |
} | |
public List<SomeDataFull> methodWithMixAbstraction() { | |
final List<SomeData> listOfDataFromDb = getDataFromDb(); | |
final List<String> someDataRetrievedIds = listOfDataFromDb.stream().map(SomeData::getId).collect(Collectors.toList()); | |
final Map<String, Boolean> someDataIdMapping = getVerificationFromExternalSystem(someDataRetrievedIds) | |
.stream().collect(Collectors.toMap(SomeDataVerification::getId, SomeDataVerification::isOk)); | |
final List<SomeData> verifiedSomeData = listOfDataFromDb.stream().filter(eachData -> someDataIdMapping.getOrDefault(eachData.getId(), false)) | |
.collect(Collectors.toList()); | |
final Map<String, SomeData2> idAdditionalDataMapping = getVerifiedAdditionalData(verifiedSomeData); | |
final List<SomeDataFull> mergedFullData = verifiedSomeData.stream().map(eachData -> { | |
final var someData2 = idAdditionalDataMapping.get(eachData.getId()); | |
return SomeDataFull.builder() | |
.id(eachData.getId()) | |
.email(someData2.getEmail()) | |
.extraData(someData2.getExtraData()) | |
.fileNumber(someData2.getFileNumber()) | |
.whateverStuff(eachData.getWhateverStuff()) | |
.build(); | |
}).collect(Collectors.toList()); | |
return mergedFullData; | |
} | |
public List<SomeDataFull> methodWithProperAbstraction() { | |
final List<SomeData> verifiedSomeData = getVerifiedSomeData(); | |
final Map<String, SomeData2> idAdditionalDataMapping = getVerifiedAdditionalData(verifiedSomeData); | |
final List<SomeDataFull> mergedFullData = mergeSomeDataAndAdditional(verifiedSomeData, idAdditionalDataMapping); | |
return mergedFullData; | |
} | |
private Map<String, SomeData2> getVerifiedAdditionalData(List<SomeData> verifiedSomeData) { | |
final List<String> someDataVerifiedIds = verifiedSomeData.stream().map(SomeData::getId).collect(Collectors.toList()); | |
return getAdditionalData(someDataVerifiedIds).stream() | |
.collect(Collectors.toMap(SomeData2::getId, Function.identity())); | |
} | |
private List<SomeDataFull> mergeSomeDataAndAdditional(List<SomeData> verifiedSomeData, Map<String, SomeData2> idAdditionalDataMapping) { | |
return verifiedSomeData.stream().map(eachData -> { | |
final var someData2 = idAdditionalDataMapping.get(eachData.getId()); | |
return SomeDataFull.builder() | |
.id(eachData.getId()) | |
.email(someData2.getEmail()) | |
.extraData(someData2.getExtraData()) | |
.fileNumber(someData2.getFileNumber()) | |
.whateverStuff(eachData.getWhateverStuff()) | |
.build(); | |
}).collect(Collectors.toList()); | |
} | |
private List<SomeData> getVerifiedSomeData() { | |
final List<SomeData> listOfDataFromDb = getDataFromDb(); | |
final List<String> someDataRetrievedIds = listOfDataFromDb.stream().map(SomeData::getId).collect(Collectors.toList()); | |
final Map<String, Boolean> someDataIdMapping = getVerificationFromExternalSystem(someDataRetrievedIds) | |
.stream().collect(Collectors.toMap(SomeDataVerification::getId, SomeDataVerification::isOk)); | |
return listOfDataFromDb.stream().filter(eachData -> someDataIdMapping.getOrDefault(eachData.getId(), false)) | |
.collect(Collectors.toList()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment