tags:

views:

92

answers:

2

Currently I'm working on a service interface which retrieves domain objects based on a primary key. However I get the feeling I'm not efficiently using generics.

Base domain objects look as follows:

public interface DomainObject<PK extends Serializable> extends Serializable {
    PK getID();
}

My service interface looks as follows:

public interface LoadService<T extends DomainObject<PK>, PK extends Serializable> {
    T load(PK ID);
}

This works, however I have to specify the PK type in the service generics, even though the PK type is already known inside T. Is there any way I can get around having to define my PK again in the LoadService interface? Something like:

LoadService<T extends DomainObject<? extends Serializable as PK> { ... }

Help will be greatly appreciated!

A: 

Try using multiple bounds for the type parameter, if T both extends DomainObject and implements Serializable:

interface LoadService<T extends DomainObject<T> & Serializable> {
}
True Soft
Try? Where's the explanation?
BalusC
+1  A: 

There is no way to avoid that because you use PK class at the 'LoadService'. I mean that you can define service like

interface LoadService<T extends DomainObject<?>> {
    void store(T data);
}

However, that's not the option if you use PK class because compiler checks that PK type is compatible with the domain object type.

Another option is to remove type parameter from DomainObject, i.e. perform the following:

interface DomainObject extends Serializable {
    Serializable getID();
}
denis.zhdanov