View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * Unless otherwise indicated, all code in ModeShape is licensed
10   * to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jcr;
25  
26  import javax.jcr.Node;
27  import javax.jcr.NodeIterator;
28  import org.modeshape.common.util.CheckArg;
29  
30  /**
31   * A concrete {@link NodeIterator} that returns the first node and then delegates to the supplied node iterator.
32   */
33  class JcrNodeIterator implements NodeIterator {
34  
35      private final long size;
36      private final AbstractJcrNode firstNode;
37      private final NodeIterator nextNodes;
38      private boolean visitedFirst = false;
39  
40      protected JcrNodeIterator( AbstractJcrNode firstNode,
41                                 NodeIterator nextNodes ) {
42          assert firstNode != null;
43          assert nextNodes != null;
44          this.firstNode = firstNode;
45          this.nextNodes = nextNodes;
46          this.size = 1 + nextNodes.getSize();
47      }
48  
49      /**
50       * {@inheritDoc}
51       * 
52       * @see javax.jcr.NodeIterator#nextNode()
53       */
54      public Node nextNode() {
55          if (!visitedFirst) {
56              visitedFirst = true;
57              return firstNode;
58          }
59          return nextNodes.nextNode();
60      }
61  
62      /**
63       * {@inheritDoc}
64       * 
65       * @see javax.jcr.RangeIterator#getPosition()
66       */
67      public long getPosition() {
68          return visitedFirst ? nextNodes.getPosition() + 1 : 0;
69      }
70  
71      /**
72       * {@inheritDoc}
73       * 
74       * @see javax.jcr.RangeIterator#getSize()
75       */
76      public long getSize() {
77          return size;
78      }
79  
80      /**
81       * {@inheritDoc}
82       * 
83       * @see javax.jcr.RangeIterator#skip(long)
84       */
85      public void skip( long skipNum ) {
86          CheckArg.isNonNegative(skipNum, "skipNum");
87          if (skipNum == 0L) return;
88          if (visitedFirst) {
89              nextNodes.skip(skipNum);
90          }
91          visitedFirst = true;
92          nextNodes.skip(skipNum - 1);
93      }
94  
95      /**
96       * {@inheritDoc}
97       * 
98       * @see java.util.Iterator#hasNext()
99       */
100     public boolean hasNext() {
101         return visitedFirst ? nextNodes.hasNext() : true;
102     }
103 
104     /**
105      * {@inheritDoc}
106      * 
107      * @see java.util.Iterator#next()
108      */
109     public Object next() {
110         return nextNode();
111     }
112 
113     /**
114      * {@inheritDoc}
115      * 
116      * @see java.util.Iterator#remove()
117      */
118     public void remove() {
119         throw new UnsupportedOperationException();
120     }
121 
122 }