You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PositionIterator.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.layoutmgr;
  19. import java.util.Iterator;
  20. import java.util.NoSuchElementException;
  21. /**
  22. * An iterator over {@link Position} instances, that is wrapped around
  23. * another 'parent' {@link Iterator}. The parent can be either another
  24. * {@code PositionIterator}, or an iterator over {@link KnuthElement}s,
  25. * for example.<br>
  26. * The {@link #next()} method always returns a {@link Position}. The
  27. * {@link #getPos(Object)} method can be overridden in subclasses
  28. * to take care of obtaining the {@link LayoutManager} or {@link Position}
  29. * from the object returned by the parent iterator's {@code next()} method.
  30. */
  31. public class PositionIterator implements Iterator<Position> {
  32. private Iterator parentIter;
  33. private Object nextObj;
  34. private LayoutManager childLM;
  35. private boolean hasNext;
  36. /**
  37. * Construct position iterator.
  38. * @param parentIter an iterator to use as parent
  39. */
  40. public PositionIterator(Iterator parentIter) {
  41. this.parentIter = parentIter;
  42. lookAhead();
  43. //checkNext();
  44. }
  45. /** @return layout manager of next child layout manager or null */
  46. public LayoutManager getNextChildLM() {
  47. // Move to next "segment" of iterator, ie: new childLM
  48. if (childLM == null && nextObj != null) {
  49. childLM = getLM(nextObj);
  50. hasNext = true;
  51. }
  52. return childLM;
  53. }
  54. /**
  55. * @param nextObj next object from which to obtain position
  56. * @return layout manager
  57. */
  58. protected LayoutManager getLM(Object nextObj) {
  59. return getPos(nextObj).getLM();
  60. }
  61. /**
  62. * Default implementation assumes that the passed
  63. * {@code nextObj} is itself a {@link Position}, and just returns it.
  64. * Subclasses for which this is not the case, <em>must</em> provide a
  65. * suitable override this method.
  66. * @param nextObj next object from which to obtain position
  67. * @return position of next object.
  68. */
  69. protected Position getPos(Object nextObj) {
  70. if (nextObj instanceof Position) {
  71. return (Position)nextObj;
  72. }
  73. throw new IllegalArgumentException(
  74. "Cannot obtain Position from the given object.");
  75. }
  76. private void lookAhead() {
  77. if (parentIter.hasNext()) {
  78. hasNext = true;
  79. nextObj = parentIter.next();
  80. } else {
  81. endIter();
  82. }
  83. }
  84. /** @return true if not at end of sub-sequence with same child layout manager */
  85. protected boolean checkNext() {
  86. LayoutManager lm = getLM(nextObj);
  87. if (childLM == null) {
  88. childLM = lm;
  89. } else if (childLM != lm && lm != null) {
  90. // End of this sub-sequence with same child LM
  91. hasNext = false;
  92. childLM = null;
  93. return false;
  94. }
  95. return true;
  96. }
  97. /** end (reset) iterator */
  98. protected void endIter() {
  99. hasNext = false;
  100. nextObj = null;
  101. childLM = null;
  102. }
  103. /** {@inheritDoc} */
  104. public boolean hasNext() {
  105. return (hasNext && checkNext());
  106. }
  107. /** {@inheritDoc} */
  108. public Position next() throws NoSuchElementException {
  109. if (hasNext) {
  110. Position retPos = getPos(nextObj);
  111. lookAhead();
  112. return retPos;
  113. } else {
  114. throw new NoSuchElementException("PosIter");
  115. }
  116. }
  117. /** @return peek at next object */
  118. public Object peekNext() {
  119. return nextObj;
  120. }
  121. /** {@inheritDoc} */
  122. public void remove() throws UnsupportedOperationException {
  123. throw new UnsupportedOperationException("PositionIterator doesn't support remove");
  124. }
  125. }