简体   繁体   English

处理集合的通用函数

[英]Generic function for handling collection

I have a function like this 我有这样的功能

    private void writeResultsIntoSheets(WritableWorkbook workbook, List<JournalEntry> summaries) throws WriteException, RowsExceededException {
        String sheetName = "Journal Entry Summary";

        WritableSheet sheet = workbook.getSheet(sheetName);
        if (sheet == null) {
            int sheetIndex = 0;
            if (workbook.getNumberOfSheets() > 0) sheetIndex = workbook.getNumberOfSheets()-1;
            sheet = workbook.createSheet(sheetName, sheetIndex); //add behind
        }

        Integer row = 0;

        int col = -1;
        sheet.addCell(new Label(++col, row, "SN"));
        sheet.addCell(new Label(++col, row, "Account Name"));
        sheet.addCell(new Label(++col, row, "Currency"));
        sheet.addCell(new Label(++col, row, "Amount"));
        sheet.addCell(new Label(++col, row, "Created On"));

        for (JournalEntry s : summaries) {
            row++;

            //write data
            col = -1;
            sheet.addCell(new Label(++col, row, (row) + "."));
            sheet.addCell(new Label(++col, row, s.getAccount().getName()));
            sheet.addCell(new Label(++col, row, s.getCurrency().getCode()));
            sheet.addCell(new Label(++col, row, String.valueOf(s.getAmount())));
            sheet.addCell(new Label(++col, row, String.valueOf(s.getCreatedOn())));

        }
    }

Basically, it will read a list of JournalEntry entity and write down the specific value into the rows in a Excel file. 基本上,它将读取JournalEntry实体的列表,并将特定值记入Excel文件中的行。 Now i have to do the same like that for other dozen entities, each will have the different values to be written down. 现在,我必须像对其他十几个实体一样进行操作,每个实体将具有不同的值要写下来。 How would i make it into a generic function and apply for the others? 我将如何使其成为通用函数并应用于其他函数?

Use the template pattern: 使用模板模式:

public abstract class SheetWriter<T> {
     private final void writeResultsIntoSheets(WritableWorkbook workbook, List<T> summaries) throws WriteException, RowsExceededException {
        String sheetName = getSheetName();

        WritableSheet sheet = workbook.getSheet(sheetName);
        if (sheet == null) {
            int sheetIndex = 0;
            if (workbook.getNumberOfSheets() > 0) sheetIndex = workbook.getNumberOfSheets()-1;
            sheet = workbook.createSheet(sheetName, sheetIndex); //add behind
        }

        int row = 0;
        writeHeadings(sheet, row);

        for (T s : summaries) {
            row++;
            writeRow(sheet, s, row);
        }
    }

    protected abstract String getSheetName();
    protected abstract void writeHeadings(WritableSheet sheet, int row) throws WriteException;
    protected abstract void writeRow(WritableSheet sheet, T item, int row) throws WriteException;
}

Then, for each entity, define a subclass of SheetWriter and implement the 3 abstract methods. 然后,为每个实体定义SheetWriter的子类并实现3个抽象方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM