From 0f971bd07ebceadcbca7b02a803f5cafcd26be7d Mon Sep 17 00:00:00 2001 From: "Andreas L. Delmelle" Date: Sun, 6 Feb 2011 21:13:50 +0000 Subject: [PATCH] Code fixups, type safety, etc. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1067762 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/layoutmgr/PageBreakingAlgorithm.java | 104 ++++++++++-------- 1 file changed, 57 insertions(+), 47 deletions(-) diff --git a/src/java/org/apache/fop/layoutmgr/PageBreakingAlgorithm.java b/src/java/org/apache/fop/layoutmgr/PageBreakingAlgorithm.java index 7e54cbe43..4b6638300 100644 --- a/src/java/org/apache/fop/layoutmgr/PageBreakingAlgorithm.java +++ b/src/java/org/apache/fop/layoutmgr/PageBreakingAlgorithm.java @@ -43,16 +43,16 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { private PageProvider pageProvider; private PageBreakingLayoutListener layoutListener; /** List of PageBreakPosition elements. */ - private LinkedList pageBreaks = null; + private LinkedList pageBreaks = null; /** Footnotes which are cited between the currently considered active node (previous * break) and the current considered break. Its type is * List<List<KnuthElement>>, it contains the sequences of KnuthElement * representing the footnotes bodies. */ - private List footnotesList = null; + private List> footnotesList = null; /** Cumulated bpd of unhandled footnotes. */ - private List lengthList = null; + private List lengthList = null; /** Length of all the footnotes which will be put on the current page. */ private int totalFootnotesLength = 0; /** @@ -64,13 +64,9 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { /** True if footnote citations have been met since the beginning of the page sequence. */ private boolean footnotesPending = false; - /** - * True if the elements met after the previous break point contain footnote citations. - */ + /** True if the elements met after the previous break point contain footnote citations. */ private boolean newFootnotes = false; - /** - * Index of the first footnote met after the previous break point. - */ + /** Index of the first footnote met after the previous break point. */ private int firstNewFootnoteIndex = 0; /** Index of the last footnote inserted on the current page. */ private int footnoteListIndex = 0; @@ -206,6 +202,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected void initialize() { super.initialize(); insertedFootnotesLength = 0; @@ -214,11 +211,12 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** - * {@inheritDoc} * Overridden to defer a part to the next page, if it * must be kept within one page, but is too large to fit in * the last column. + * {@inheritDoc} */ + @Override protected KnuthNode recoverFromTooLong(KnuthNode lastTooLong) { if (log.isDebugEnabled()) { @@ -256,6 +254,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { * @param node2 The other knuth node. * @return the node with the least demerit. */ + @Override protected KnuthNode compareNodes(KnuthNode node1, KnuthNode node2) { /* if either node is null, return the other one */ @@ -281,6 +280,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected KnuthNode createNode(int position, // CSOK: ParameterNumber int line, int fitness, int totalWidth, int totalStretch, int totalShrink, @@ -294,6 +294,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected KnuthNode createNode(int position, int line, int fitness, int totalWidth, int totalStretch, int totalShrink) { return new KnuthPageNode(position, line, fitness, @@ -307,11 +308,11 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** - * {@inheritDoc} * Page-breaking specific handling of the given box. Currently it adds the footnotes * cited in the given box to the list of to-be-handled footnotes. - * @param box a block-level element possibly containing foonotes citations + * {@inheritDoc} */ + @Override protected void handleBox(KnuthBox box) { super.handleBox(box); if (box instanceof KnuthBlockBox @@ -325,11 +326,12 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** - * {@inheritDoc} * Overridden to consider penalties with value {@link KnuthElement#INFINITE} * as legal break-points, if the current keep-context allows this * (a keep-*.within-page="always" constraint still permits column-breaks) + * {@inheritDoc} */ + @Override protected void handlePenaltyAt(KnuthPenalty penalty, int position, int allowedBreaks) { super.handlePenaltyAt(penalty, position, allowedBreaks); @@ -352,12 +354,12 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { * @param elementLists list of KnuthElement sequences corresponding to the footnotes * bodies */ - private void handleFootnotes(List elementLists) { + private void handleFootnotes(List> elementLists) { // initialization if (!footnotesPending) { footnotesPending = true; - footnotesList = new ArrayList(); - lengthList = new ArrayList(); + footnotesList = new ArrayList>(); + lengthList = new ArrayList(); totalFootnotesLength = 0; } if (!newFootnotes) { @@ -366,9 +368,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } // compute the total length of the footnotes - for (Iterator elementListsIterator = elementLists.iterator(); - elementListsIterator.hasNext();) { - final List noteList = (List) elementListsIterator.next(); + for (List noteList : elementLists) { //Space resolution (Note: this does not respect possible stacking constraints //between footnotes!) @@ -376,23 +376,23 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { int noteLength = 0; footnotesList.add(noteList); - for (Iterator noteListIterator = noteList.iterator(); - noteListIterator.hasNext();) { - final KnuthElement element = (KnuthElement) noteListIterator.next(); + for (KnuthElement element : noteList) { if (element.isBox() || element.isGlue()) { noteLength += element.getWidth(); } } int prevLength = (lengthList == null || lengthList.isEmpty()) ? 0 - : ((Integer) ListUtil.getLast(lengthList)).intValue(); - //TODO: replace with Integer.valueOf() once we switch to Java 5 - lengthList.add(new Integer(prevLength + noteLength)); + : ListUtil.getLast(lengthList); + if (lengthList != null) { + lengthList.add(prevLength + noteLength); + } totalFootnotesLength += noteLength; } } /** {@inheritDoc} */ + @Override protected int restartFrom(KnuthNode restartingNode, int currentIndex) { int returnValue = super.restartFrom(restartingNode, currentIndex); newFootnotes = false; @@ -410,14 +410,14 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { return returnValue; } - private void resetFootnotes(List elementLists) { + private void resetFootnotes(List> elementLists) { for (int i = 0; i < elementLists.size(); i++) { - /*LinkedList removedList = (LinkedList)*/ListUtil.removeLast(footnotesList); + ListUtil.removeLast(footnotesList); ListUtil.removeLast(lengthList); // update totalFootnotesLength if (!lengthList.isEmpty()) { - totalFootnotesLength = ((Integer) ListUtil.getLast(lengthList)).intValue(); + totalFootnotesLength = ListUtil.getLast(lengthList); } else { totalFootnotesLength = 0; } @@ -429,6 +429,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected void considerLegalBreak(KnuthElement element, int elementIdx) { if (element.isPenalty()) { int breakClass = ((KnuthPenalty) element).getBreakClass(); @@ -457,6 +458,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected boolean elementCanEndLine(KnuthElement element, int line, int difference) { if (!(element.isPenalty()) || pageProvider == null) { return true; @@ -489,11 +491,12 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected int computeDifference(KnuthNode activeNode, KnuthElement element, int elementIndex) { KnuthPageNode pageNode = (KnuthPageNode) activeNode; int actualWidth = totalWidth - pageNode.totalWidth; - int footnoteSplit = 0; + int footnoteSplit; boolean canDeferOldFN; if (element.isPenalty()) { actualWidth += element.getWidth(); @@ -562,6 +565,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { * @param node active node for the preceding page break * @param contentElementIndex index of the Knuth element considered for the * current page break + * @return true if footnotes can be deferred */ private boolean canDeferOldFootnotes(KnuthPageNode node, int contentElementIndex) { return (noBreakBetween(node.position, contentElementIndex) @@ -668,8 +672,8 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { // together with all previous, not yet inserted footnotes; // but if this is not possible, try adding as much content as possible int splitLength = 0; - ListIterator noteListIterator = null; - KnuthElement element = null; + ListIterator noteListIterator; + KnuthElement element; boolean somethingAdded = false; // prevListIndex and prevElementIndex points to the last footnote element @@ -687,16 +691,14 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { if (footnotesList.size() - 1 > listIndex) { // add the previous footnotes: these cannot be broken or deferred if (!canDeferOldFootnotes && newFootnotes && firstNewFootnoteIndex > 0) { - splitLength = ((Integer) lengthList.get(firstNewFootnoteIndex - 1)).intValue() - - prevLength; + splitLength = lengthList.get(firstNewFootnoteIndex - 1) - prevLength; listIndex = firstNewFootnoteIndex; elementIndex = 0; } // try adding the new footnotes - while (((Integer) lengthList.get(listIndex)).intValue() - prevLength + while (lengthList.get(listIndex) - prevLength <= availableLength) { - splitLength = ((Integer) lengthList.get(listIndex)).intValue() - - prevLength; + splitLength = lengthList.get(listIndex) - prevLength; somethingAdded = true; listIndex++; elementIndex = 0; @@ -727,7 +729,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { // all footnotes, and we have already tried (and failed) to insert // this whole footnote, the while loop will never reach the end // of the note sequence - element = (KnuthElement) noteListIterator.next(); + element = noteListIterator.next(); if (element.isBox()) { // element is a box splitLength += element.getWidth(); @@ -774,6 +776,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected double computeAdjustmentRatio(KnuthNode activeNode, int difference) { // compute the adjustment ratio if (difference > 0) { @@ -804,6 +807,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected double computeDemerits(KnuthNode activeNode, KnuthElement element, int fitnessClass, double r) { double demerits = 0; @@ -856,6 +860,8 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { return demerits; } + /** {@inheritDoc} */ + @Override protected void finish() { for (int i = startLine; i < endLine; i++) { for (KnuthPageNode node = (KnuthPageNode) getNode(i); @@ -880,7 +886,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { // create pages containing the remaining footnote bodies while (insertedFootnotesLength < totalFootnotesLength) { - final int tmpLength = ((Integer) lengthList.get(footnoteListIndex)).intValue(); + final int tmpLength = lengthList.get(footnoteListIndex); // try adding some more content if ((tmpLength - insertedFootnotesLength) <= availableBPD) { // add a whole footnote @@ -925,7 +931,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { * @return a list of {@link PageBreakPosition} elements * corresponding to the computed page- and column-breaks */ - public LinkedList getPageBreaks() { + public LinkedList getPageBreaks() { return pageBreaks; } @@ -937,7 +943,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { */ public void insertPageBreakAsFirst(PageBreakPosition pageBreak) { if (pageBreaks == null) { - pageBreaks = new LinkedList(); + pageBreaks = new LinkedList(); } pageBreaks.addFirst(pageBreak); } @@ -948,19 +954,19 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { * whole content should be painted as one part. */ public void removeAllPageBreaks() { - if (pageBreaks == null) { + if (pageBreaks == null || pageBreaks.isEmpty()) { return; } - while (pageBreaks.size() > 1) { - pageBreaks.removeFirst(); - } + pageBreaks.subList(0, pageBreaks.size() - 1).clear(); } /** {@inheritDoc} */ + @Override public void updateData1(int total, double demerits) { } /** {@inheritDoc} */ + @Override public void updateData2(KnuthNode bestActiveNode, KnuthSequence sequence, int total) { @@ -1029,6 +1035,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected int filterActiveNodes() { // leave only the active node with fewest total demerits KnuthNode bestActiveNode = null; @@ -1058,8 +1065,8 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { * @param index the index in the list of footnotes * @return the element-list */ - protected final List getFootnoteList(int index) { - return (List) footnotesList.get(index); + protected final List getFootnoteList(int index) { + return footnotesList.get(index); } /** @return the associated top-level formatting object. */ @@ -1068,6 +1075,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected int getLineWidth(int line) { int bpd; if (pageProvider != null) { @@ -1097,11 +1105,13 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } /** {@inheritDoc} */ + @Override protected int getIPDdifference() { return ipdDifference; } /** {@inheritDoc} */ + @Override protected int handleIpdChange() { log.trace("Best node for ipd change:" + bestNodeForIPDChange); // TODO finish() @@ -1123,6 +1133,7 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { * @param line number of the line ending at the node's corresponding breakpoint * @param node the active node to add */ + @Override protected void addNode(int line, KnuthNode node) { if (node.position < par.size() - 1 && line > 0 && (ipdDifference = compareIPDs(line - 1)) != 0) { // CSOK: InnerAssignment @@ -1153,5 +1164,4 @@ class PageBreakingAlgorithm extends BreakingAlgorithm { } return pageProvider.compareIPDs(line); } - } -- 2.39.5