summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/cursor
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2006-09-03 06:14:41 +0000
committerBrian S. O'Neill <bronee@gmail.com>2006-09-03 06:14:41 +0000
commit39fce59a840b723eb013bc79285687986592b2da (patch)
tree6ae5e1602fca5bedfe63643286588a1d35575ccc /src/main/java/com/amazon/carbonado/cursor
parent967e17697f343849a4c0e420c813d382b11eda44 (diff)
More work on query engine.
Diffstat (limited to 'src/main/java/com/amazon/carbonado/cursor')
-rw-r--r--src/main/java/com/amazon/carbonado/cursor/IteratorCursor.java36
1 files changed, 34 insertions, 2 deletions
diff --git a/src/main/java/com/amazon/carbonado/cursor/IteratorCursor.java b/src/main/java/com/amazon/carbonado/cursor/IteratorCursor.java
index 0330629..a32b45f 100644
--- a/src/main/java/com/amazon/carbonado/cursor/IteratorCursor.java
+++ b/src/main/java/com/amazon/carbonado/cursor/IteratorCursor.java
@@ -21,19 +21,44 @@ package com.amazon.carbonado.cursor;
import java.util.Iterator;
import java.util.NoSuchElementException;
+import java.util.concurrent.locks.Lock;
+
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+
/**
* Adapts an Iterator into a Cursor.
*
* @author Brian S O'Neill
*/
public class IteratorCursor<S> extends AbstractCursor<S> {
- private Iterator<S> mIterator;
+ private static final AtomicReferenceFieldUpdater<IteratorCursor, Lock> lockUpdater =
+ AtomicReferenceFieldUpdater.newUpdater(IteratorCursor.class, Lock.class, "mLock");
+
+ private volatile Iterator<S> mIterator;
+ private volatile Lock mLock;
/**
* @param iterable collection to iterate over, or null for empty cursor
*/
public IteratorCursor(Iterable<S> iterable) {
- this(iterable == null ? (Iterator<S>) null : iterable.iterator());
+ this(iterable, null);
+ }
+
+ /**
+ * @param iterable collection to iterate over, or null for empty cursor
+ * @param lock optional lock to hold while cursor is open
+ */
+ public IteratorCursor(Iterable<S> iterable, Lock lock) {
+ if (iterable == null) {
+ mIterator = null;
+ mLock = null;
+ } else {
+ if (lock != null) {
+ lock.lock();
+ }
+ mIterator = iterable.iterator();
+ mLock = lock;
+ }
}
/**
@@ -41,10 +66,17 @@ public class IteratorCursor<S> extends AbstractCursor<S> {
*/
public IteratorCursor(Iterator<S> iterator) {
mIterator = iterator;
+ mLock = null;
}
public void close() {
mIterator = null;
+ // Use AtomicReferenceFieldUpdater to allow close method to be safely
+ // called multiple times without unlocking multiple times.
+ Lock lock = lockUpdater.getAndSet(this, null);
+ if (lock != null) {
+ lock.unlock();
+ }
}
public boolean hasNext() {