Skip to content

Instantly share code, notes, and snippets.

@krisskross
Created May 18, 2012 16:55
Show Gist options
  • Save krisskross/2726390 to your computer and use it in GitHub Desktop.
Save krisskross/2726390 to your computer and use it in GitHub Desktop.
public class ProductCatalogue {
private Multimap<Class, ? extends Item> catalogue = ArrayListMultimap.create();
public void add(Item item) {
catalogue.put(item.getClass(), item);
}
public <T extends Item> Collection<Item> list(Class<T> clazz) {
return catalogue.get(clazz);
}
}
ProductCatalogue catalogue = new ProductCatalogue();
catalogue.add(new Book("1", "Book1"));
catalogue.add(new Movie("2", "Movie1"));
// only get books
System.out.println("Books " + catalogue.list(Book.class));
// only get movies
System.out.println("Movies " + catalogue.list(Movie.class));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment