pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / ScoreCachingWrappingScorer.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.io.IOException;
21
22 /**
23  * A {@link Scorer} which wraps another scorer and caches the score of the
24  * current document. Successive calls to {@link #score()} will return the same
25  * result and will not invoke the wrapped Scorer's score() method, unless the
26  * current document has changed.<br>
27  * This class might be useful due to the changes done to the {@link Collector}
28  * interface, in which the score is not computed for a document by default, only
29  * if the collector requests it. Some collectors may need to use the score in
30  * several places, however all they have in hand is a {@link Scorer} object, and
31  * might end up computing the score of a document more than once.
32  */
33 public class ScoreCachingWrappingScorer extends Scorer {
34
35   private final Scorer scorer;
36   private int curDoc = -1;
37   private float curScore;
38   
39   /** Creates a new instance by wrapping the given scorer. */
40   public ScoreCachingWrappingScorer(Scorer scorer) {
41     super(scorer.getSimilarity(), scorer.weight);
42     this.scorer = scorer;
43   }
44
45   @Override
46   protected boolean score(Collector collector, int max, int firstDocID) throws IOException {
47     return scorer.score(collector, max, firstDocID);
48   }
49
50   @Override
51   public Similarity getSimilarity() {
52     return scorer.getSimilarity();
53   }
54   
55   @Override
56   public float score() throws IOException {
57     int doc = scorer.docID();
58     if (doc != curDoc) {
59       curScore = scorer.score();
60       curDoc = doc;
61     }
62     
63     return curScore;
64   }
65
66   @Override
67   public int docID() {
68     return scorer.docID();
69   }
70
71   @Override
72   public int nextDoc() throws IOException {
73     return scorer.nextDoc();
74   }
75   
76   @Override
77   public void score(Collector collector) throws IOException {
78     scorer.score(collector);
79   }
80   
81   @Override
82   public int advance(int target) throws IOException {
83     return scorer.advance(target);
84   }
85   
86 }