1 package org.apache.maven.index;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.Closeable;
23 import java.io.IOException;
24 import java.util.Iterator;
25
26 import org.apache.lucene.search.Query;
27
28 /**
29 * A Search Response for the "iterator-like" search request. The totalHitsCount reports <em>total</em> hits found on
30 * index, even if the set of ArtifactInfos are usually limited! On the flipside, the hitsCount is actually unknown,
31 * since this instance performs filtering on the fly, hence it does not know how many hits it will return ahead of time.
32 *
33 * @author cstamas
34 */
35 public class IteratorSearchResponse
36 extends AbstractSearchResponse
37 implements Iterable<ArtifactInfo>, Closeable
38 {
39 private final IteratorResultSet results;
40
41 public IteratorSearchResponse( Query query, int totalHits, IteratorResultSet results )
42 {
43 super( query, totalHits, -1 );
44
45 this.results = results;
46 }
47
48 public IteratorResultSet getResults()
49 {
50 return results;
51 }
52
53 public IteratorResultSet iterator()
54 {
55 return getResults();
56 }
57
58 @Override
59 public void close()
60 throws IOException
61 {
62 getResults().close();
63 }
64
65 /**
66 * A helper method delegating the call to the IteratorResultSet.
67 *
68 * @return
69 */
70 public int getTotalProcessedArtifactInfoCount()
71 {
72 return getResults().getTotalProcessedArtifactInfoCount();
73 }
74
75 // ==
76
77 public static final IteratorResultSet EMPTY_ITERATOR_RESULT_SET = new IteratorResultSet()
78 {
79 public boolean hasNext()
80 {
81 return false;
82 }
83
84 public ArtifactInfo next()
85 {
86 return null;
87 }
88
89 public void remove()
90 {
91 throw new UnsupportedOperationException( "Method not supported on " + getClass().getName() );
92 }
93
94 public Iterator<ArtifactInfo> iterator()
95 {
96 return this;
97 }
98
99 public int getTotalProcessedArtifactInfoCount()
100 {
101 return 0;
102 }
103
104 public void close()
105 throws IOException
106 {
107 }
108 };
109
110 public static final IteratorSearchResponse empty( final Query q )
111 {
112 return new IteratorSearchResponse( q, 0, EMPTY_ITERATOR_RESULT_SET );
113 }
114
115 /**
116 * Empty search response.
117 *
118 * @deprecated Use {@link #empty(Query)} instead.
119 */
120 public static final IteratorSearchResponse EMPTY_ITERATOR_SEARCH_RESPONSE = empty( null );
121
122 /**
123 * Too many search response.
124 *
125 * @deprecated Left here for backward compatibility, but since version 4.1.0 (see MINDEXER-14) there is NO notion of "hit limit" anymore.
126 */
127 public static final IteratorSearchResponse TOO_MANY_HITS_ITERATOR_SEARCH_RESPONSE = new IteratorSearchResponse( null, -1, EMPTY_ITERATOR_RESULT_SET );
128 }