001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.lucene;
022:
023: import com.liferay.portal.kernel.search.Document;
024: import com.liferay.portal.kernel.search.Hits;
025: import com.liferay.util.Time;
026:
027: import java.io.IOException;
028:
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: import org.apache.lucene.search.Searcher;
033:
034: /**
035: * <a href="HitsImpl.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: *
039: */
040: public class HitsImpl implements Hits {
041:
042: public HitsImpl() {
043: _start = System.currentTimeMillis();
044: }
045:
046: public long getStart() {
047: return _start;
048: }
049:
050: public void setStart(long start) {
051: _start = start;
052: }
053:
054: public float getSearchTime() {
055: return _searchTime;
056: }
057:
058: public void setSearchTime(float time) {
059: _searchTime = time;
060: }
061:
062: public Document[] getDocs() {
063: return _docs;
064: }
065:
066: public void setDocs(Document[] docs) {
067: _docs = docs;
068: }
069:
070: public int getLength() {
071: return _length;
072: }
073:
074: public void setLength(int length) {
075: _length = length;
076: }
077:
078: public float[] getScores() {
079: return _scores;
080: }
081:
082: public void setScores(float[] scores) {
083: _scores = scores;
084: }
085:
086: public void setScores(Float[] scores) {
087: float[] primScores = new float[scores.length];
088:
089: for (int i = 0; i < scores.length; i++) {
090: primScores[i] = scores[i].floatValue();
091: }
092:
093: setScores(primScores);
094: }
095:
096: public Searcher getSearcher() {
097: return _searcher;
098: }
099:
100: public void setSearcher(Searcher searcher) {
101: _searcher = searcher;
102: }
103:
104: public void closeSearcher() {
105: try {
106: if (_searcher != null) {
107: _searcher.close();
108:
109: _searcher = null;
110: }
111: } catch (Exception e) {
112: }
113: }
114:
115: public Document doc(int n) {
116: try {
117: if ((_docs[n] == null) && (_hits != null)) {
118: _docs[n] = new DocumentImpl(_hits.doc(n));
119: }
120: } catch (IOException ioe) {
121: }
122:
123: return _docs[n];
124: }
125:
126: public float score(int n) {
127: try {
128: if ((_scores[n] == 0) && (_hits != null)) {
129: _scores[n] = _hits.score(n);
130: }
131: } catch (IOException ioe) {
132: }
133:
134: return _scores[n];
135: }
136:
137: public Hits subset(int begin, int end) {
138: Hits subset = new HitsImpl();
139:
140: if ((begin > -1) && (begin <= end)) {
141: subset.setStart(getStart());
142:
143: Document[] subsetDocs = new DocumentImpl[getLength()];
144: float[] subsetScores = new float[getLength()];
145:
146: int j = 0;
147:
148: for (int i = begin; (i < end) && (i < getLength()); i++, j++) {
149: subsetDocs[j] = doc(i);
150: subsetScores[j] = score(i);
151: }
152:
153: subset.setLength(j);
154:
155: subset.setDocs(subsetDocs);
156: subset.setScores(subsetScores);
157:
158: _searchTime = (float) (System.currentTimeMillis() - _start)
159: / Time.SECOND;
160:
161: subset.setSearchTime(getSearchTime());
162: }
163:
164: return subset;
165: }
166:
167: public List toList() {
168: List subset = new ArrayList(_length);
169:
170: for (int i = 0; i < _length; i++) {
171: subset.add(doc(i));
172: }
173:
174: return subset;
175: }
176:
177: public void recordHits(org.apache.lucene.search.Hits hits,
178: Searcher searcher) throws IOException {
179:
180: _hits = hits;
181: _length = hits.length();
182: _docs = new DocumentImpl[_length];
183: _scores = new float[_length];
184: _searcher = searcher;
185: }
186:
187: private org.apache.lucene.search.Hits _hits;
188: private long _start;
189: private float _searchTime;
190: private Document[] _docs = new DocumentImpl[0];
191: private int _length;
192: private float[] _scores = new float[0];
193: private Searcher _searcher;
194:
195: }
|