KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > P2HashMapIterator


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  * @persistent
27  */

28 class P2HashMapIterator implements Iterator {
29
30     private P1HashElement i_current;
31
32     private final P2HashMap i_map;
33     private int i_nextIndex;
34
35     private P1HashElement i_previous;
36
37     P2HashMapIterator(P2HashMap a_map) {
38         i_map = a_map;
39         i_nextIndex = -1;
40         getNextCurrent();
41     }
42
43     private int currentIndex() {
44         if (i_current == null) {
45             return -1;
46         }
47         return i_current.i_hashCode & i_map.i_mask;
48     }
49
50     private void getNextCurrent() {
51         i_previous = i_current;
52         i_current = (P1HashElement)nextElement();
53         if (i_current != null) {
54             i_current.checkActive();
55         }
56     }
57
58     public boolean hasNext() {
59         synchronized (i_map.streamLock()) {
60             return i_current != null;
61         }
62     }
63
64     public Object JavaDoc next() {
65         synchronized (i_map.streamLock()) {
66             i_map.checkActive();
67             Object JavaDoc ret = null;
68             if (i_current != null) {
69                 ret = i_current.activatedKey(i_map.elementActivationDepth());
70             }
71             getNextCurrent();
72             return ret;
73         }
74     }
75
76     private P1ListElement nextElement() {
77         if (i_current != null && i_current.i_next != null) {
78             return i_current.i_next;
79         }
80         if (i_nextIndex <= currentIndex()) {
81             searchNext();
82         }
83         if (i_nextIndex >= 0) {
84             return i_map.i_table[i_nextIndex];
85         }
86         return null;
87     }
88
89     public void remove() {
90         synchronized (i_map.streamLock()) {
91             i_map.checkActive();
92             if (i_previous != null) {
93                 int index = i_previous.i_hashCode & i_map.i_mask;
94                 if (index >= 0 && index < i_map.i_table.length) {
95                     P1HashElement last = null;
96                     P1HashElement phe = i_map.i_table[index];
97                     while (phe != i_previous && phe != null) {
98                         phe.checkActive();
99                         last = phe;
100                         phe = (P1HashElement)phe.i_next;
101                     }
102                     if (phe != null) {
103                         i_map.i_size--;
104                         if (last == null) {
105                             i_map.i_table[index] = (P1HashElement)phe.i_next;
106                         } else {
107                             last.i_next = phe.i_next;
108                             last.update();
109                         }
110                         i_map.modified();
111                         phe.delete(i_map.i_deleteRemoved);
112                     }
113                 }
114                 i_previous = null;
115             }
116         }
117     }
118
119     private void searchNext() {
120         if (i_nextIndex > -2) {
121             while (++i_nextIndex < i_map.i_tableSize) {
122                 if (i_map.i_table[i_nextIndex] != null) {
123                     return;
124                 }
125             }
126             i_nextIndex = -2;
127         }
128     }
129
130 }
131
Popular Tags