Generic factory of generic containers

Posted by Feuermurmel on Stack Overflow See other posts from Stack Overflow or by Feuermurmel
Published on 2011-11-24T18:08:28Z Indexed on 2011/11/26 17:52 UTC
Read the original article Hit count: 143

Filed under:
|
|

I have a generic abstract class Factory<T> with a method createBoxedInstance() which returns instances of T created by implementations of createInstance() wrapped in the generic container Box<T>.

abstract class Factory<T> {
    abstract T createInstance();

    public final Box<T> createBoxedInstance() {
        return new Box<T>(createInstance());
    }

    public final class Box<T> {
        public final T content;

        public Box(T content) {
            this.content = content;
        }
    }
}

At some points I need a container of type Box<S> where S is an ancestor of T. Is it possible to make createBoxedInstance() itself generic so that it will return instances of Box<S> where S is chosen by the caller? Sadly, defining the function as follows does not work as a type parameter cannot be declared using the super keyword, only used.

public final <S super T> Box<S> createBoxedInstance() {
    return new Box<S>(createInstance());
}

The only alternative I see, is to make all places that need an instance of Box<S> accept Box<? extends S> which makes the container's content member assignable to S.

Is there some way around this without re-boxing the instances of T into containers of type Box<S>? (I know I could just cast the Box<T> to a Box<S> but I would feel very, very guilty.)

© Stack Overflow or respective owner

Related posts about java

Related posts about generics