01: /**********************************************************************
02: Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15:
16: Contributors:
17: ...
18: **********************************************************************/package org.jpox.sco;
19:
20: import java.util.ArrayList;
21: import java.util.Collection;
22: import java.util.Iterator;
23:
24: import org.jpox.StateManager;
25: import org.jpox.store.scostore.CollectionStore;
26:
27: /**
28: * An iterator for a SCO Collection object. Takes in the delegate and the
29: * backing store, and provides iteration through the objects.
30: * Only accessible from package to provide protection
31: *
32: * @version $Revision: 1.7 $
33: */
34: public class SCOCollectionIterator implements Iterator {
35: private final Iterator iter;
36: private Object last = null;
37:
38: private Collection ownerSCO;
39:
40: /**
41: * Constructor taking the delegate, backing store.
42: * @param sco The owener sco
43: * @param sm State Manager of SCO Collection to iterate
44: * @param theDelegate The delegate collection
45: * @param backingStore The backing store (connected to the DB)
46: * @param useDelegate Whether to use the delegate
47: */
48: public SCOCollectionIterator(Collection sco, StateManager sm,
49: Collection theDelegate, CollectionStore backingStore,
50: boolean useDelegate) {
51: ownerSCO = sco;
52:
53: // Populate our entries list
54: ArrayList entries = new ArrayList();
55:
56: Iterator i = null;
57: if (useDelegate) {
58: i = theDelegate.iterator();
59: } else {
60: if (backingStore != null) {
61: i = backingStore.iterator(sm);
62: } else {
63: i = theDelegate.iterator();
64: }
65: }
66:
67: while (i.hasNext()) {
68: entries.add(i.next());
69: }
70:
71: iter = entries.iterator();
72: }
73:
74: public boolean hasNext() {
75: return iter.hasNext();
76: }
77:
78: public Object next() {
79: return last = iter.next();
80: }
81:
82: public void remove() {
83: if (last == null) {
84: throw new IllegalStateException();
85: }
86:
87: ownerSCO.remove(last);
88: last = null;
89: }
90: }
|