jeudi 23 août 2018

How to avoid Method Overloading in Java to avoid duplicates?

I have 2 method which performs 80% same work but differ in result processing. I am doing :

 private <T> T getResponse(final RestURI query, final Class<T> responseClass) throws IOException {
        T response = null;

        final RestResponse<Record> tempResponse = getResponseFromDataPath(query);

        if (isResponseOK(tempResponse, query)) {
            final CustomReader reader = createCustomReaderFromResponse(tempResponse);
            response = objectMapper.readValue(reader, responseClass);
        }

        return response;
    }

    private <T> T getResponse(final RestURI query, final TypeReference valueTypeRef) throws IOException {
        T response = null;

        final RestResponse<Record> tempResponse = getResponseFromDataPath(query);

        if (isResponseOK(tempResponse, query)) {
            final CustomReader reader = createCustomReaderFromResponse(tempResponse);
            response = objectMapper.readValue(reader, valueTypeRef);
        }

        return response;
    }

This looks like lot of duplicate. How do I make this such that I reduce the duplicates.

Much appreciate the advice!

Thanks

Aucun commentaire:

Enregistrer un commentaire