KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > P2ListElementIterator


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o;
22
23 import java.util.*;
24
25 /**
26  * JDK 2 Iterator
27  *
28  * Intended state: i_next is always active.
29  *
30  * @persistent
31  */

32 class P2ListElementIterator implements Iterator {
33
34     private final P2LinkedList i_list;
35
36     private P1ListElement i_preprevious;
37     private P1ListElement i_previous;
38     private P1ListElement i_next;
39
40     P2ListElementIterator(P2LinkedList a_list, P1ListElement a_next) {
41         i_list = a_list;
42         i_next = a_next;
43         checkNextActive();
44     }
45
46     private void checkNextActive() {
47         if (i_next != null) {
48             i_next.checkActive();
49         }
50     }
51
52     public void remove() {
53         if (i_previous != null) {
54             synchronized (i_previous.streamLock()) {
55                 if (i_preprevious != null) {
56                     i_preprevious.i_next = i_previous.i_next;
57                     i_preprevious.update();
58                 }
59                 i_list.checkRemoved(i_preprevious, i_previous);
60                 i_previous.delete(i_list.i_deleteRemoved);
61             }
62         }
63     }
64
65     public boolean hasNext() {
66         return i_next != null;
67     }
68
69     public Object JavaDoc next() {
70         if (i_next != null) {
71             synchronized (i_next.streamLock()) {
72                 i_preprevious = i_previous;
73                 i_previous = i_next;
74                 Object JavaDoc obj = i_next.activatedObject(i_list.elementActivationDepth());
75                 i_next = i_next.i_next;
76                 checkNextActive();
77                 return obj;
78             }
79         }
80         return null;
81     }
82
83     P1ListElement nextElement() {
84         i_preprevious = i_previous;
85         i_previous = i_next;
86         i_next = i_next.i_next;
87         checkNextActive();
88         return i_previous;
89     }
90
91     P1ListElement move(int a_elements) {
92         if (a_elements < 0) {
93             return null;
94         }
95         for (int i = 0; i < a_elements; i++) {
96             if (hasNext()) {
97                 nextElement();
98             } else {
99                 return null;
100             }
101         }
102         if (hasNext()) {
103             return nextElement();
104         }
105         return null;
106     }
107
108 }
109
Popular Tags