package uk.ac.ebi.quickgo.client.service.loader.presets;

import uk.ac.ebi.quickgo.rest.search.RetrievalException;
import uk.ac.ebi.quickgo.rest.search.request.FilterRequest;
import uk.ac.ebi.quickgo.rest.search.request.converter.ConvertedFilter;
import uk.ac.ebi.quickgo.rest.search.request.converter.RESTFilterConverterFactory;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Simple to use implementation to retrieve values from an external restful service.
 */
public class RestValuesRetriever {

    private final RESTFilterConverterFactory converterFactory;

    public RestValuesRetriever(RESTFilterConverterFactory restConverterFactory) {
        this.converterFactory = restConverterFactory;
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(RestValuesRetriever.class);

    /**
     * Retrieve values identified by the lookup key from an external source.
     * @param lookupKey
     * @param <T> the type of the return value.
     * @return a set of retrieved values.
     */
    public <T> Set<T> retrieveValues(String lookupKey) {
        FilterRequest restRequest = FilterRequest.newBuilder().addProperty(lookupKey).build();

        try {
            ConvertedFilter<List<T>> convertedFilter = converterFactory.convert(restRequest);
            final List<T> convertedValues = convertedFilter.getConvertedValue();
            return new HashSet<>(convertedValues);
        } catch (RetrievalException | IllegalStateException e) {
            LOGGER.error("Failed to retrieve via REST call the relevant Used values: ", e);
        }
        return new HashSet<>();
    }
}

@@ -345,60 +345,16 @@
 til.
-HashSet;%0Aimport java.util.List;%0Aimport java.util.Set
+Optional
 ;%0Aim
@@ -537,16 +537,108 @@
 iever %7B%0A
+    private static final Logger LOGGER = LoggerFactory.getLogger(RestValuesRetriever.class);
 %0A    pri
@@ -841,102 +841,8 @@
  %7D%0A%0A
-    private static final Logger LOGGER = LoggerFactory.getLogger(RestValuesRetriever.class);%0A%0A
     
@@ -926,32 +926,8 @@
 ce.%0A
-     * @param lookupKey%0A
     
@@ -970,16 +970,99 @@
  value.%0A
+     * @param lookupKey the value used as the identifier for the looked up values.%0A
      * @
@@ -1118,19 +1118,24 @@
 lic %3CT%3E 
-Set
+Optional
 %3CT%3E retr
@@ -1260,17 +1260,16 @@
 uild();%0A
-%0A
         
@@ -1302,23 +1302,17 @@
 dFilter%3C
-List%3CT%3E
+T
 %3E conver
@@ -1378,40 +1378,27 @@
     
-final List%3CT%3E convertedValues = 
+return Optional.of(
 conv
@@ -1432,58 +1432,8 @@
 ue()
-;%0A            return new HashSet%3C%3E(convertedValues
 );%0A 
@@ -1521,16 +1521,30 @@
 R.error(
+String.format(
 %22Failed 
@@ -1560,48 +1560,42 @@
 ve v
-ia REST call the relevant Used values: %22
+alues for %25s via REST%22, lookupKey)
 , e)
@@ -1625,21 +1625,22 @@
 urn 
-new HashSet%3C%3E
+Optional.empty
 ();%0A

Replace using a Set<T> as the return type to Optional<T>