4

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()?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Marcio Frayze
  • 43
  • 1
  • 5

2 Answers2

4

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);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
1

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.

Opal
  • 81,889
  • 28
  • 189
  • 210