1

I'm trying to write a general method that writes different types of Java Beans (so List<JavaBean>) to a file. I'm currently constructing a FileManager utility class. Each Java Bean implements the same interface. Here's an example of what I'm trying to do.

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 writeToCsvFile(String filename, List<Data> data) { file writing logic goes here }
}

I want to be able to pass a List<RecipeData> and a List<DemographicData> to this method. Obviously what I have does not work.

It doesn't seem I can even do the following:

List<Data> data = new ArrayList<RecipeData>(); 

How would this normally be done? In Swift I might use the as? keyword to cast it to the correct type.

**************EDIT**************

Just to preface I'm using the SuperCSV library to assist in parsing rows into a Java Bean and I am using the accepted answer below for the method definition. So I have the following code:

Data dataset;
while((dataset = beanReader.read(Data.class, nameMappings, processors)) != null ) {
    container.add(dataset);
}

I get the following error:

The method add(capture#1-of ? extends Data) in the type List is not applicable for the arguments (Data)

dataset needs to be either type RecipeData or DemographicData for this to work I'd assume. Is there an easy way to fix this so that it is flexible if I add more Beans in the future?

Alex Cauthen
  • 497
  • 5
  • 12
  • 32

1 Answers1

2
final public class FileManager {
    public static void writeToCsvFile(String filename, List<? extends Data> data) { file writing logic goes here }
}

additionally, you can declare

List<Data> data = new ArrayList<RecipeData>(); 

as

List<Data> data = new ArrayList<Data>();

or, in Java 7,

List<Data> data = new ArrayList<>();

and just populate it with RecipeData, since either way you are losing the information that this List is to contain only RecipeData

f.khantsis
  • 3,256
  • 5
  • 50
  • 67
  • This gives me a compiler error "Data cannot be resolved to a type" when I declare my method in that way. – Alex Cauthen Jun 13 '16 at 18:56
  • well.. is `Data` imported in the FileManager class? – f.khantsis Jun 13 '16 at 18:59
  • It's in the same package, so I wouldn't think it'd matter. If what you say is true, could I not do: public static void writeToCsvFile(String filename, List data) – Alex Cauthen Jun 13 '16 at 19:00
  • After refreshing the project that appears to work, however, it does give me errors for instance in my ParseCsv method when I call add, to add a Bean to a List. Can I just do public static void writeToCsvFile(String filename, List data) ? – Alex Cauthen Jun 13 '16 at 19:05
  • What kind of bean? You can, but then you must instantiate the List as a List of only `Data`, like so: List data = new ArrayList(); This list may have both `RecipeData` and `DemographicData` at the same time. – f.khantsis Jun 13 '16 at 19:07
  • let me update the original thread with my problem. I know what I'm doing wrong but I'm not sure how to fix it. – Alex Cauthen Jun 13 '16 at 19:10