package org.railwaystations.rsapi.adapter.web.writer;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class PhotographersTxtWriter extends AbstractHttpMessageConverter<Map<String, Long>> {

    public PhotographersTxtWriter() {
        super(MediaType.TEXT_PLAIN);
    }

    private static void photographerToCsv(final PrintWriter pw, final Map.Entry<String, Long> photographer) {
        pw.println(String.format("%s\t%s", photographer.getValue(),
                photographer.getKey()));
    }

    @Override
    protected boolean supports(final Class<?> clazz) {
        return Map.class.isAssignableFrom(clazz);
    }

    @Override
    protected Map<String, Long> readInternal(final Class<? extends Map<String, Long>> clazz, final HttpInputMessage inputMessage) throws HttpMessageNotReadableException {
        return null;
    }

    @Override
    protected void writeInternal(final Map<String, Long> stringLongMap, final HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        try (final PrintWriter pw = new PrintWriter(new OutputStreamWriter(outputMessage.getBody(), StandardCharsets.UTF_8))) {
            pw.println("count\tphotographer");
            stringLongMap.entrySet().forEach(photographer -> photographerToCsv(pw, photographer));
            pw.flush();
        }
    }

}

@@ -413,16 +413,57 @@
 ception;
+%0Aimport org.springframework.lang.NonNull;
 %0A%0Aimport
@@ -1060,16 +1060,25 @@
 upports(
+@NonNull 
 final Cl
@@ -1089,24 +1089,24 @@
 ?%3E clazz) %7B%0A
-
         retu
@@ -1205,24 +1205,33 @@
 eadInternal(
+@NonNull 
 final Class%3C
@@ -1265,16 +1265,25 @@
 %3E clazz,
+ @NonNull
  final H
@@ -1365,19 +1365,85 @@
     
-return null
+throw new HttpMessageNotReadableException(%22read not supported%22, inputMessage)
 ;%0A  

Add NonNull Annotation