Created
September 4, 2014 03:04
-
-
Save xenoterracide/173c9c178dc903ced098 to your computer and use it in GitHub Desktop.
Java 7 Generics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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