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: LMiter.java 1610839 2014-07-15 20:25:58Z vhennebert $ */
19 
20 package org.apache.fop.layoutmgr;
21 
22 import java.util.List;
23 import java.util.ListIterator;
24 import java.util.NoSuchElementException;
25 
26 /** An iterator for layout managers. */
27 public class LMiter implements ListIterator<LayoutManager> {
28 
29     /** list of layout managers */
30     protected List<LayoutManager> listLMs;
31     /** current position in iteration */
32     protected int curPos;
33     /** The LayoutManager to which this LMiter is attached **/
34     private LayoutManager lp;
35 
36     /**
37      * Construct a layout manager iterator.
38      * @param lp the associated layout manager (parent)
39      */
LMiter(LayoutManager lp)40     public LMiter(LayoutManager lp) {
41         this.lp = lp;
42         listLMs = lp.getChildLMs();
43     }
44 
45     /** {@inheritDoc} */
hasNext()46     public boolean hasNext() {
47         return (curPos < listLMs.size()) || lp.createNextChildLMs(curPos);
48     }
49 
50     /** {@inheritDoc} */
hasPrevious()51     public boolean hasPrevious() {
52         return (curPos > 0);
53     }
54 
55     /** {@inheritDoc} */
previous()56     public LayoutManager previous() throws NoSuchElementException {
57         if (curPos > 0) {
58             return listLMs.get(--curPos);
59         } else {
60             throw new NoSuchElementException();
61         }
62     }
63 
64     /** {@inheritDoc} */
next()65     public LayoutManager next() throws NoSuchElementException {
66         if (curPos < listLMs.size()) {
67             return listLMs.get(curPos++);
68         } else {
69             throw new NoSuchElementException();
70         }
71     }
72 
73     /** {@inheritDoc} */
remove()74      public void remove() throws NoSuchElementException {
75         if (curPos > 0) {
76             listLMs.remove(--curPos);
77             // Note: doesn't actually remove it from the base!
78         } else {
79             throw new NoSuchElementException();
80         }
81     }
82 
83 
84     /** {@inheritDoc} */
add(LayoutManager lm)85    public void add(LayoutManager lm) throws UnsupportedOperationException {
86         throw new UnsupportedOperationException("LMiter doesn't support add");
87     }
88 
89     /** {@inheritDoc} */
set(LayoutManager lm)90     public void set(LayoutManager lm) throws UnsupportedOperationException {
91         throw new UnsupportedOperationException("LMiter doesn't support set");
92     }
93 
94     /** {@inheritDoc} */
nextIndex()95     public int nextIndex() {
96         return curPos;
97     }
98 
99     /** {@inheritDoc} */
previousIndex()100     public int previousIndex() {
101         return curPos - 1;
102     }
103 
104 }
105