Reputation: 3443
i am stuck with an java tradition of using anonymous methods, I am using third party library which have a generic interface which takes table_name as its generic types
like
TableQueryCallback<WYF_Brands> =new TableQueryCallback<WYF_Brands>() {
@Override
public void onCompleted(List<WYF_Brands> arg0, int arg1,
Exception arg2, ServiceFilterResponse arg3) {
// TODO Auto-generated method stub
}
};
here WYF_Brands is my table name.
what i want is
TableQueryCallback<WYF_Users> =new TableQueryCallback<WYF_Users>() {
@Override
public void onCompleted(List<WYF_Users> arg0, int arg1,
Exception arg2, ServiceFilterResponse arg3) {
// TODO Auto-generated method stub
}
};
where WYF_Users is my another table.
Requirement:i want to use such a method for all of my tables but in a reusable manner.
I have number of tables in database and don't wont to create different methods for different tables .I don't know how to use generics that can accept any table name as perameter.
Another thing is that this Interface is part of third party library so i can't change it as it is inside executable jar file.
i am using java as programming language.
Upvotes: 1
Views: 371
Reputation: 13890
I assume you have common base class/interface for WYF_Brands
, WYF_Users
and all other tables. Let it be WYF_Base
. I also assume, that this base class/interface is enough for you to implement your common method. If so, then you can implement method once like this:
public class CommonTableQueryCallback <T extends WYF_Base>
implements TableQueryCallback <T>
{
@Override
public void onCompleted(List <T> list, int n,
Exception exception, ServiceFilterResponse response) {
// Implement your logic here.
// All elements of `list` are guaranteed to extend/implement WYF_Base
// And compiler knows this!
WYF_Base e = list.get (0); // This is correct!
}
}
Then you can use this class like the following:
TableQueryCallback <WYF_Brands> callback =
new CommonTableQueryCallback <WYF_Brands> ();
Upvotes: 1
Reputation: 1502106
It sounds like you just want a generic method:
public <T> TableQueryCallback<T> createTableQueryCallback() {
return new TableQueryCallback<T>() {
@Override
public void onCompleted(List<T> list, int arg1,
Exception arg2, ServiceFilterResponse arg3) {
// I'm assuming the implementation here would be the same each time?
}
};
}
Although I'd be tempted just to create a named nested class instead:
private static SomeSpecificTableQueryCallback<T> implements TableQueryCallback<T> {
@Override
public void onCompleted(List<T> list, int arg1,
Exception arg2, ServiceFilterResponse arg3) {
// I'm assuming the implementation here would be the same each time?
}
}
... I don't see why making it anonymous provides you any benefit here.
Upvotes: 1