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);
}
Community
  • 1
  • 1
DieterDP
  • 4,039
  • 2
  • 29
  • 38
  • I might just be missing something but ... you already guarantee that `Holder1` and `Holder2` can only hold something that extends `Content` in their definitions. – Brian Roach Apr 18 '14 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 :) – DieterDP Apr 18 '14 at 16:23

3 Answers3

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);
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • 1
    I'm not really sure I understand, using `ConvertorPrototype2` I wouldn't be able to do the following, for example: `Holder2 holder2 = convertor.convert(new Holder1Impl())`. That is, I want to leave the generic type of the content out of the interface description. – DieterDP Apr 18 '14 at 16:27
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);
}
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • 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`. – DieterDP Apr 22 '14 at 08:43
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.

Community
  • 1
  • 1
DieterDP
  • 4,039
  • 2
  • 29
  • 38