0

I have a project where I am currently loading the records from the excel file to the database using from org.apache.poi

I have 3 kinds of files that I am loading into different Dto classes. Two of these dtos shares the same base class (they have common attributes)

@Getter
@Setter
@ToString
public class PpyRecordDTO {

private String units;
private Double quantity;

}

@Getter
@Setter
@ToString
public class FirstRecordDTO extends RecordDTO {
private String normDescription;
private String relatedUnits;

}


@Getter
@Setter
@ToString
public class SecondRecordDTO extends RecordDTO{

private String normName;
}

@Getter
@Setter
@ToString
public class ThirdRecordDTO {

private String code;
}

The ThirdRecordDto class has a unique attribute and no shared attributes with the base dto class RecordDTO

I want to return from this method the base class : RecordDto (But ThirdRecordDTO cannot extend it since there are no common fields)

    public static List<?extends RecordDTO> readPpyExcelFile(MultipartFile file, SourceType sourceType){
    //TODO: making readPpyExcelFile generic
    try {
        Workbook workbook = WorkbookFactory.create(new BufferedInputStream(file.getInputStream()));

        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rows = sheet.iterator();

        List<? extends RecordDTO> lstRecords = processRecords(rows, sourceType);

        // Close WorkBook
        workbook.close();

        return lstRecords;
    } catch(ApiGenericException apiException){
        throw new ApiGenericException(apiException.getStatus(), apiException.getMessage(), apiException.getErrorType());
    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new ApiGenericException(HttpStatus.INTERNAL_SERVER_ERROR,"Enable while loading the file", ApiErrorType.Unhandled
        );
    }
}

Is there a way to make the dto ThirdRecordDto also be returned or inherit from an abstract class which is shared by the other dtos in order to return the type <? extends List from this method?

4
  • 1
    You might consider using a "empty" interface to define conformance Commented Jun 12, 2021 at 1:08
  • Thank you @MadProgrammer for your response, do you have any examples or links? Commented Jun 12, 2021 at 1:12
  • 2
    You could make all the classes to implement an empty interface and use that as the generic class instead of the record class. I'm not a big of this since there is nothing shared that would make sense using an interface. I believe something is wrong when you try to have a superclass with children that share nothing Commented Jun 12, 2021 at 1:14
  • 1
    Look at Serializable - it defines conformance, but no actual functionality Commented Jun 12, 2021 at 1:15

1 Answer 1

1

In general you can go with something like this:

public interface Ppy {
 // common methods if any
}

public class PpyRecordDTO implements Ppy{...}
public class FirstRecordDTO extends PpyRecordDTO {...} // so that it also implements Ppy interface
public class SecondRecordDTO extends PpyRecordDTO {...} // the same as above
public class ThirdRecordDTO implements Ppy {...} // Note, it doesn't extend PpyRecordDTO but implements the interface

Now in the method, it's possible to:

 public static List<Ppy> readPpyExcelFile(MultipartFile file, SourceType sourceType){...}

This will work, however, you should ask yourself the following: what will do the code that will call this method, namely how it will differentiate between the different implementations? If the interface has a common method that makes sense for all implementations - fine, it will be able to call the method. For example, if it has a method like render(Page) or something, the code might be:

List<Ppy> ppis = readPpyExcelFile(...);
Page page = ...
for(Ppy ppi : ppis) {
    ppi.render(page);
} 

However, if the interface doesn't have any common methods - it won't help much. Inheritance is used when the child object can be viewed as a specialization of Parent (so that the child "is a" parent). So think whether inheritance is really appropriate here, assuming ThirdRecordDTO doesn't have anything in common with the rest of the classes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for this explanation. Yes I will think about it. This method that I am using has common shared code with other files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.