Using Generics in java

Hi,
I have the following code :
public static LatestFetchResponse parseFetchResponse(@Nullable String responseBody,@NotNull GsonBuilder jsonBuilder)
    {
        FetchBuddyMessagesResponse fetchBuddyChats = new FetchBuddyMessagesResponse();
        FetchGroupMessagesResponse fetchGroupChats = new FetchGroupMessagesResponse();
        ChatsFetchError fetchChatError = new ChatsFetchError();

        Gson gson = jsonBuilder.create();

        JsonParser jsonParser = new JsonParser();
        JsonElement object = jsonParser.parse(responseBody).getAsJsonObject();

        if (Utils.isFetchError(object))
        {
            fetchChatError = gson.fromJson(object, ChatsFetchError.class);
        }
        else
        {
            if (Utils.isBuddyChat(object))
            {
                fetchBuddyChats = gson.fromJson(object, FetchBuddyMessagesResponse.class);
            }
            else
            {
                fetchGroupChats = gson.fromJson(object, FetchGroupMessagesResponse.class);
            }
        }


        return new LatestFetchResponse(fetchBuddyChats, fetchGroupChats, fetchChatError);
    }

Open in new window


The above code is repeated in many classes except the class name LatestFetchResponse gets changed.

How do i take it out and make it a generic function.

I tried something like :
    public static <T> T parseFetchResponse(@Nullable String responseBody,@NotNull GsonBuilder jsonBuilder)
{
.
.
.
.
return         return new T(fetchBuddyChats, fetchGroupChats, fetchChatError);

But this is giving error.
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
This does not look like a case for a generic. Just normal inheritance. But: even then I don't understand you approach here. A kind of factory method?

As a generic, it must be constrained, thus your different fetch result classes must inherit from the same base class/interface providing your 3 parameter constructor.

E.g. something like this (air-code):

class FetchResult {   
  FetchResult(FetchBuddyMessagesResponse fetchBuddyChats, FetchGroupMessagesResponse fetchGroupChats, ChatsFetchError fetchChatError) { }   
}

class LatestFetchResult extends FetchResult { }

class WhatEver<T extends FetchResult> {
  public static T parseFetchResponse(@Nullable String responseBody,@NotNull GsonBuilder jsonBuilder)
  {
	return new T(fetchBuddyChats, fetchGroupChats, fetchChatError);
  }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.