I'm currently developing a storage library which lets the user persist objects to external storage. I wanted to add support for lists and sets so I added functions like this:
public <E extends Persistable> List<E> readList(String fileName, List<E> defaultValue)
public <E extends Persistable> Set<E> readSet(String fileName, Set<E> defaultValue)
Now I thought that it would be nice to add support for every type of Collection. I thought about something like this:
public <C extends Collection<E>, E extends Persistable> C readCollection(String fileName, C defaultValue)
My problem is the following: If the user calls readCollection(FILE, collection) and passes a non null collection as second parameter, I can use it to add the stored items to that collection. But if the user has stored items before and passes null as second parameter I have to create a new instance of the collection. Is that even possible in Java?