KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > PositionIterator


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
18 /* $Id: PositionIterator.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.layoutmgr;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24
25 public abstract class PositionIterator implements Iterator JavaDoc {
26     
27     private Iterator JavaDoc parentIter;
28     private Object JavaDoc nextObj;
29     private LayoutManager childLM;
30     private boolean bHasNext;
31
32     protected PositionIterator(Iterator JavaDoc pIter) {
33         parentIter = pIter;
34         lookAhead();
35         //checkNext();
36
}
37
38     public LayoutManager getNextChildLM() {
39         // Move to next "segment" of iterator, ie: new childLM
40
if (childLM == null && nextObj != null) {
41             childLM = getLM(nextObj);
42             bHasNext = true;
43         }
44         return childLM;
45     }
46
47     protected abstract LayoutManager getLM(Object JavaDoc nextObj);
48
49     protected abstract Position getPos(Object JavaDoc nextObj);
50
51     private void lookAhead() {
52         if (parentIter.hasNext()) {
53             bHasNext = true;
54             nextObj = parentIter.next();
55         } else {
56             endIter();
57         }
58     }
59
60     protected boolean checkNext() {
61         LayoutManager lm = getLM(nextObj);
62         if (childLM == null) {
63             childLM = lm;
64         } else if (childLM != lm && lm != null) {
65             // End of this sub-sequence with same child LM
66
bHasNext = false;
67             childLM = null;
68             return false;
69         }
70         return true;
71     }
72
73     protected void endIter() {
74         bHasNext = false;
75         nextObj = null;
76         childLM = null;
77     }
78
79     public boolean hasNext() {
80         return (bHasNext && checkNext());
81     }
82
83
84     public Object JavaDoc next() throws NoSuchElementException JavaDoc {
85         if (bHasNext) {
86             Object JavaDoc retObj = getPos(nextObj);
87             lookAhead();
88             return retObj;
89         } else {
90             throw new NoSuchElementException JavaDoc("PosIter");
91         }
92     }
93
94     public Object JavaDoc peekNext() {
95         return nextObj;
96     }
97
98     public void remove() throws UnsupportedOperationException JavaDoc {
99         throw new UnsupportedOperationException JavaDoc("PositionIterator doesn't support remove");
100     }
101 }
102
103
Popular Tags