KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > impl > llom > traverse > OMChildrenIterator


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.om.impl.llom.traverse;
17
18 import org.apache.axis2.om.OMException;
19 import org.apache.axis2.om.OMNode;
20
21 import java.util.Iterator JavaDoc;
22
23 /**
24  * Class OMChildrenIterator
25  */

26 public class OMChildrenIterator implements Iterator JavaDoc {
27     /**
28      * Field currentChild
29      */

30     protected OMNode currentChild;
31
32     /**
33      * Field lastChild
34      */

35     protected OMNode lastChild;
36
37     /**
38      * Field nextCalled
39      */

40     protected boolean nextCalled = false;
41
42     /**
43      * Field removeCalled
44      */

45     protected boolean removeCalled = false;
46
47     /**
48      * Constructor OMChildrenIterator
49      *
50      * @param currentChild
51      */

52     public OMChildrenIterator(OMNode currentChild) {
53         this.currentChild = currentChild;
54     }
55
56     /**
57      * Removes from the underlying collection the last element returned by the
58      * iterator (optional operation). This method can be called only once per
59      * call to <tt>next</tt>. The behavior of an iterator is unspecified if
60      * the underlying collection is modified while the iteration is in
61      * progress in any way other than by calling this method.
62      *
63      * @throws UnsupportedOperationException if the <tt>remove</tt>
64      * operation is not supported by this Iterator.
65      * @throws IllegalStateException if the <tt>next</tt> method has not
66      * yet been called, or the <tt>remove</tt> method has already
67      * been called after the last call to the <tt>next</tt>
68      * method.
69      */

70     public void remove() {
71         if (!nextCalled) {
72             throw new IllegalStateException JavaDoc(
73                     "next method has not yet being called");
74         }
75         if (removeCalled) {
76             throw new IllegalStateException JavaDoc("remove has already being called");
77         }
78         removeCalled = true;
79
80         // since this acts on the last child there is no need to mess with the current child
81
if (lastChild == null) {
82             throw new OMException("cannot remove a child at this stage!");
83         }
84         lastChild.detach();
85     }
86
87     /**
88      * Returns <tt>true</tt> if the iteration has more elements. (In other
89      * words, returns <tt>true</tt> if <tt>next</tt> would return an element
90      * rather than throwing an exception.)
91      *
92      * @return <tt>true</tt> if the iterator has more elements.
93      */

94     public boolean hasNext() {
95         return (currentChild != null);
96     }
97
98     /**
99      * Returns the next element in the iteration.
100      *
101      * @return the next element in the iteration.
102      * @throws java.util.NoSuchElementException
103      * iteration has no more elements.
104      */

105     public Object JavaDoc next() {
106         nextCalled = true;
107         removeCalled = false;
108         if (hasNext()) {
109             lastChild = currentChild;
110             currentChild = currentChild.getNextSibling();
111             return lastChild;
112         }
113         return null;
114     }
115 }
116
Popular Tags