0

I try to create this class:

public class Class1<T extends Class2<E>> {
    ...
    public E someFunction(T param) {
        ...
    }
    ...
}

I saw topic [blog]: Java class with 2 generic parameters and in it topic suggest using this solution:

public class Class1<T extends Class2<E extends Class3>, E extends Class3> {
    ...
    public E someFunction(T param) {
        ...
    }
    ...
}

It is works, but in this case I should using 2 parameters without one:

Class1<Class21<Class31>, Class31> var = ...

In this solution I duplicate class 'Class31'. Has this task have more simple solution, to using following code:

Class1<Class21<Class31>> var = ...

Update1:

What problem are you trying to solve?

For example, I have class for images. Images can be single-channel (gray image) and multi-channel (3 channels for RGB, HSB and etc. or 4 channels or RGB and etc. + Alpha channel). Also, image can save value RGB in byte 0..255 or in float (0.0, 1.0) and it is different type of images. So, I want to create class for this images and construct it as:

MyImage<MyChannelType3<MyColorTypeInt>>   img1 = ...
MyImage<MyChannelType3<MyColorTypeFloat>> img2 = ...
MyImage<MyChannelType1<MyColorTypeFloat>> img3 = ...

and have access to colors in pixel as:

int Red     = img1.getPixel(x, y)[0];
float Green = img2.getPixel(x, y)[0];
float Gray  = img3.getPixel(x, y);

I think is more useful than:

MyImage<MyChannelType3<MyColorTypeInt>>   img1 = ...
int Red = img1.getPixel(x, y).getChannel(0).getValue();

If I will be firs return MyChannelType3, than MyColorTypeInt and then value or return Object and demand casting it to int, float, int[] or float[].

1
  • It's not clear what you're actually asking. What problem are you trying to solve? I think that your declaration should be Class1<Class21,Class31> var, if you're using the form suggested in the blog. Commented Feb 12, 2012 at 2:39

1 Answer 1

2

This redundancy is unavoidable in Java.

Explanation: In Java a type parameter is just what the name implies, a formal parameter for a type to be substituted with an actual type. What you require is more. You need T to be a parameter for a type constructor. Much like a classes' constructor produces instances of the class, a type constructor produces types. List is a type constuctor which given the actual type String produces the type List<String>.

Applying this knowledge to your problem, you would want T to be a type constructor which takes types E <: Class3 producing types T <: Class2<E> where <: is the subtype relation. This might be declared and used as follows.

class Class1<T<X extends Class3> extends Class2<X>, E extends Class3> {
    public E someFunction(T<E> param) {
        ...
    }
}

Class1<Class21, Class31> var = ...

Further reading on the subject can be found in Generics of a Higher Kind (pdf). Beyond that you can have a look at Scala which offers higher kinds.

Edit based on update: A perhaps preferable solution might be to implement a PixelAccessor.

public class PixelAccessor<C extends YourColor> {
    public static <C extends YourColor> get(Image<? extends ChannelType<? extends C>> image) {
        return new PixelAccessor<C>(image);
    }

    private final Image<? extends ChannelType<? extends C>> image;

    private PixelAccessor(Image<? extends ChannelType<? extends C>> image) {
        this.image = image;
    }

    public C getPixel(int x, int y) {
        // sadly this is unchecked, but "weak" type systems demand compromises
        @SuppressWarnings("unchecked")
        C color = (C) image.getPixel(x, y);
        return color;
    }
}
Sign up to request clarification or add additional context in comments.

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.