1 21 package com.db4o; 22 23 import java.util.*; 24 25 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 next() { 65 synchronized (i_map.streamLock()) { 66 i_map.checkActive(); 67 Object 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 |