0

I am new to generics and i have a method like below

public <T1, T2> void test(Details<T1, T2> reportDetails, List<T1> ent1, List<T2> ent2)
Converter<? super T1, Box> conv1
 Converter<? super T2, Box> conv2

converter is of type org.springframework.core.convert.converter

This is actually for downloading a reports, T1 is one report and T2 is another report and i want it to be more generic so that if more table gets added i don't have to add new types I need T1, T2 to be dynamic that is i want one generic which will server both T1 and T2 so that i don't have to add a new type(T3) if required in future. is there a way to do that ?

to be more clear : I need a way where i can create a generalised list of Converter objects which can store both T1 and T2 so that i dont have to have T1 and T2 and i can have only one.

2
  • What do you actually want to achieve? Totally unclear what you're asking. Commented Dec 11, 2017 at 7:28
  • i have edited my post and have added what i am looking for in detail Commented Dec 11, 2017 at 12:37

2 Answers 2

2

The question is a bit unclear.

The signature you have used basically means that above method is generic, such that it will support any two types for T1 and T2.

For any combination of "two" concrete types, the above signature can work.

If you want more number of types, then you just have to enhance the signature. You can read about Tuples, to further understand this.

EDIT: If I understand you correctly, the way I would solve it is by defining "Report" as a type and then building my way up.

public interface Report {    
    void getReportData(/*any parameters here */);
}

And the method you defined will become:

<T1 extends Report, T2 extends Report> void test(Details<T1, T2> reportDetails, List<T1> ent1, List<T2> ent2);

And the converters will be all kept somewhere which would be defined somewhat like:

List<Converter<T extends Report, Base>>

Hope this is something you are after. I think you should spend some time on bounds in generics if this was what you were looking for.

Sign up to request clarification or add additional context in comments.

3 Comments

i have edited my post and have added what i am looking for in detail
@user3927150 Does the above answer your question now?
sorry for late response. it did not work.my requirement is a single T should cover both T1 and T2.
0

Can you please try <? extends T> and <? super T> . An unbounded wildcard looks like , and basically means . It means the generic can be any type. A bounded wildcard ( or ) places a restriction on the type by saying that it either has to extend a specific type ( is known as an upper bound), or has to be an ancestor of a specific type ( is known as a lower bound).More info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.