pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test-framework / java / org / apache / lucene / search / AssertingIndexSearcher.java
1 package org.apache.lucene.search;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.concurrent.ExecutorService;
21 import java.io.IOException;
22
23 import org.apache.lucene.index.IndexReader;
24
25 /** 
26  * Helper class that adds some extra checks to ensure correct
27  * usage of {@code IndexSearcher} and {@code Weight}.
28  * TODO: Extend this by more checks, that's just a start.
29  */
30 public class AssertingIndexSearcher extends IndexSearcher {
31   public  AssertingIndexSearcher(IndexReader r) {
32     super(r);
33   }
34   
35   public  AssertingIndexSearcher(IndexReader r, ExecutorService ex) {
36     super(r, ex);
37   }
38   
39   // not anonymous because else not serializable (compare trunk)
40   private static final class UnmodifiableWeight extends Weight {
41     private final Weight w;
42     
43     UnmodifiableWeight(Weight w) {
44       this.w = w;
45     }
46   
47     @Override
48     public Explanation explain(IndexReader reader, int doc) throws IOException {
49       return w.explain(reader, doc);
50     }
51
52     @Override
53     public Query getQuery() {
54       return w.getQuery();
55     }
56
57     @Override
58     public float getValue() {
59       return w.getValue();
60     }
61
62     @Override
63     public void normalize(float norm) {
64       throw new IllegalStateException("Weight already normalized.");
65     }
66
67     @Override
68     public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
69       Scorer scorer = w.scorer(reader, scoreDocsInOrder, topScorer);
70       if (scorer != null) {
71         // check that scorer obeys disi contract for docID() before next()/advance
72         try {
73           int docid = scorer.docID();
74           assert docid == -1 || docid == DocIdSetIterator.NO_MORE_DOCS;
75         } catch (UnsupportedOperationException ignored) {
76           // from a top-level BS1
77           assert topScorer;
78         }
79       }
80       return scorer;
81     }
82
83     @Override
84     public float sumOfSquaredWeights() throws IOException {
85       throw new IllegalStateException("Weight already normalized.");
86     }
87
88     @Override
89     public boolean scoresDocsOutOfOrder() {
90       return w.scoresDocsOutOfOrder();
91     }
92   }
93   
94   /** Ensures, that the returned {@code Weight} is not normalized again, which may produce wrong scores. */
95   @Override
96   public Weight createNormalizedWeight(Query query) throws IOException {
97     final Weight w = super.createNormalizedWeight(query);
98     return new UnmodifiableWeight(w);
99   }
100 }