Newer
Older
java-lab / src / main / java / se / progic / javalab / OutputParser.java
@Marcus Bengtsson Marcus Bengtsson on 6 Nov 2 KB Session 1 cleanup
package se.progic.javalab;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
import se.progic.javalab.QueueInfo.DataRow;

public class OutputParser {

    private final ObjectMapper mapper;

    public static final String ROW_FORMAT = "%-90s %-15s %-15s %-15s %-15s %-15s %-15s %-15s\n";


    public OutputParser() {
        this.mapper = new ObjectMapper();
        this.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public List<DataRow> parseJson(final String json, final String filter) {

        try {
            final QueueInfo queueInfo = mapper.readValue(json, QueueInfo.class);

            final List<DataRow> result = queueInfo.data().stream()
                .filter(row -> filter == null ? true : row.name().toUpperCase().contains(filter.toUpperCase()))
                .toList();

            return result;
        } catch (IOException e) {
            throw new CliException(e);
        }
    }

    public void printOutput(final List<DataRow> data) {
        System.out.printf(ROW_FORMAT,
                          "Queue Name",
                          "Routing Type",
                          "Message Cnt",
                          "Consumer Cnt",
                          "Messages Added",
                          "Messages Acked",
                          "Delivering Cnt",
                          "Scheduled Cnt");

        data.stream()
            .sorted()
            .forEach(row -> System.out.printf(ROW_FORMAT,
                                              row.name(),
                                              row.routingType(),
                                              row.messageCount(),
                                              row.consumerCount(),
                                              row.messagesAdded(),
                                              row.messagesAcknowledged(),
                                              row.deliveringCount(),
                                              row.scheduledCount()));
    }
}