package br.gov.servicos.editor.servicos;

import br.gov.servicos.editor.cartas.Carta;
import br.gov.servicos.editor.cartas.Cartas;
import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.Formatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import static java.util.Locale.getDefault;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static lombok.AccessLevel.PRIVATE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
@FieldDefaults(level = PRIVATE, makeFinal = true)
class ListarCartasController {

    Path v3;
    Cartas cartas;

    Formatter<Carta> formatter;

    @Autowired
    ListarCartasController(File repositorioCartasLocal, Cartas cartas, Formatter<Carta> formatter) {
        this.cartas = cartas;
        this.formatter = formatter;
        this.v3 = Paths.get(repositorioCartasLocal.getAbsolutePath(), "cartas-servico", "v3", "servicos");
    }

    @ResponseBody
    @RequestMapping(value = "/editar/api/servicos", method = GET)
    Iterable<Metadados> listar() throws IOException {
        FilenameFilter filter = (x, name) -> name.endsWith(".xml");
        Function<Path, Carta> getId = f -> carta(formatter, f);
        Function<Path, Map<Carta, Path>> indexaServicos = f -> Arrays.asList(f.toFile().listFiles(filter))
                .stream()
                .map(File::toPath)
                .collect(toMap(getId, x -> x));

        Map<Carta, Path> mapaServicos = indexaServicos.apply(v3);

        return mapaServicos.entrySet().parallelStream()
                .map(p -> p.getKey().getMetadados())
                .filter(Objects::nonNull)
                .collect(toList());
    }

    @SneakyThrows
    private Carta carta(Formatter<Carta> factory, Path f) {
        return factory.parse(f.toFile().getName().replaceAll(".xml$", ""), getDefault());
    }

}

package br.gov.servicos.editor.servicos;

import br.gov.servicos.editor.cartas.Carta;
import br.gov.servicos.editor.cartas.RepositorioGit;
import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.Formatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;

import static br.gov.servicos.editor.utils.Unchecked.Function.unchecked;
import static java.util.Locale.getDefault;
import static java.util.stream.Collectors.toList;
import static lombok.AccessLevel.PRIVATE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
@FieldDefaults(level = PRIVATE, makeFinal = true)
class ListarCartasController {

    RepositorioGit repositorioGit;
    Formatter<Carta> formatter;

    @Autowired
    ListarCartasController(RepositorioGit repositorioGit, Formatter<Carta> formatter) {
        this.formatter = formatter;
        this.repositorioGit = repositorioGit;
    }

    @ResponseBody
    @RequestMapping(value = "/editar/api/servicos", method = GET)
    Iterable<Metadados> listar() throws IOException {
        return Arrays.asList(repositorioGit.getCaminhoAbsoluto().toFile().listFiles((x, name) -> name.endsWith(".xml")))
                .parallelStream()
                .map(f -> f.getName().replaceAll(".xml$", ""))
                .map(unchecked(id -> formatter.parse(id, getDefault())))
                .map(Carta::getMetadados)
                .collect(toList());
    }

    @SneakyThrows
    private Carta carta(Formatter<Carta> factory, Path f) {
        return factory.parse(f.toFile().getName().replaceAll(".xml$", ""), getDefault());
    }

}

Simplifica listagem de cartas