gpt4 book ai didi

java - 使用抽象方法设计通用API,但在传递给匿名类时必须使用final 变量。有更好的方法来实现这一点吗?

转载 作者:行者123 更新时间:2023-12-02 08:05:03 27 4
gpt4 key购买 nike

我正在尝试创建一个在 iText 上运行的通用 API。该API的功能之一是允许用户将PDF分割为单独的页面,并允许用户在分割后将文本列表添加到每个pdf页面上。例如,一个20页的pdf,运行此过程后,我将有20个1页pdf,第一个pdf上将有一个文本000001,最后一个pdf上将有000020 pdf。因此,为了实现这一目标,我使用抽象方法,允许开发人员根据当前页码的文本格式编写代码。

public abstract class GenericText {

/**
* The X position of the text. (0, 0) is at the bottom left
*/
private float x;

/**
* The Y position of the text. (0, 0) is at the bottom left
*/
private float y;

/**
* The rotation of the text. Rotation 0, 90, 180, 270
*/
private float rotation;

/**
* <code>com.itextpdf.text.pdf.BaseFont</code>. Determine the font for the text
*/
private BaseFont font;

/**
* Determine the font size of the text
*/
private float fontSize;

/**
* This tells whether text can only be placed first page or on every page
*/
private ComponentPlacement placement;

/**
* Since the text that the user want to insert onto the Pdf might vary
* from page to page, or from logical document to logical document, we allow
* the user to write their own implementation of the text. To give the user enough
* flexibility, we give them the reference to the physical page index, the logical page index.
* @param physcialPage The actual page number that the user current looking at
* @param logicalPage A Pdf might contain multiples sub-documents, <code>logicalPage</code>
* tell the user which logical sub-document the system currently looking at
*/
public abstract String generateText(int physicalPage, int logicalPage);
...
}

PdfPrcessor.java:这是分割发生的地方

/**
* This is the main process that will split the pdf into individual page, and text to each page
*/
public void splitPdf(String inputPdf, boolean isSplit, List<GenericText> textList,
String outputDir, String baseOutputName, String outputPdfName) throws IOException, DocumentException{
...
PdfReader reader = new PdfReader(inputPdf)
PdfContentByte cb = ... ;
for(int physicalPageIndex=1 ; physicalPageIndex<=reader.getNumberOfPages(); physicalPageIndex ++)
...
//Code to split PDF. Write each page to a separate pdf. For each pdf, insert all text inside `textList` onto the pdf
...
//Insert text
if(textList != null){
for(GenericText textComponent : textList){
String text = textComponent.generateText(physicalPageIndex, logicalPageIndex);
addText(text, cb, textComponent.getFont(), textComponent.getFontSize(), textComponent.getX(), textComponent.getY(), textComponent.getRotation());
}
}
}
...
}

所以在我的主课中我会这样做,

final String printName = printNameLookup.get(baseOutputName);
final String seq = config.getPrintJobSeq();
GenericText keyline = new GenericText(90, 640, 0, arial, 7, ComponentPlacement.FIRST_PAGE){
@Override
public String generateText(int physicalPage, int logicalPage) {
return printName + seq + " " + Utils.right(String.valueOf(logicalPage), 6, '0');
}
};
textList.add(keyline);
pdfProcess.splitPdf(inputPdfPath, true, textList, outputDir, baseOutputName, outputPdfName);

这工作很棒,我认为它非常灵活,但是,printNameseq已被声明为final以便通过在generateText(intPhysicalPage,intLogicalPage)内。我如何设计它,以便它不需要 final 字段。 界面有帮助吗?我使用guava API,我可以做到这一点

ImmutableListMultimap<String, File> groups = Multimaps.index(pdfList,
new Function<File, String>(){
public String apply(File input){
String[] ids = getId(input.getName());
PackageLog pl = logProcessor.lookUp(new Long(ids[0]), ids[1]);
String printName = printNameLookup.get(getPackageName(pl, s));
}
});

logProcessorprintNameLookup 不是 final,我喜欢他们的设计方式,我现在正在阅读他们的源代码,但这需要有时,任何具有设计知识的专家都可以给我一些启发吗?

最佳答案

将它们复制到最终变量中并使用它们。

String printName = printNameLookup.get(baseOutputName);
String seq = config.getPrintJobSeq();
// use these in the anonymous class.
final String finalPrintName = printName;
final String finalSeq = seq;

或者使用数组

final String[] printName = { printNameLookup.get(baseOutputName) };
final String[] seq = { config.getPrintJobSeq() };

// use printName[0] and seq[0] everywhere.

关于java - 使用抽象方法设计通用API,但在传递给匿名类时必须使用final 变量。有更好的方法来实现这一点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8317703/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com