user264953
user264953

Reputation: 1967

Combining several methods into a single generic method

If I have 'n' number of methods like this, is there a way out by which I can optimize this and make it a single function?

Or are there other better options by which I can make this more generic?

public List<Address> getAddressList(String response) {
    List<Address> AddressList = new ArrayList<Address>();
    if (response != null && response.length() > 0) {
        try {
            Gson gson = new Gson();
            Type collectionType = new TypeToken<List<Address>>(){}.getType();
            AddressList = gson.fromJson(response, collectionType);              
        } catch (IllegalStateException ex) {
        } catch (Exception ex) {
        }
    }
    return AddressList;
}

public List<Tweet> getTweetList(String response) {
    List<Tweet> tweetList = new ArrayList<Tweet>();
    if (response != null && response.length() > 0) {
        try {
            Gson gson = new Gson();
            Type collectionType = new TypeToken<List<Tweet>>(){}.getType();
            tweetList = gson.fromJson(response, collectionType);                
        } catch (IllegalStateException ex) {
        } catch (Exception ex) {
        }
    }
    return tweetList;
} 

Upvotes: 0

Views: 66

Answers (1)

Dan Temple
Dan Temple

Reputation: 2764

To duplicate axtavt's answer from this here question:


There is no way to do it without passing actual type of T (as Class<T>) to your method.

But if you pass it explicitly, you can create a TypeToken for List<T> as follows:

private <T> List<T> GetListFromFile(String filename, Class<T> elementType) {
    ...
    TypeToken<List<T>> token = new TypeToken<List<T>>() {}
        .where(new TypeParameter<T>() {}, elementType);
    List<T> something = gson.fromJson(data, token);
    ...
}

See also:


So, to answer your question, you could do something like this:

public List<Address> getAddressList(final String response) {
    return getGenericList(response, Address.class);
}

public List<Tweet> getTweetList(final String response) {
    return getGenericList(response, Tweet.class);
}

@SuppressWarnings("serial")
private <T> List<T> getGenericList(final String response, final Class<T> elementType) {
    List<T> list = new ArrayList<T>();
    if (response != null && response.length() > 0) {
        try {
            final Gson gson = new Gson();
            final Type collectionType = 
                    new TypeToken<List<T>>(){}.where(new TypeParameter<T>() {}, elementType).getType();
            list = gson.fromJson(response, collectionType);
        }
        catch (final IllegalStateException ex) {
        }
        catch (final Exception ex) {
        }
    }
    return list;
}

EDIT: Tried the code out

I tried this code out with the following small test that should just create a list of a couple of addresses:

public static void main(final String[] args) {
    final List<Address> addressList = getAddressList("[{}, {}]");
    System.out.println(addressList);
}

And the output was:

[gson.Address@6037fb1e, gson.Address@7b479feb]

I made my own Address class in my test project, hence the gson.Address in the above output.

Upvotes: 3

Related Questions