From ca1059f194f5492b6b4200ab98219baf7c080e34 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Sun, 19 Aug 2012 08:45:25 +0000 Subject: [PATCH] LUCENE-3312: Small refctoring and final field git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene3312@1374710 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/util/FilterIterator.java | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/util/FilterIterator.java b/lucene/core/src/java/org/apache/lucene/util/FilterIterator.java index 8e9b5a9b232..d67a2ff7888 100644 --- a/lucene/core/src/java/org/apache/lucene/util/FilterIterator.java +++ b/lucene/core/src/java/org/apache/lucene/util/FilterIterator.java @@ -22,41 +22,40 @@ import java.util.NoSuchElementException; public abstract class FilterIterator implements Iterator { - private Iterator iterator; + private final Iterator iterator; private T next = null; private boolean nextIsSet = false; - protected abstract boolean predicateFunction(T field); + protected abstract boolean predicateFunction(T object); public FilterIterator(Iterator baseIterator) { this.iterator = baseIterator; } - public boolean hasNext() { - if (nextIsSet) { - return true; - } else { - return setNext(); + public final boolean hasNext() { + return nextIsSet || setNext(); + } + + public final T next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + assert nextIsSet; + try { + return next; + } finally { + nextIsSet = false; + next = null; } } - public T next() { - if (!nextIsSet) { - if (!setNext()) { - throw new NoSuchElementException(); - } - } - nextIsSet = false; - return next; - } - - public void remove() { + public final void remove() { throw new UnsupportedOperationException(); } private boolean setNext() { while (iterator.hasNext()) { - T object = iterator.next(); + final T object = iterator.next(); if (predicateFunction(object)) { next = object; nextIsSet = true;