I am really struggling with using wildcards/generics. I'm trying to create a FileManager
utility class that can accept custom Java Beans and write read/write the bean to a file. Just to give an example imagine I have an interface called Data
that is implemented by RecipeData
and DemographicData
. I'm using Super CSV
to convert a CSV file to a Java Bean. Here is my read method which is based on tutorial code I found from Super CSV
.
public interface Data { method declarations }
public class RecipeData implements Data { class stuff goes here }
public class DemographicData implements Data { class stuff goes here }
final public class FileManager {
public static void parseCsvFile(String filename, CellProcessor[] processors, String[] nameMappings, List<? extends Data> container) {
ICsvBeanReader beanReader = null;
try {
beanReader = new CsvBeanReader(new FileReader(filename), CsvPreference.STANDARD_PREFERENCE);
Data data;
while((data = beanReader.read(Data.class, nameMappings, processors)) != null ) {
container.add(data);
}
} finally {
if (beanReader != null) {
beanReader.close();
}
}
}
}
Currently, I'm getting the following error:
The method add(capture#1-of ? extends Data) in the type List is not applicable for the arguments (Data)
I'm not sure what I'm doing is even possible. The idea is, that the container
that is passed can be of type RecipeData
or DemographicData
. So I think one problem is that data
should be of those two types.
Can anyone give feedback on how I could potentially fix this or if it'll even work?
EDIT: I'm really not sure this is possible.