01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.lucene.search;
17:
18: import java.util.Iterator;
19: import java.util.NoSuchElementException;
20:
21: /**
22: * An iterator over {@link Hits} that provides lazy fetching of each document.
23: * {@link Hits#iterator()} returns an instance of this class. Calls to {@link #next()}
24: * return a {@link Hit} instance.
25: *
26: * @author Jeremy Rayner
27: */
28: public class HitIterator implements Iterator {
29: private Hits hits;
30: private int hitNumber = 0;
31:
32: /**
33: * Constructed from {@link Hits#iterator()}.
34: */
35: HitIterator(Hits hits) {
36: this .hits = hits;
37: }
38:
39: /**
40: * @return true if current hit is less than the total number of {@link Hits}.
41: */
42: public boolean hasNext() {
43: return hitNumber < hits.length();
44: }
45:
46: /**
47: * Returns a {@link Hit} instance representing the next hit in {@link Hits}.
48: *
49: * @return Next {@link Hit}.
50: */
51: public Object next() {
52: if (hitNumber == hits.length())
53: throw new NoSuchElementException();
54:
55: Object next = new Hit(hits, hitNumber);
56: hitNumber++;
57: return next;
58: }
59:
60: /**
61: * Unsupported operation.
62: *
63: * @throws UnsupportedOperationException
64: */
65: public void remove() {
66: throw new UnsupportedOperationException();
67: }
68:
69: /**
70: * Returns the total number of hits.
71: */
72: public int length() {
73: return hits.length();
74: }
75: }
|