KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > query > CachingTermPositions


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.search.impl.lucene.query;
18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.index.TermEnum;
23 import org.apache.lucene.index.TermPositions;
24
25 /**
26  * @author andyh
27  *
28  * TODO To change the template for this generated type comment go to Window -
29  * Preferences - Java - Code Style - Code Templates
30  */

31 public class CachingTermPositions implements TermPositions
32 {
33     int[] results;
34
35     int position = -1;
36
37     int last = -1;
38
39     TermPositions delegate;
40
41     CachingTermPositions(TermPositions delegate)
42     {
43         this.delegate = delegate;
44     }
45
46     /*
47      * (non-Javadoc)
48      *
49      * @see org.apache.lucene.index.TermPositions#nextPosition()
50      */

51     public int nextPosition() throws IOException JavaDoc
52     {
53         if (results == null)
54         {
55             results = new int[freq()];
56         }
57         position++;
58         if (last < position)
59         {
60             results[position] = delegate.nextPosition();
61             last = position;
62         }
63         return results[position];
64
65     }
66
67     public void reset()
68     {
69         position = -1;
70     }
71
72     private void clear()
73     {
74         position = -1;
75         last = -1;
76         results = null;
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see org.apache.lucene.index.TermDocs#seek(org.apache.lucene.index.Term)
83      */

84     public void seek(Term term) throws IOException JavaDoc
85     {
86         delegate.seek(term);
87         clear();
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see org.apache.lucene.index.TermDocs#seek(org.apache.lucene.index.TermEnum)
94      */

95     public void seek(TermEnum termEnum) throws IOException JavaDoc
96     {
97         delegate.seek(termEnum);
98         clear();
99     }
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see org.apache.lucene.index.TermDocs#doc()
105      */

106     public int doc()
107     {
108         return delegate.doc();
109     }
110
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.apache.lucene.index.TermDocs#freq()
115      */

116     public int freq()
117     {
118         return delegate.freq();
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see org.apache.lucene.index.TermDocs#next()
125      */

126     public boolean next() throws IOException JavaDoc
127     {
128         if (delegate.next())
129         {
130             clear();
131             return true;
132         }
133         else
134         {
135             return false;
136         }
137     }
138
139     /*
140      * (non-Javadoc)
141      *
142      * @see org.apache.lucene.index.TermDocs#read(int[], int[])
143      */

144     public int read(int[] docs, int[] freqs) throws IOException JavaDoc
145     {
146         int answer = delegate.read(docs, freqs);
147         clear();
148         return answer;
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see org.apache.lucene.index.TermDocs#skipTo(int)
155      */

156     public boolean skipTo(int target) throws IOException JavaDoc
157     {
158         if (delegate.skipTo(target))
159         {
160             clear();
161             return true;
162         }
163         else
164         {
165             return false;
166         }
167     }
168
169     /*
170      * (non-Javadoc)
171      *
172      * @see org.apache.lucene.index.TermDocs#close()
173      */

174     public void close() throws IOException JavaDoc
175     {
176         delegate.close();
177         clear();
178     }
179
180 }
Popular Tags