On the way of refactoring the below code snippet to java8 using streams
private Date getDate(List<BrowseHistory> historyList, String subscribe, String cancelled) {
for(int i=0; i<= historyList.size(); i++) {
if(isContainsStatus(historyList.get(i), subscribe) && isExist(historyList, cancelled, i)) {
return historyList.get(i).getCreated()
}
}
return null;
}
private boolean isExist(List<BrowseHistory> historyList, String status, int i) {
return historyList.size() == i+1 || (isContainsStatus(historyList.get(i+1), status) || isContainsStatus(historyList.get(i+1), Status.PEND.toString()));
}
private boolean isContainsStatus(BrowseHistory history, String status) {
return history.getStatus().contains(status);
}
I could able to refactor till the if block, with java8 filter.
Below is the Java8 refactored code
private Date getDate(List<BrowseHistory> historyList, String subscribe, String cancelled) {
return IntStream.range(0, historyList.size())
.filter(i -> isContainsStatus(historyList.get(i), subscribe) && isStatusExist(historyList, cancelled, i))
.mapToObj(historyList::get)
.map(BrowseHistory::getCreated)
.findFirst() // find first that got through those filters
.orElse(null);
}
Edited the java8 refactored code based on the below answer.
userSubscriptionPeriodList?