KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > util > EntityCollection


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.jcr.impl.util;
7
8 import java.util.Iterator JavaDoc;
9 import javax.jcr.NodeIterator;
10 import javax.jcr.PropertyIterator;
11 import javax.jcr.nodetype.NodeTypeIterator;
12 import javax.jcr.Node;
13 import javax.jcr.Item;
14 import javax.jcr.Property;
15 import javax.jcr.nodetype.NodeType;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Collection JavaDoc;
19
20
21 import javax.jcr.StringIterator;
22 import org.exoplatform.services.jcr.impl.core.ItemIterator;
23
24 /**
25  * Created by The eXo Platform SARL .
26  *
27  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
28  * @version $Id: EntityCollection.java,v 1.2 2004/07/08 23:36:50 benjmestrallet Exp $
29  */

30
31 public class EntityCollection
32     implements NodeIterator, PropertyIterator, ItemIterator,
33     NodeTypeIterator, StringIterator {
34
35   private Iterator JavaDoc iter;
36   private List JavaDoc list;
37   private long pos;
38
39   public EntityCollection(List JavaDoc list) {
40     if (list == null)
41       this.list = new ArrayList JavaDoc();
42     else
43       this.list = list;
44
45     this.iter = list.iterator();
46     this.pos = 0;
47   }
48
49   public EntityCollection() {
50     this.list = new ArrayList JavaDoc();
51     this.iter = list.iterator();
52     this.pos = 0;
53   }
54
55
56   /**
57    * @see NodeIterator#nextNode()
58    */

59   public Node nextNode() {
60     pos++;
61     return (Node) iter.next();
62   }
63
64   /**
65    * @see RangeIterator#skip(int)
66    */

67   public void skip(int skipNum) {
68     pos += skipNum;
69     while (skipNum-- > 0) {
70       iter.next();
71     }
72   }
73
74   /**
75    * Returns the number of elements in the iterator.
76    * If this information is unavailable, returns -1.
77    *
78    * @return a long
79    */

80   public long getSize() {
81     return list.size();
82   }
83
84   /**
85    * Returns the current position within the iterator. The number
86    * returned is the 0-based index of the next element in the iterator,
87    * i.e. the one that will be returned on the subsequent <code>next</code> call.
88    * <p/>
89    * Note that this method does not check if there is a next element,
90    * i.e. an empty iterator will always return 0.
91    *
92    * @return a long
93    */

94   public long getPos() {
95     return pos;
96   }
97
98   /**
99    * @see ElementIterator#nextElement()
100    */

101   public Item nextItem() {
102     pos++;
103     return (Item) iter.next();
104   }
105
106   /**
107    * @see Iterator#hasNext()
108    */

109   public boolean hasNext() {
110     return iter.hasNext();
111   }
112
113   /**
114    * @see Iterator#next()
115    */

116   public Object JavaDoc next() {
117     pos++;
118     return iter.next();
119   }
120
121   /**
122    * @see Iterator#remove()
123    */

124   public void remove() {
125     iter.remove();
126   }
127
128   /**
129    * @see PropertyIterator#nextProperty()
130    */

131   public Property nextProperty() {
132     pos++;
133     return (Property) iter.next();
134   }
135
136   /**
137    * Returns the next <code>String</code> in the iteration.
138    *
139    * @return the next <code>String</code> in the iteration.
140    * @throws java.util.NoSuchElementException
141    * if iteration has no more <code>String</code>s.
142    */

143   public String JavaDoc nextString() {
144     pos++;
145     return (String JavaDoc) iter.next();
146   }
147
148
149   /**
150    *
151    */

152   public NodeType nextNodeType() {
153     pos++;
154     return (NodeType) iter.next();
155   }
156
157   /**
158    * @see HitIterator#nextHit()
159    */

160 // public Hit nextHit() {
161
// pos++;
162
// return (Hit) iter.next();
163
// }
164

165   public void add(Object JavaDoc obj) {
166     pos = 0;
167     list.add(obj);
168     iter = list.iterator();
169   }
170
171   public void addAll(Collection JavaDoc col) {
172     pos = 0;
173     list.addAll(col);
174     iter = list.iterator();
175   }
176
177   public void remove(Object JavaDoc obj) {
178     pos = 0;
179     list.remove(obj);
180     iter = list.iterator();
181   }
182
183   public long size() {
184     return getSize();
185   }
186
187   public List JavaDoc getList() {
188     return list;
189   }
190
191 }
192
Popular Tags