Skip to content

Instantly share code, notes, and snippets.

@xenoterracide
Created September 4, 2014 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xenoterracide/173c9c178dc903ced098 to your computer and use it in GitHub Desktop.
Save xenoterracide/173c9c178dc903ced098 to your computer and use it in GitHub Desktop.
Java 7 Generics
interface Entity<ID> {
ID getId();
setId( ID id );
}
class Repository<ID, ENTITY extends Entity<ID>> {
Map<ID, ENTITY> identityMap = new HashMap<>();
void add( ENTITY entity ) {
identityMap.put( entity.getId(), entity );
}
}
// ID could be an Integer, UUID, String, or anything, ENTITY could be anything that
// implements the Entity interface, and it's all strict typed at compile time, so you can't
// add a UUID, User to a repository specified with Integer, Post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment