Eugenio Cuevas
Eugenio Cuevas

Reputation: 11078

Using generics to refactor

I am used to use generics in typed collections, but I never actually used them to develop something.

I have several classes like this:

public class LogInfoWsClient extends GenericWsClient {
    public void sendLogInfo(List<LogInfo> logInfoList) {
        WebResource ws = super.getWebResource("/services/logInfo");
        try {
            String response = ws.accept(MediaType.TEXT_HTML).type(MediaType.APPLICATION_XML).put(String.class, new GenericEntity<List<LogInfo>>(logInfoList) {
            });     
    }
}

Where the only thing changing between one and another is the service String ("/services/info"), and the type of the list (LogInfo in this case)

I have refactored a couple of methods to a GenericWsClient class, but my objective would be to have something I can use like this:

List<LogInfo> myList = database.getList();
SuperGenericClient<List<LogInfo>> superClient = new SuperGenericClient<List<LogInfo>>();
superClient.send(myList,"/services/logInfo");

But I cannot figure out how to do it, or even if its possible. Would it be possible?

Upvotes: 2

Views: 139

Answers (3)

mprabhat
mprabhat

Reputation: 20323

Yes it is possible infact if you look at java.util.collection package for example you will find all classes to be parameterzid.

So your class will be something like this

public SuperGenericClient<E> {       
    public E getSomething() {
         return E;
    }
}

Then to use it you will have

SuperGenericClient<String> myGenericClient = new SuperGenericClient<String>();
String something = myGenericClient.getSomething();

Extending your example itself your code will look like this:

public class SuperGenericClient<E> extends GenericWsClient {
    public void send(List<E> entityList, String service) {
        WebResource ws = super.getWebResource(service);
        try {
            String response = ws.accept(MediaType.TEXT_HTML).type(MediaType.APPLICATION_XML).put(String.class, new GenericEntity<E>(entityList) {
            });
        }            
    }
}

public class GenericEntity<E> {
    public GenericEntity(List<E> list){

    }
}

You must read this for a very good understanding of Generics.

Upvotes: 1

Argyle
Argyle

Reputation: 3394

Declare your class like this:

public class LogThing<T> {
    public void sendLogInfo(List<T> list) {
        // do thing!
    }
}

And when you use it, do so like this:

List<LogInfo> myList = db.getList();
LogThing<LogInfo> superClient = new LogThing<LogInfo>();
superClient.sendLogInfo(myList);

Upvotes: 1

assylias
assylias

Reputation: 328923

You could write your class like the one below - you can apply the same idea to GenericEntity.

public class SuperGenericClient<T> extends GenericWsClient {

    public void send(List<T> list, String service) {
        WebResource ws = super.getWebResource(service);
        try {
            String response = ws.accept(MediaType.TEXT_HTML).type(MediaType.APPLICATION_XML).put(String.class, new GenericEntity<T>(list) {
            });
        }            
    }
}

You can then call it like that:

List<LogInfo> myList = database.getList();
SuperGenericClient<LogInfo> superClient = new SuperGenericClient<LogInfo>();
superClient.send(myList,"/services/logInfo");

Upvotes: 1

Related Questions