pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / spellchecker / src / java / org / apache / lucene / search / suggest / BufferingTermFreqIteratorWrapper.java
1 package org.apache.lucene.search.suggest;
2
3
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.apache.lucene.search.spell.TermFreqIterator;
8
9 /**
10  * This wrapper buffers incoming elements.
11  */
12 public class BufferingTermFreqIteratorWrapper implements TermFreqIterator {
13
14   /** Entry in the buffer. */
15   public static final class Entry implements Comparable<Entry> {
16     String word;
17     float freq;
18     
19     public Entry(String word, float freq) {
20       this.word = word;
21       this.freq = freq;
22     }
23     
24     public int compareTo(Entry o) {
25       return word.compareTo(o.word);
26     }    
27   }
28
29   protected ArrayList<Entry> entries = new ArrayList<Entry>();
30   
31   protected int curPos;
32   protected Entry curEntry;
33   
34   public BufferingTermFreqIteratorWrapper(TermFreqIterator source) {
35     // read all source data into buffer
36     while (source.hasNext()) {
37       String w = source.next();
38       Entry e = new Entry(w, source.freq());
39       entries.add(e);
40     }
41     curPos = 0;
42   }
43
44   public float freq() {
45     return curEntry.freq;
46   }
47
48   public boolean hasNext() {
49     return curPos < entries.size();
50   }
51
52   public String next() {
53     curEntry = entries.get(curPos);
54     curPos++;
55     return curEntry.word;
56   }
57
58   public void remove() {
59     throw new UnsupportedOperationException("remove is not supported");
60   }
61   
62   public List<Entry> entries() {
63     return entries;
64   }
65 }