2

Here is what I want to do:

I have an interface Service as follows:

public interface Service<A extends Object, R extends Object> {
    R run(A a);
}

Now, I want to link two such services, while keeping it generic:

public class Link<A, R> implements Service<A, R> {

    private Service<A, X> s1; // fail
    private Service<X, R> s2; // fail

    public <X> Link(Service<A, X> s1, Service<X, R> s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    public R run(A a) {
        return s2.run(s1.run(a));
    }
}

The problem that I want to address is to assign a generic type X to class Link, but I want that to be inferred through its constructor and not through the class declaration.

How is it achievable?

Edit:

The way I've solved this is by a utility static method which does the linking process:

public static <A, I, R> Service<A, R> link(final Service<A, I> s1, final Service<I, R> s2) {
    return new Service<A, R>() {
        @Override
        public R run(A a) throws Exception {
            I intermediate = s1.run(a);
            return s2.run(intermediate);
        }
    };
}
2
  • 6
    FWIW, the "extends Object" in you generic type definitions are redundant. Dec 14, 2012 at 17:20
  • Ya, I was experimenting with <? extends Object> etc. and I forgot to remove that.
    – koikahin
    Dec 14, 2012 at 17:50

3 Answers 3

4

You cannot have a generic type X that is part of your class (not selected anew for each method call) that is not part of its declaration.

Sure, you could hide the unsafe casts and be certain your program would work, but those are really your only two options in the limitations of Java's type system.

What I would do, though, is instead of providing a Link constructor, provide the static factory method

public static <A, X, R> Service<A, R> link(Service<A, X> s1, Service<X, R> s2) {
  return new Link<A, X, R>(s1, s2);
}

...ensuring that X is automatically inferred and never escapes.

6
  • That does make sense. But then how do I achieve what I intend to achieve?
    – koikahin
    Dec 14, 2012 at 17:21
  • 1
    I gave you your two options. Accept the use of unsafe casts, or accept that X has to go in your class declaration. Those are your options. Dec 14, 2012 at 17:22
  • I added a sort of third option that comes closer to what you'd probably want. Dec 14, 2012 at 17:28
  • the third one is what I really wanted to achieve, and I was planning something very similar to that too. But I thought that maybe I am missing something. Thanks for your answer.
    – koikahin
    Dec 14, 2012 at 17:59
  • this should probably be a static method
    – newacct
    Dec 14, 2012 at 22:33
1

For better covariance you could adjust the above answers like this:

public class Link <F, X, T>  implements Service <F, T> {
    private final Service<F, ? extends X> from;
    private final Service <X, ? extends T> to;

    public Link(Service <F, ? extends X> from, Service <X, ? extends T> to) {
        this.from = from;
        this.to = to;
    }

    @Override
    public T run (F input) {
        return to.run (from.run (input));
    }
}
1
  • The more important point is this: When I am linking multiple of them, I don't want to track the intermediate X, because it adds unnecessary complexity, and, well, I shouldn't need to. But this is nice! I was expecting more on the lines of this, but unfortunately this isn't perfect either :( Btw, the answer deserves a Vote up (though I don't have the right to do it yet)
    – koikahin
    Dec 15, 2012 at 4:02
0

This will work:

public class Link<A, R, X> implements Service<A, R> {

    private Service<A, X> s1; 
    private Service<X, R> s2; 

    public Link(Service<A, X> s1, Service<X, R> s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    public R run(A a) {
       return s2.run(s1.run(a));
    }
}
1
  • The point of the OP's question was that the OP wanted to avoid declaring class Link<A, R, X>, keeping the type just Link<A, R>, but that's impossible. Dec 14, 2012 at 17:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.