add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / ReqOptSumScorer.java
1 package org.apache.lucene.search;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import java.io.IOException;
20
21 /** A Scorer for queries with a required part and an optional part.
22  * Delays skipTo() on the optional part until a score() is needed.
23  * <br>
24  * This <code>Scorer</code> implements {@link Scorer#skipTo(int)}.
25  */
26 class ReqOptSumScorer extends Scorer {
27   /** The scorers passed from the constructor.
28    * These are set to null as soon as their next() or skipTo() returns false.
29    */
30   private Scorer reqScorer;
31   private Scorer optScorer;
32
33   /** Construct a <code>ReqOptScorer</code>.
34    * @param reqScorer The required scorer. This must match.
35    * @param optScorer The optional scorer. This is used for scoring only.
36    */
37   public ReqOptSumScorer(
38       Scorer reqScorer,
39       Scorer optScorer)
40   {
41     super(reqScorer.weight);
42     this.reqScorer = reqScorer;
43     this.optScorer = optScorer;
44   }
45
46   @Override
47   public int nextDoc() throws IOException {
48     return reqScorer.nextDoc();
49   }
50   
51   @Override
52   public int advance(int target) throws IOException {
53     return reqScorer.advance(target);
54   }
55   
56   @Override
57   public int docID() {
58     return reqScorer.docID();
59   }
60   
61   /** Returns the score of the current document matching the query.
62    * Initially invalid, until {@link #nextDoc()} is called the first time.
63    * @return The score of the required scorer, eventually increased by the score
64    * of the optional scorer when it also matches the current document.
65    */
66   @Override
67   public float score() throws IOException {
68     int curDoc = reqScorer.docID();
69     float reqScore = reqScorer.score();
70     if (optScorer == null) {
71       return reqScore;
72     }
73     
74     int optScorerDoc = optScorer.docID();
75     if (optScorerDoc < curDoc && (optScorerDoc = optScorer.advance(curDoc)) == NO_MORE_DOCS) {
76       optScorer = null;
77       return reqScore;
78     }
79     
80     return optScorerDoc == curDoc ? reqScore + optScorer.score() : reqScore;
81   }
82
83 }
84