001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/search/tags/sakai_2-4-1/search-impl/impl/src/java/org/sakaiproject/search/component/service/impl/SearchListImpl.java $
003: * $Id: SearchListImpl.java 22609 2007-03-14 19:28:42Z ian@caret.cam.ac.uk $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.search.component.service.impl;
021:
022: import java.io.IOException;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.ListIterator;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.lucene.analysis.Analyzer;
031: import org.apache.lucene.search.Hits;
032: import org.apache.lucene.search.Query;
033: import org.sakaiproject.search.api.SearchIndexBuilder;
034: import org.sakaiproject.search.api.SearchList;
035: import org.sakaiproject.search.api.SearchResult;
036: import org.sakaiproject.search.api.SearchService;
037: import org.sakaiproject.search.filter.SearchItemFilter;
038:
039: /**
040: * @author ieb
041: */
042: public class SearchListImpl implements SearchList {
043:
044: private static Log dlog = LogFactory.getLog(SearchListImpl.class);
045:
046: private Hits h;
047:
048: private Query query;
049:
050: private int start = 0;
051:
052: private int end = 500;
053:
054: private Analyzer analyzer;
055:
056: private SearchItemFilter filter;
057:
058: private SearchIndexBuilder searchIndexBuilder;
059:
060: private SearchService searchService;
061:
062: public SearchListImpl(Hits h, Query query, int start, int end,
063: Analyzer analyzer, SearchItemFilter filter,
064: SearchIndexBuilder searchIndexBuilder,
065: SearchService searchService) {
066: this .h = h;
067: this .query = query;
068: this .start = start;
069: this .end = end;
070: this .analyzer = analyzer;
071: this .filter = filter;
072: this .searchIndexBuilder = searchIndexBuilder;
073: this .searchService = searchService;
074:
075: }
076:
077: /**
078: * @{inheritDoc}
079: */
080: public Iterator iterator(final int startAt) {
081: return new Iterator() {
082: int counter = Math.max(startAt, start);
083:
084: public boolean hasNext() {
085: return counter < Math.min(h.length(), end);
086: }
087:
088: public Object next() {
089:
090: try {
091: final int this Hit = counter;
092: counter++;
093: return filter.filter(new SearchResultImpl(h,
094: this Hit, query, analyzer,
095: searchIndexBuilder, searchService));
096: } catch (IOException e) {
097: throw new RuntimeException(
098: "Cant get Hit for some reason ", e);
099: }
100: }
101:
102: public void remove() {
103: throw new UnsupportedOperationException(
104: "Not Implemented");
105: }
106:
107: };
108: }
109:
110: public int size() {
111: return Math.min(h.length(), end - start);
112: }
113:
114: public int getFullSize() {
115: return h.length();
116: }
117:
118: public boolean isEmpty() {
119: return (size() == 0);
120: }
121:
122: public boolean contains(Object arg0) {
123: throw new UnsupportedOperationException("Not Implemented");
124: }
125:
126: public Iterator iterator() {
127: return iterator(0);
128: }
129:
130: public Object[] toArray() {
131: Object[] o;
132: try {
133: o = new Object[size()];
134: for (int i = 0; i < o.length; i++) {
135:
136: o[i + start] = filter.filter(new SearchResultImpl(h, i
137: + start, query, analyzer, searchIndexBuilder,
138: searchService));
139: }
140: } catch (IOException e) {
141: throw new RuntimeException("Failed to load all results ", e);
142: }
143: return o;
144: }
145:
146: public Object[] toArray(Object[] arg0) {
147: if (arg0 instanceof SearchResult[]) {
148: return toArray();
149: }
150: return null;
151: }
152:
153: public boolean add(Object arg0) {
154: throw new UnsupportedOperationException("Not Implemented");
155: }
156:
157: public boolean remove(Object arg0) {
158: throw new UnsupportedOperationException("Not Implemented");
159: }
160:
161: public boolean containsAll(Collection arg0) {
162: throw new UnsupportedOperationException("Not Implemented");
163: }
164:
165: public boolean addAll(Collection arg0) {
166: throw new UnsupportedOperationException("Not Implemented");
167: }
168:
169: public boolean addAll(int arg0, Collection arg1) {
170: throw new UnsupportedOperationException("Not Implemented");
171: }
172:
173: public boolean removeAll(Collection arg0) {
174: throw new UnsupportedOperationException("Not Implemented");
175: }
176:
177: public boolean retainAll(Collection arg0) {
178: throw new UnsupportedOperationException("Not Implemented");
179: }
180:
181: public void clear() {
182: throw new UnsupportedOperationException("Not Implemented");
183: }
184:
185: public Object get(int arg0) {
186: try {
187: return filter.filter(new SearchResultImpl(h, arg0, query,
188: analyzer, searchIndexBuilder, searchService));
189: } catch (IOException e) {
190: throw new RuntimeException("Failed to retrieve result ", e);
191: }
192:
193: }
194:
195: public Object set(int arg0, Object arg1) {
196: throw new UnsupportedOperationException("Not Implemented");
197: }
198:
199: public void add(int arg0, Object arg1) {
200: throw new UnsupportedOperationException("Not Implemented");
201:
202: }
203:
204: public Object remove(int arg0) {
205: throw new UnsupportedOperationException("Not Implemented");
206: }
207:
208: public int indexOf(Object arg0) {
209: throw new UnsupportedOperationException("Not Implemented");
210: }
211:
212: public int lastIndexOf(Object arg0) {
213: throw new UnsupportedOperationException("Not Implemented");
214: }
215:
216: public ListIterator listIterator() {
217: throw new UnsupportedOperationException("Not Implemented");
218: }
219:
220: public ListIterator listIterator(int arg0) {
221: throw new UnsupportedOperationException("Not Implemented");
222: }
223:
224: public List subList(int arg0, int arg1) {
225: throw new UnsupportedOperationException("Not Implemented");
226: }
227:
228: public int getStart() {
229: return start;
230: }
231:
232: }
|