From 7505950c5fee17493000f1ede9dc3317c3369a0f Mon Sep 17 00:00:00 2001 From: Michael Busch Date: Sat, 23 Jun 2007 15:56:34 +0000 Subject: [PATCH] LUCENE-937: CachingTokenFilter now uses an iterator to access the Tokens that are cached in the LinkedList. git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@550060 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 5 +++++ .../apache/lucene/analysis/CachingTokenFilter.java | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index d05f25d29d7..22e5a915aaf 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -14,6 +14,11 @@ New features Optimizations + 1. LUCENE-937: CachingTokenFilter now uses an iterator to access the + Tokens that are cached in the LinkedList. This increases performance + significantly, especially when the number of Tokens is large. + (Mark Miller via Michael Busch) + Documentation Build diff --git a/src/java/org/apache/lucene/analysis/CachingTokenFilter.java b/src/java/org/apache/lucene/analysis/CachingTokenFilter.java index c49729eca22..c35011d09eb 100644 --- a/src/java/org/apache/lucene/analysis/CachingTokenFilter.java +++ b/src/java/org/apache/lucene/analysis/CachingTokenFilter.java @@ -18,6 +18,7 @@ package org.apache.lucene.analysis; */ import java.io.IOException; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -33,7 +34,7 @@ import java.util.List; */ public class CachingTokenFilter extends TokenFilter { private List cache; - private int index; + private Iterator iterator; public CachingTokenFilter(TokenStream input) { super(input); @@ -44,18 +45,21 @@ public class CachingTokenFilter extends TokenFilter { // fill cache lazily cache = new LinkedList(); fillCache(); + iterator = cache.iterator(); } - if (index == cache.size()) { + if (!iterator.hasNext()) { // the cache is exhausted, return null return null; } - return (Token) cache.get(index++); + return (Token) iterator.next(); } public void reset() throws IOException { - index = 0; + if(cache != null) { + iterator = cache.iterator(); + } } private void fillCache() throws IOException {