From: Andreas L. Delmelle Date: Sat, 5 Feb 2011 00:13:18 +0000 (+0000) Subject: Refactoring PositionIterator Step 1: make the class instantiable, provide default... X-Git-Tag: fop-1_1rc1old~330 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=186dd12cec2194d3187146919c09246cf410844e;p=xmlgraphics-fop.git Refactoring PositionIterator Step 1: make the class instantiable, provide default implementations for getLM() and getPos(), and make the constructor public git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1067353 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/layoutmgr/KnuthPossPosIter.java b/src/java/org/apache/fop/layoutmgr/KnuthPossPosIter.java index c4c4a466c..71790ec82 100644 --- a/src/java/org/apache/fop/layoutmgr/KnuthPossPosIter.java +++ b/src/java/org/apache/fop/layoutmgr/KnuthPossPosIter.java @@ -51,6 +51,7 @@ public class KnuthPossPosIter extends PositionIterator { // Check position < endPos /** {@inheritDoc} */ + @Override protected boolean checkNext() { if (iterCount > 0) { return super.checkNext(); @@ -61,6 +62,7 @@ public class KnuthPossPosIter extends PositionIterator { } /** {@inheritDoc} */ + @Override public Position next() { --iterCount; return super.next(); @@ -75,11 +77,13 @@ public class KnuthPossPosIter extends PositionIterator { } /** {@inheritDoc} */ + @Override protected LayoutManager getLM(Object nextObj) { return ((ListElement) nextObj).getLayoutManager(); } /** {@inheritDoc} */ + @Override protected Position getPos(Object nextObj) { return ((ListElement) nextObj).getPosition(); } diff --git a/src/java/org/apache/fop/layoutmgr/PositionIterator.java b/src/java/org/apache/fop/layoutmgr/PositionIterator.java index f88142613..b2bf404ff 100644 --- a/src/java/org/apache/fop/layoutmgr/PositionIterator.java +++ b/src/java/org/apache/fop/layoutmgr/PositionIterator.java @@ -28,12 +28,11 @@ import java.util.NoSuchElementException; * {@code PositionIterator}, or an iterator over {@link KnuthElement}s, * for example.
* The {@link #next()} method always returns a {@link Position}. The - * protected {@link #getLM(Object)} and {@link #getPos(Object)} methods - * must be overridden in subclasses to take care of obtaining the - * {@link LayoutManager} or {@link Position} from the object returned - * by the parent iterator's {@code next()} method. + * {@link #getPos(Object)} method can be overridden in subclasses + * to take care of obtaining the {@link LayoutManager} or {@link Position} + * from the object returned by the parent iterator's {@code next()} method. */ -public abstract class PositionIterator implements Iterator { +public class PositionIterator implements Iterator { private Iterator parentIter; private Object nextObj; @@ -44,7 +43,7 @@ public abstract class PositionIterator implements Iterator { * Construct position iterator. * @param parentIter an iterator to use as parent */ - protected PositionIterator(Iterator parentIter) { + public PositionIterator(Iterator parentIter) { this.parentIter = parentIter; lookAhead(); //checkNext(); @@ -64,13 +63,25 @@ public abstract class PositionIterator implements Iterator { * @param nextObj next object from which to obtain position * @return layout manager */ - protected abstract LayoutManager getLM(Object nextObj); + protected LayoutManager getLM(Object nextObj) { + return getPos(nextObj).getLM(); + } /** + * Default implementation assumes that the passed + * {@code nextObj} is itself a {@link Position}, and just returns it. + * Subclasses for which this is not the case, must provide a + * suitable override this method. * @param nextObj next object from which to obtain position - * @return position of next object + * @return position of next object. */ - protected abstract Position getPos(Object nextObj); + protected Position getPos(Object nextObj) { + if (nextObj instanceof Position) { + return (Position)nextObj; + } + throw new IllegalArgumentException( + "Cannot obtain Position from the given object."); + } private void lookAhead() { if (parentIter.hasNext()) {