package uk.ac.ebi.quickgo.annotation.converter;

import uk.ac.ebi.quickgo.annotation.model.Annotation;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * This class contains methods that are shared between the AnnotationToX converters.
 * @author Tony Wardell
 * Date: 20/01/2017
 * Time: 17:01
 * Created with IntelliJ IDEA.
 */
abstract class AnnotationTo {
    private static final String ID_DELIMITER = ":";
    private static final String COMMA = ",";
    private static final String PIPE = "|";
    private static final DateFormat YYYYMMDD_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");

    String withFromAsString(List<Annotation.ConnectedXRefs<Annotation.SimpleXRef>> connectedXRefs) {
        if (connectedXRefs == null || connectedXRefs.size() == 0) {
            return "";
        }
        return connectedXRefs.stream()
                             .map(this::simpleRefAndToString)
                             .collect(Collectors.joining(PIPE));
    }

    private String simpleRefAndToString(Annotation.ConnectedXRefs<Annotation.SimpleXRef> itemList) {
        return itemList.getConnectedXrefs()
                       .stream()
                       .map(Annotation.SimpleXRef::asXref)
                       .collect(Collectors.joining(COMMA));
    }

    String extensionsAsString(List<Annotation.ConnectedXRefs<Annotation.QualifiedXref>> connectedXRefs) {
        if (connectedXRefs == null || connectedXRefs.size() == 0) {
            return "";
        }
        return connectedXRefs.stream()
                             .map(this::qualifiedRefAndToString)
                             .collect(Collectors.joining(PIPE));
    }

    private String qualifiedRefAndToString(Annotation.ConnectedXRefs<Annotation.QualifiedXref> itemList) {
        return itemList.getConnectedXrefs()
                       .stream()
                       .map(Annotation.QualifiedXref::asXref)
                       .collect(Collectors.joining(COMMA));
    }

    public String[] idToComponents(String id) {
        return id==null? new String[] {"",""}:id.split(ID_DELIMITER);
    }

    public String toYMD(Date date) {
        return date==null?"":YYYYMMDD_DATE_FORMAT.format(date);
    }

}

@@ -207,16 +207,52 @@
 l.List;%0A
+import java.util.function.Function;%0A
 import j
@@ -2103,31 +2103,24 @@
 %0A    %7D%0A%0A    
-public 
 String%5B%5D idT
@@ -2140,24 +2140,24 @@
 tring id) %7B%0A
+
         retu
@@ -2229,15 +2229,8 @@
     
-public 
 Stri
@@ -2322,10 +2322,87 @@
 %0A    %7D%0A%0A
+    final Function%3CString, String%3E nullToEmptyString = s -%3E s==null ? %22%22: s;%0A
 %7D%0A

Move nullToEmptyString to parent class.