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