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    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed 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 java.util.Collection;
27  import java.util.Collections;
28  import java.util.Iterator;
29  import javax.jcr.nodetype.NodeType;
30  import javax.jcr.nodetype.NodeTypeIterator;
31  import net.jcip.annotations.Immutable;
32  
33  /**
34   * Type-safe {@link Iterator} implementation for NodeTypes, as per the JCR specification.
35   */
36  @Immutable
37  final class JcrNodeTypeIterator implements NodeTypeIterator {
38  
39      private int size;
40      private int position;
41      private Iterator<NodeType> iterator;
42  
43      JcrNodeTypeIterator( Collection<? extends NodeType> values ) {
44          this.iterator = Collections.unmodifiableCollection(values).iterator();
45          this.size = values.size();
46          this.position = 0;
47  
48      }
49  
50      /**
51       * {@inheritDoc}
52       * 
53       * @see javax.jcr.nodetype.NodeTypeIterator#nextNodeType()
54       */
55      public NodeType nextNodeType() {
56          // TODO: Does this really need to return a copy of the node type to prevent manipulation?
57          position++;
58          return iterator.next();
59      }
60  
61      /**
62       * {@inheritDoc}
63       * 
64       * @see javax.jcr.RangeIterator#getPosition()
65       */
66      public long getPosition() {
67          return position;
68      }
69  
70      /**
71       * {@inheritDoc}
72       * 
73       * @see javax.jcr.RangeIterator#getSize()
74       */
75      public long getSize() {
76          return size;
77      }
78  
79      /**
80       * {@inheritDoc}
81       * 
82       * @see javax.jcr.RangeIterator#skip(long)
83       */
84      public void skip( long count ) {
85          position += count;
86          while (count-- > 0)
87              iterator.next();
88      }
89  
90      /**
91       * {@inheritDoc}
92       * 
93       * @see java.util.Iterator#hasNext()
94       */
95      public boolean hasNext() {
96          return iterator.hasNext();
97      }
98  
99      /**
100      * {@inheritDoc}
101      * 
102      * @see java.util.Iterator#next()
103      */
104     public Object next() {
105         position++;
106         return iterator.next();
107     }
108 
109     /**
110      * {@inheritDoc}
111      * 
112      * @see java.util.Iterator#remove()
113      */
114     public void remove() {
115         throw new UnsupportedOperationException("Node types cannot be removed through their iterator");
116     }
117 
118 }