Skip to content

Instantly share code, notes, and snippets.

@prestigegodson
Created July 7, 2022 23:23
Show Gist options
  • Save prestigegodson/e7bebab720b4f8d044ba929d8a2dab14 to your computer and use it in GitHub Desktop.
Save prestigegodson/e7bebab720b4f8d044ba929d8a2dab14 to your computer and use it in GitHub Desktop.
private Page<GroupedCustomerTransaction> groupCustomerTransactionRecordByMonthOfYear(Page<CustomerTransactionDTO> customerTransactionPage) {
List<CustomerTransactionDTO> customerTransactions = customerTransactionPage.getContent();
Map<String, List<CustomerTransactionDTO>> store = new HashMap<>();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MMMM-yyyy");
customerTransactions.stream().forEach(customerTransaction -> {
String createdYearMonth = dateTimeFormatter.format(customerTransaction.getCreatedDate());
if(store.containsKey(createdYearMonth)){
store.get(createdYearMonth).add(customerTransaction);
}else{
List<CustomerTransactionDTO> list = new ArrayList<CustomerTransactionDTO>(){};
list.add(customerTransaction);
store.put(createdYearMonth, list);
}
});
List<GroupedCustomerTransaction> groupedCustomerTransactions = buildCustomerTransactionGroup(store);
return new PageImpl<>(groupedCustomerTransactions, customerTransactionPage.getPageable(), customerTransactionPage.getTotalElements());
}
private List<GroupedCustomerTransaction> buildCustomerTransactionGroup(Map<String, List<CustomerTransactionDTO>> store) {
return store.keySet().stream().map(key ->
GroupedCustomerTransaction.builder()
.date(key)
.customerTransactions(store.get(key))
.build()).collect(Collectors.toList());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment