KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > HitIterator


1 /**
2  * Copyright 2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.lucene.search;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.NoSuchElementException JavaDoc;
21
22 /**
23  * An iterator over {@link Hits} that provides lazy fetching of each document.
24  * {@link Hits#iterator()} returns an instance of this class. Calls to {@link #next()}
25  * return a {@link Hit} instance.
26  *
27  * @author Jeremy Rayner
28  */

29 public class HitIterator implements Iterator JavaDoc {
30   private Hits hits;
31   private int hitNumber = 0;
32
33   /**
34    * Constructed from {@link Hits#iterator()}.
35    */

36   HitIterator(Hits hits) {
37     this.hits = hits;
38   }
39
40   /**
41    * @return true if current hit is less than the total number of {@link Hits}.
42    */

43   public boolean hasNext() {
44     return hitNumber < hits.length();
45   }
46
47   /**
48    * Returns a {@link Hit} instance representing the next hit in {@link Hits}.
49    *
50    * @return Next {@link Hit}.
51    */

52   public Object JavaDoc next() {
53     if (hitNumber == hits.length())
54       throw new NoSuchElementException JavaDoc();
55
56     Object JavaDoc next = new Hit(hits, hitNumber);
57     hitNumber++;
58     return next;
59   }
60
61   /**
62    * Unsupported operation.
63    *
64    * @throws UnsupportedOperationException
65    */

66   public void remove() {
67     throw new UnsupportedOperationException JavaDoc();
68   }
69
70   /**
71    * Returns the total number of hits.
72    */

73   public int length() {
74     return hits.length();
75   }
76 }
77
78
Popular Tags