KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > MultipleTermPositions


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.lucene.util.PriorityQueue;
26
27 /**
28  * Describe class <code>MultipleTermPositions</code> here.
29  *
30  * @author Anders Nielsen
31  * @version 1.0
32  */

33 public class MultipleTermPositions implements TermPositions {
34
35   private static final class TermPositionsQueue extends PriorityQueue {
36     TermPositionsQueue(List JavaDoc termPositions) throws IOException JavaDoc {
37       initialize(termPositions.size());
38
39       Iterator JavaDoc i = termPositions.iterator();
40       while (i.hasNext()) {
41         TermPositions tp = (TermPositions) i.next();
42         if (tp.next())
43           put(tp);
44       }
45     }
46
47     final TermPositions peek() {
48       return (TermPositions) top();
49     }
50
51     public final boolean lessThan(Object JavaDoc a, Object JavaDoc b) {
52       return ((TermPositions) a).doc() < ((TermPositions) b).doc();
53     }
54   }
55
56   private static final class IntQueue {
57     private int _arraySize = 16;
58     private int _index = 0;
59     private int _lastIndex = 0;
60     private int[] _array = new int[_arraySize];
61
62     final void add(int i) {
63       if (_lastIndex == _arraySize)
64         growArray();
65
66       _array[_lastIndex++] = i;
67     }
68
69     final int next() {
70       return _array[_index++];
71     }
72
73     final void sort() {
74       Arrays.sort(_array, _index, _lastIndex);
75     }
76
77     final void clear() {
78       _index = 0;
79       _lastIndex = 0;
80     }
81
82     final int size() {
83       return (_lastIndex - _index);
84     }
85
86     private void growArray() {
87       int[] newArray = new int[_arraySize * 2];
88       System.arraycopy(_array, 0, newArray, 0, _arraySize);
89       _array = newArray;
90       _arraySize *= 2;
91     }
92   }
93
94   private int _doc;
95   private int _freq;
96   private TermPositionsQueue _termPositionsQueue;
97   private IntQueue _posList;
98
99   /**
100    * Creates a new <code>MultipleTermPositions</code> instance.
101    *
102    * @exception IOException
103    */

104   public MultipleTermPositions(IndexReader indexReader, Term[] terms) throws IOException JavaDoc {
105     List JavaDoc termPositions = new LinkedList JavaDoc();
106
107     for (int i = 0; i < terms.length; i++)
108       termPositions.add(indexReader.termPositions(terms[i]));
109
110     _termPositionsQueue = new TermPositionsQueue(termPositions);
111     _posList = new IntQueue();
112   }
113
114   public final boolean next() throws IOException JavaDoc {
115     if (_termPositionsQueue.size() == 0)
116       return false;
117
118     _posList.clear();
119     _doc = _termPositionsQueue.peek().doc();
120
121     TermPositions tp;
122     do {
123       tp = _termPositionsQueue.peek();
124
125       for (int i = 0; i < tp.freq(); i++)
126         _posList.add(tp.nextPosition());
127
128       if (tp.next())
129         _termPositionsQueue.adjustTop();
130       else {
131         _termPositionsQueue.pop();
132         tp.close();
133       }
134     } while (_termPositionsQueue.size() > 0 && _termPositionsQueue.peek().doc() == _doc);
135
136     _posList.sort();
137     _freq = _posList.size();
138
139     return true;
140   }
141
142   public final int nextPosition() {
143     return _posList.next();
144   }
145
146   public final boolean skipTo(int target) throws IOException JavaDoc {
147     while (_termPositionsQueue.peek() != null && target > _termPositionsQueue.peek().doc()) {
148       TermPositions tp = (TermPositions) _termPositionsQueue.pop();
149       if (tp.skipTo(target))
150         _termPositionsQueue.put(tp);
151       else
152         tp.close();
153     }
154     return next();
155   }
156
157   public final int doc() {
158     return _doc;
159   }
160
161   public final int freq() {
162     return _freq;
163   }
164
165   public final void close() throws IOException JavaDoc {
166     while (_termPositionsQueue.size() > 0)
167       ((TermPositions) _termPositionsQueue.pop()).close();
168   }
169
170   /**
171    * Not implemented.
172    * @throws UnsupportedOperationException
173    */

174   public void seek(Term arg0) throws IOException JavaDoc {
175     throw new UnsupportedOperationException JavaDoc();
176   }
177
178   /**
179    * Not implemented.
180    * @throws UnsupportedOperationException
181    */

182   public void seek(TermEnum termEnum) throws IOException JavaDoc {
183     throw new UnsupportedOperationException JavaDoc();
184   }
185
186   /**
187    * Not implemented.
188    * @throws UnsupportedOperationException
189    */

190   public int read(int[] arg0, int[] arg1) throws IOException JavaDoc {
191     throw new UnsupportedOperationException JavaDoc();
192   }
193
194 }
195
Popular Tags