Java method returning two or more generic types -


there lot of questions regarding java methods returning generic types, none of them helped me out far.

so here's code:

interface dao<k, t> {    void    insert(t t);    void    update(k k, t t);    void    delete(k k);    void    delete();    t       select(k k);    list<t> select(); }  public class coursedao implements dao<string, course> {    public void         insert(course t) {}    public void         update(string k, course t) {}    public void         delete(string k) {}    public void         delete() {}    public course       select(string k) {}    public list<course> select() {} }  public class studentdao implements dao<long, student> {    public void          insert(student t) {}    public void          update(long k, student t) {}    public void          delete(long k) {}    public void          delete() {}    public student       select(long k) {}    public list<student> select() {} }  public enum entitytype { course, student } 

now want factory method accepts entitytype parameter , return instance of coursedao or studentdao depending on parameter value.

i tried code below without success:

public <k,t> dao<k,t> createdaofactory(entitytype type) {    switch (type) {       case course  : return (k,t) new coursedao();  break;       case student : return (k,t) new studentdao(); break;    }    return null; } 

could me in writing , invoking method???

cheers,
romualdo.

the cast you're looking (dao<k,t>). you'll warning because generic type erasure makes unsafe. inherent risk in switch factory might forget create corresponding case when add new entitytype. safer alternative redefine entitytype generic types, , let factory. unfortunately, isn't possible proper enums, can simulate this:

abstract class entitytype<k, t> {     public abstract dao<k, t> createdao();      public static final entitytype<string, course> course = new entitytype<string, course>() {         @override         public dao<string, course> createdao() {             return new coursedao();         }     };     public static final entitytype<long, student> student = new entitytype<long, student>() {         @override         public dao<long, student> createdao() {             return new studentdao();         }     }; } 

or can use lambdas reduce boilerplate:

class entitytype<k, t> {     private final supplier<dao<k, t>> constructor;      private entitytype(supplier<dao<k, t>> constructor) {         this.constructor = constructor;     }      public dao<k, t> createdao() {         return constructor.get();     }      public static final entitytype<string, course> course = new entitytype<>(coursedao::new);     public static final entitytype<long, student> student = new entitytype<>(studentdao::new); } 

now, instead of calling createdaofactory(entitytype.course), call entitytype.course.createdao().


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -