1 8 9 package com.sleepycat.persist; 10 11 import java.util.Iterator ; 12 import java.util.NoSuchElementException ; 13 14 import com.sleepycat.je.DatabaseException; 15 import com.sleepycat.je.LockMode; 16 import com.sleepycat.util.RuntimeExceptionWrapper; 17 18 23 class BasicIterator<V> implements Iterator <V> { 24 25 private EntityCursor<V> entityCursor; 26 private ForwardCursor<V> forwardCursor; 27 private LockMode lockMode; 28 private V nextValue; 29 30 33 BasicIterator(EntityCursor<V> entityCursor, LockMode lockMode) { 34 this.entityCursor = entityCursor; 35 this.forwardCursor = entityCursor; 36 this.lockMode = lockMode; 37 } 38 39 42 BasicIterator(ForwardCursor<V> forwardCursor, LockMode lockMode) { 43 this.forwardCursor = forwardCursor; 44 this.lockMode = lockMode; 45 } 46 47 public boolean hasNext() { 48 if (nextValue == null) { 49 try { 50 nextValue = forwardCursor.next(lockMode); 51 } catch (DatabaseException e) { 52 throw new RuntimeExceptionWrapper(e); 53 } 54 return nextValue != null; 55 } else { 56 return true; 57 } 58 } 59 60 public V next() { 61 if (hasNext()) { 62 V v = nextValue; 63 nextValue = null; 64 return v; 65 } else { 66 throw new NoSuchElementException (); 67 } 68 } 69 70 public void remove() { 71 if (entityCursor == null) { 72 throw new UnsupportedOperationException (); 73 } 74 try { 75 if (!entityCursor.delete()) { 76 throw new IllegalStateException 77 ("Record at cursor position is already deleted"); 78 } 79 } catch (DatabaseException e) { 80 throw new RuntimeExceptionWrapper(e); 81 } 82 } 83 } 84 | Popular Tags |