1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.lucene;
24  
25  import com.liferay.portal.kernel.search.Document;
26  import com.liferay.portal.kernel.search.Hits;
27  import com.liferay.util.Time;
28  
29  import java.io.IOException;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import org.apache.lucene.search.Searcher;
35  
36  /**
37   * <a href="HitsImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class HitsImpl implements Hits {
43  
44      public HitsImpl() {
45          _start = System.currentTimeMillis();
46      }
47  
48      public long getStart() {
49          return _start;
50      }
51  
52      public void setStart(long start) {
53          _start = start;
54      }
55  
56      public float getSearchTime() {
57          return _searchTime;
58      }
59  
60      public void setSearchTime(float time) {
61          _searchTime = time;
62      }
63  
64      public Document[] getDocs() {
65          return _docs;
66      }
67  
68      public void setDocs(Document[] docs) {
69          _docs = docs;
70      }
71  
72      public int getLength() {
73          return _length;
74      }
75  
76      public void setLength(int length) {
77          _length = length;
78      }
79  
80      public float[] getScores() {
81          return _scores;
82      }
83  
84      public void setScores(float[] scores) {
85          _scores = scores;
86      }
87  
88      public void setScores(Float[] scores) {
89          float[] primScores = new float[scores.length];
90  
91          for (int i = 0; i < scores.length; i++) {
92              primScores[i] = scores[i].floatValue();
93          }
94  
95          setScores(primScores);
96      }
97  
98      public Searcher getSearcher() {
99          return _searcher;
100     }
101 
102     public void setSearcher(Searcher searcher) {
103         _searcher = searcher;
104     }
105 
106     public void closeSearcher() {
107         try {
108             if (_searcher != null){
109                 _searcher.close();
110 
111                 _searcher = null;
112             }
113         }
114         catch (Exception e) {
115         }
116     }
117 
118     public Document doc(int n) {
119         try {
120             if ((_docs[n] == null) && (_hits != null)) {
121                 _docs[n] = new DocumentImpl(_hits.doc(n));
122             }
123         }
124         catch(IOException ioe) {
125         }
126 
127         return _docs[n];
128     }
129 
130     public float score(int n) {
131         try {
132             if ((_scores[n] == 0) && (_hits != null)) {
133                 _scores[n] = _hits.score(n);
134             }
135         }
136         catch (IOException ioe) {
137         }
138 
139         return _scores[n];
140     }
141 
142     public Hits subset(int begin, int end) {
143         Hits subset = new HitsImpl();
144 
145         if ((begin > - 1) && (begin <= end)) {
146             subset.setStart(getStart());
147 
148             Document[] subsetDocs = new DocumentImpl[getLength()];
149             float[] subsetScores = new float[getLength()];
150 
151             int j = 0;
152 
153             for (int i = begin; (i < end) && (i < getLength()); i++, j++) {
154                 subsetDocs[j] = doc(i);
155                 subsetScores[j] = score(i);
156             }
157 
158             subset.setLength(j);
159 
160             subset.setDocs(subsetDocs);
161             subset.setScores(subsetScores);
162 
163             _searchTime =
164                 (float)(System.currentTimeMillis() - _start) / Time.SECOND;
165 
166             subset.setSearchTime(getSearchTime());
167         }
168 
169         return subset;
170     }
171 
172     public List toList() {
173         List subset = new ArrayList(_length);
174 
175         for (int i = 0; i < _length; i++) {
176             subset.add(doc(i));
177         }
178 
179         return subset;
180     }
181 
182     public void recordHits(
183             org.apache.lucene.search.Hits hits, Searcher searcher)
184         throws IOException {
185 
186         _hits = hits;
187         _length = hits.length();
188         _docs = new DocumentImpl[_length];
189         _scores = new float[_length];
190         _searcher = searcher;
191     }
192 
193     private org.apache.lucene.search.Hits _hits;
194     private long _start;
195     private float _searchTime;
196     private Document[] _docs = new DocumentImpl[0];
197     private int _length;
198     private float[] _scores = new float[0];
199     private Searcher _searcher;
200 
201 }