3 ms·
A use-case I've had for a quick right-biased Java Either I threw together is handling a migration from java.util.Date to java.time.LocalDate: List<LocalDat
by ubertaco 7y ago
A use-case I've had for a quick right-biased Java Either I threw together is handling a migration from java.util.Date to java.time.LocalDate:
List<LocalDate> dobs = users.stream()
.map(person -> Either.either(person::getDeprecatedDob, person::getDobLocalDate))
.map(oldOrNewFormat -> oldOrNewFormat.fold(
(Date oldFormat) -> convertOldFormatToNew(oldFormat),
(LocalDate newFormat) -> newFormat,
))
.collect(Collectors.toList());
I use the right-biased-ness of the Either to prefer the new format, if available, but failing that, use the old format and then convert as needed.
- james-mcelwain 7y agoThis could be accomplished using Optional#orElseGet without any additional library dependencies, though.
- ubertaco 7y agoThat's true. I didn't think of doing that. Nice!