I have the following method which works fine. But it has multple if / else
logic going on in there and there is likely going to be an additional few more if/else logic that needs to be added soon.
Is there a way I could write this more elegantly, possibly by using Predicate instead?
To note: replaceFunction is a functional interface I wrote myself with the method apply in it.
It takes in 3 Strings and return a String based on some logic.
private String getLabel(String endDate, Map<String, Object> details) {
LocalDate offerEndDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
LocalDate currentDate = ZonedDateTime.now(clock).withZoneSameInstant(zoneId).toLocalDate();
long numberOfDays = DAYS.between(currentDate, offerEndDate);
// can't use switch cos numberOfDays is long and don't want to perform any downcasting to an int just for that.
if (numberOfDays > 20) {
return replaceFunction.apply((String)details.get("EXPIRY_DATE"), "~EXPIRY_DATE~", offerEndDate.format(DateTimeFormatter.ofPattern("MM-dd-yy")));
} else if (numberOfDays > 13) {
return replaceFunction.apply((String)details.get("EXPIRY_DAYS"), "~NO_OF_DAYS~", String.valueOf(numberOfDays));
} else if (numberOfDays == 2) {
return (String)details.get("EXPIRES_TOMORROW");
} else if (numberOfDays == 1) {
return (String)details.get("LAST_EXPIRY_DAY");
} else {
return null;
}
}