0

Lets say I have 2 interfaces which define some kind of container format holding a specific type of data.

public interface Content { } public interface Holder1<T extends Content> { } public interface Holder2<T extends Content> { } 

Now I want some converter which defines objects that can transform an object of type Holder1 into an Holder2.

This converter should keep information about the kind of objects stored within the original object:

public interface ConverterPrototype1 { public <U extends Content> Holder2<U> convert(Holder1<U> source); } 

But I also want to be able to restrict the type of Holder1 that some converter can work on:

interface ConverterPrototype2<U extends Content, V extends Holder1<U>> { public Holder2<U> convert(V source); } 

Is there a way to combine the semantics of these 2 interfaces into a single one? Something like

//INVALID CODE! interface CombinedConvertor<V extends Holder1> { public <U extends Content> Holder2<U> convert(V<U> source); } 

I'm not sure if my title is suited for this problem, but I couldn't find a better description... Similar problems posted here always seemed to talk about different things.

Edit: After stumbling upon this link, I came up with following code. It is still invalid, but closer to actual java code.

//INVALID CODE! interface CombinedConvertor<X extends Source<?>> { public <U extends Content, V extends X & Source<U>> Target<U> convert(V source); } 
2
  • I might just be missing something but ... you already guarantee that Holder1 and Holder2 can only hold something that extends Content in their definitions. Commented Apr 18, 2014 at 16:15
  • Correct, but I'd like to know the specific type that is returned when using the Convertor (for whatever possible implementation of Content), instead of simply working with the interface. I hope that makes sense :) Commented Apr 18, 2014 at 16:23

3 Answers 3

0

It is possible to create a CombinedConvertor interface, as long as you define another generic type parameter to represent the single generic type parameter of Holder1, as you already have done to ConverterProptotype2. This removes the generic type parameter from the convert method and adds it to the interface itself.

interface CombinedConvertor<U extends Content, V extends Holder1<U>> { public Holder2<U> convert(V source); } 
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not really sure I understand, using ConvertorPrototype2 I wouldn't be able to do the following, for example: Holder2<MyContent> holder2 = convertor.convert(new Holder1Impl<MyContent>()). That is, I want to leave the generic type of the content out of the interface description.
0

If I'm understanding what you're asking, and from your comment to the other answer ... you can infer both.

public interface Converter { public <U extends Content, V extends Holder1<U>> Holder2<U> convert(V source); } 

1 Comment

This is pretty close, but it doesn't allow me to restrict the type of Holder1 to be used as input parameter for the Converter. For example, I'd like to have a converter that works only on Holder1SubInterface.
0

After finding the format of code posted in my edit, I found many related items talking about this problem. It appears the java language simply does not support this case. I can recommend this post to learn more about the details of this limitation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.