java - Casting objects when returning a generics list -


i'm relatively new java , generics. i'm trying understand if i'm doing wrong or not in writing generic method. have following code (greatly simplified):

public class contentiniter {            public contenttype getcontenttype(); }  public interface content { }  public class show implements content {        }  public class movie implements content {        }  public enum contenttype {     movie, show }  public class channel {      public list<show> getshows() {         return getcontentbytype(contenttype.show)     }      public list<movie> getmovies() {         return getcontentbytype(contenttype.movie)     }      private <t> list<t> getcontentbytype(contenttype contenttype) {         list<t> typecontents = lists.newarraylist();         list<contentiniter> allcontentiniters = somemethod(); // returns initers both shows , movies         (content contentiniter : allcontentiniters) {             if (contentiniter.getcontenttype().equals(contenttype)) {                 switch (contenttype) {                 case movie:                     typecontents.add((t) new movie(contentiniter));                     break;                 case show:                     typecontents.add((t) new show(contentiniter));                     break;                 }             }         }         return typecontents;     }  } 

my question relates line:

typecontents.add((t) new movie(contentiniter)); 

the way i've been able code compile if cast content object t. seems yucky me (and don't understand why compiler can't infer type based on calls). moreover, though code works, intellij complains of unchecked cast.

is there better way write generic method?

update: screwed code bit when tried simplify it. fixed reference typecontents. also, added bit more complexity better reflects reality, in hopes of explaining why wasn't checking instanceof.

update 2: realized there yet error...contentiniter doesn't implement content. it's worth noting, contentiniter made object. if seems weird, think of event or other strategy content objects use delegate behaviors.

you're not using generics properly, you're mixing them enumeration when it's not necessary. ideally calling getcontentbytype<show>() , determine list of correct type allcontents using reflection.

try more along lines of (untested):

private <t> list<t> getcontents() {     list<t> typecontents = lists.newarraylist();     list<content> allcontents = somemethod(); // returns both shows , movies     (content content : allcontents) {         if (content instanceof t) {             typecontents.add((t) content);         }     }     return typecontents; } 

and call:

list<show> shows = getcontents<show>(); 

you can restrict types called on extend content.

private <t extends content> list<t> getcontents() {     ... } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -