Created
January 21, 2010 19:15
-
-
Save dsedivec/283094 to your computer and use it in GitHub Desktop.
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
import java.util.HashMap; | |
public class EmptyCollectionMap extends HashMap { | |
Class collectionType; | |
public EmptyCollectionMap(Class collectionType) { | |
super(); | |
this.collectionType = collectionType; | |
} | |
public Object get(Object key) { | |
Object collection; | |
if (containsKey(key)) | |
collection = super.get(key); | |
else { | |
try { | |
collection = collectionType.newInstance(); | |
} catch (Exception ex) { | |
throw new RuntimeException(ex); | |
} | |
put(key, collection); | |
} | |
return collection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment