summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/result/HitIterator.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/result/HitIterator.java')
-rw-r--r--container-search/src/main/java/com/yahoo/search/result/HitIterator.java66
1 files changed, 66 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/result/HitIterator.java b/container-search/src/main/java/com/yahoo/search/result/HitIterator.java
new file mode 100644
index 00000000000..adf642a28ec
--- /dev/null
+++ b/container-search/src/main/java/com/yahoo/search/result/HitIterator.java
@@ -0,0 +1,66 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.result;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import com.yahoo.search.Result;
+
+
+/**
+ * An iterator for the list of hits in a result. This iterator supports the remove operation.
+ *
+ * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
+ */
+public class HitIterator implements Iterator<Hit> {
+
+ /** The index into the list of hits */
+ private int index = -1;
+
+ /** The list of hits to iterate over */
+ private List<Hit> hits = null;
+
+ /** The result the hits belong to */
+ private HitGroup hitGroup = null;
+
+ /** Whether the iterator is in a state where remove is OK */
+ private boolean canRemove = false;
+
+ public HitIterator(HitGroup hitGroup, List<Hit> hits) {
+ this.hitGroup = hitGroup;
+ this.hits = hits;
+ }
+
+ public HitIterator(Result result, List<Hit> hits) {
+ this.hitGroup = result.hits();
+ this.hits = hits;
+ }
+
+ public boolean hasNext() {
+ if (hits.size() > (index + 1)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public Hit next() throws NoSuchElementException {
+ if (hits.size() <= (index + 1)) {
+ throw new NoSuchElementException();
+ } else {
+ canRemove = true;
+ return hits.get(++index);
+ }
+ }
+
+ public void remove() throws IllegalStateException {
+ if (!canRemove) {
+ throw new IllegalStateException();
+ }
+ hitGroup.remove(index);
+ index--;
+ canRemove = false;
+ }
+
+}