如何实现通用

时间:2018-07-20 07:16:10

标签: java generics

我有一个需要传递给不同对象的方法,这是通用方法,并且这四个对象也存在于同一类中,我不想复制和粘贴此方法四次。

我想使其通用,以便可以接受任何争论,请向我建议我如何实现通用概念。

这是我要通用的代码:

 private <T> void downloadPDFFile(T acrApproval, HttpServletResponse response) throws Exception {
        File downloadFile = new File(fileUploadUtility.getBasePath() + File.separator + acrApproval.getPdfPath());
        FileInputStream inputStream = new FileInputStream(downloadFile);
        // get MIME type of the file
        String mimeType = context.getMimeType(fileUploadUtility.getBasePath() + File.separator + acrApproval.getPdfPath());
        if (mimeType == null) {
            // set to binary type if MIME mapping not found
            mimeType = "application/octet-stream";
        }
        // set content attributes for the response
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());

        // set headers for the response
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"",
                "acr_report_" + acrApproval.getAcrApprovalId() + ".pdf");
        response.setHeader(headerKey, headerValue);

        // get output stream of the response
        OutputStream outStream = response.getOutputStream();

        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;

        // write bytes read from the input stream into the output stream
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }

        inputStream.close();
        outStream.close();
    }

现在为什么会给我一个错误,找不到符号getPdfPath()getPdfPath()AcrApproval的类型

错误

File downloadFile = new File(fileUploadUtility.getBasePath() + File.separator + acrApproval.getPdfPath());

String mimeType = context.getMimeType(fileUploadUtility.getBasePath() + File.separator + acrApproval.getPdfPath());

String headerValue = String.format("attachment; filename=\"%s\"",
                    "acr_report_" + acrApproval.getAcrApprovalId() + ".pdf");

四个检查

if (acrApproval instanceof AcrApproval) {
            headerValue = String.format("attachment; filename=\"%s\"",
                    "acr_report_" + acrApproval.getId() + ".pdf");
        } else if (acrApproval instanceof AcrReviewOfficer) {
            headerValue = String.format("attachment; filename=\"%s\"",
                    "acr_report_" + acrApproval.getId() + ".pdf");
        } else if (acrApproval instanceof AcrRoA) {
            headerValue = String.format("attachment; filename=\"%s\"",
                    "acr_report_" + acrApproval.getId() + ".pdf");
        } else if (acrApproval instanceof AcrReport) {
            headerValue = String.format("attachment; filename=\"%s\"",
                    "acr_report_" + acrApproval.getId() + ".pdf");
        }

acrApproval是AcrPdfProvider的类型,它是由所有四个类实现的接口

1 个答案:

答案 0 :(得分:1)

看到您使用ArcApproval中的2种方法,您可能希望将其提取到接口中。之所以称为PdfDataProvider,是因为我不知道您的其他课程:

public interface PdfDataProvider{
    String getPdfPath();
    String getId();
}

然后让您的类实现此接口。

现在,您只需接受该接口作为参数即可,因此无需泛型:

private void downloadPDFFile(PdfProvider provider, HttpServletResponse response) throws Exception { /* ... */ }

如果您仍然想使用泛型并且碰巧使用了 Java-8 + ,则可以将方法参数列表扩展为2个参数,一个用于获取pdfPath,另一个用于ID。添加泛型时,它看起来可能像这样:

private <T> void downloadPdfFile(T t, Function<T, String> pdfPathProvider, 
                                 Function<T, String> idProvider, HttpServletResponse response) throws Exception {/* ... */}

要获取pdfPath,您只需使用参数pdfPathProvider调用t的apply方法。对于id也是一样。

要调用该方法,您必须提供2个新函数:

downloadPdfFile(acrApproval, AcrApproval::getPdfPath, AcrApproval::getArcApprovalId, response);