Trying to convert a Try<List<Message>> to a Try<List<SummarizedMessage>> using Java Vavr

431 Views Asked by At

I'm able to "solve" this with the following (ugly and smelly) code:

List<Message> messages = messagesRepository.findAll().get();

return Try.success(
                    messages
                    .stream()
                    .map(m -> new SummarizedMessage(m.destination(), m.title(), m.date()))
                    .toList()
            );

But how can I avoid this get() and Try.success()?

2

There are 2 best solutions below

2
On BEST ANSWER

You can use map and orElse.

messagesRepository.findAll().map(messages -> messages.stream()
                     .map(m -> new SummarizedMessage(m.destination(), m.title(), m.date()))
                     .toList()).orElse(null);
0
On

If this is a Spring-based project you can use vavr's collections instead of java's. Thats is, MessageRepository.findAll can return a list of type io.vavr.collection.List. So it may look as follows:

import io.vavr.collection.List;
import io.vavr.control.Try;

import java.time.Instant;

class Main {
    public static void main(String[] args) {
        var repo = new MessageRepository();
        var summarized = repo
            .findAll()
            .mapTry(
                messages -> messages.map(message -> new SummarizedMessage(message.title(), message.destination(), message.date()))
            )
            .getOrElse(List.empty());
        System.out.println(summarized);

    }
}

record Message(String title, String origin, String destination, String text, Instant date) {
}

record SummarizedMessage(String title, String destination, Instant date) {
}

class MessageRepository {

    Try<List<Message>> findAll() {
        return Try.of(() -> List.of(
            new Message("title", "origin", "dest", "text", Instant.now())
        ));
    }
}

It allows you to drop unnecessary .stream() call. Also instead of null it's better to return an empty list (List.empty of List.of) but it may depend on your logic.