0

I have this interface

public interface MyInterface{
    Collection<T> Find(T t);
    Collection<T> FindAll();
    T FindById(int id);
    void Add(T t);
    void Remove(T t);
}

What must I do for the T FindById(int id) method if I need to be guaranteed that T will have an Id of type int? Should I create another interface that my object must extend to use FindById?

4 Answers 4

4

Should I create another interface that my object must extend to use FindById?

Yes (although it is not specific to the FindById method). More precisely, you should have another interface that can guarantee that your class will have an integer ID that you can access. Something like this should suffice:

public interface HasId {
    int GetId();
}

Then you can make your MyInterface class use T extends HasId, as follows:

public interface MyInterface<T extends HasId> {
    Collection<T> Find(T t);
    Collection<T> FindAll();
    T FindById(int id);
    void Add(T t);
    void Remove(T t);
} 
Sign up to request clarification or add additional context in comments.

2 Comments

So in this case if I pass in an integer like 2 in my implementation class I could say return T.GetId() == t?
FindById takes in an integer, so you can just call FindById(x.GetId()), where x is the HasId object.
3

To really get that type-safety, as well as flexibility for non-int identifiers, try something like:

public interface IdentifiedThing<I extends Serializable> {
    I getId();
}

You can then lock down the typing:

public interface MyInterface<I extends Serializable, IT extends IdentifiedThing<I>> {
    Collection<IT> find(IT t);
    Collection<IT> findAll();
    IT findById(I id);
    void add(IT t);
    void remove(IT t);
}

(I've fixed your method capitalization to Java-style too ;-)

5 Comments

The only downside is that id must be an object, so you will suffer autoboxing/unboxing if you need to convert ints to Integers.
@sk. true - as with almost all things in programming, it's a tradeoff. Robert may feel the compile-time safety is worth the runtime performance hit.
I'm confused as to what this does. This guarantees type-safety and that the object will have an id?
@Robert exactly. You're making sure that MyInterface will only deal with IT instances, but further, you're ensuring that these ITs actually have an identifier which is of the same type as that specified as the parameter to findById()
@millhouse This is very nifty. Thanks.
1

You can accept only specific objects into your collection like here it's Entity which will have id for sure

interface Entity {
    int getId();
}

interface MyCollection<T extends Entity> {
    T findById(int id);
}

Comments

0

it's a template way here. And there is something wrong , it should be

public interface MyInterface<T>{

T is the template type.

When you implement this interface , a type should be given to replace the T , if there isn't , T will be replaced by Object .

I think you are right that if you want to guarantee there is an id of type int in the return type.

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.