I have a series of Data Transport Objects, DTO. They have a few elements that are common to each different DTO. One element is a collection of the DTO, because we can have a list, or series of these DTO's to process at a time. I've used generics so Spring can handle the different DTOs and pass up to the UI. Here are some excerpts from my GenericListDTO() class.
@XmlElement
@NotNull
private Collection<DTO> dataList;
@XmlElement
private int totObjs;
My DTO builder is defined like this:
public static <DTO> GenericListDTOBuilder<DTO> builder() {
return new GenericListDTOBuilder<>();
}
Here is the builder class defined inside my GenericList Class.
public static class GenericListDTOBuilder<DTO> {
private Collection<DTO> dataList;
public GenericListDTOBuilder<DTO> data(@NotNull Collection<DTO> data) {
this.dataList = dataList;
return this;
}
private int total;
public GenericListDTOBuilder<DTO> total(int total) {
this.total = total;
return this;
}
public GenericListDTO<DTO> build() {
return new GenericListDTO<>(this.data, this.total, this.validUntil);
}
}
}
Later in the class I have this constructor.
@NotNull
public static <DTO> GenericListDTO<DTO> just(@NotNull Collection<DTO> dataList) {
return builder().dataList(dataList).total(dataList.size()).build();
}
Intellij at the .dataList(dataList) portion gives this error, "Required type: Collection Provided:Collection.
I don't understand why it's giving that error when dataList has been defined as private Collection dataList; and I'm passing in a Collection object.
To get this to not have an error I have to double cast it.
return (GenericListDTA) builder().dataList((Collection) dataList).total(dataList.size()).build();
Why do wont it recognize dataList as a Collection of DTO? Why is it defaulting to Collection