KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > ReqExclScorer


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2005 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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 JavaDoc;
20
21
22 /** A Scorer for queries with a required subscorer and an excluding (prohibited) subscorer.
23  * <br>
24  * This <code>Scorer</code> implements {@link Scorer#skipTo(int)},
25  * and it uses the skipTo() on the given scorers.
26  */

27 public class ReqExclScorer extends Scorer {
28   private Scorer reqScorer, exclScorer;
29
30   /** Construct a <code>ReqExclScorer</code>.
31    * @param reqScorer The scorer that must match, except where
32    * @param exclScorer indicates exclusion.
33    */

34   public ReqExclScorer(
35       Scorer reqScorer,
36       Scorer exclScorer) {
37     super(null); // No similarity used.
38
this.reqScorer = reqScorer;
39     this.exclScorer = exclScorer;
40   }
41
42   private boolean firstTime = true;
43   
44   public boolean next() throws IOException JavaDoc {
45     if (firstTime) {
46       if (! exclScorer.next()) {
47         exclScorer = null; // exhausted at start
48
}
49       firstTime = false;
50     }
51     if (reqScorer == null) {
52       return false;
53     }
54     if (! reqScorer.next()) {
55       reqScorer = null; // exhausted, nothing left
56
return false;
57     }
58     if (exclScorer == null) {
59       return true; // reqScorer.next() already returned true
60
}
61     return toNonExcluded();
62   }
63   
64   /** Advance to non excluded doc.
65    * <br>On entry:
66    * <ul>
67    * <li>reqScorer != null,
68    * <li>exclScorer != null,
69    * <li>reqScorer was advanced once via next() or skipTo()
70    * and reqScorer.doc() may still be excluded.
71    * </ul>
72    * Advances reqScorer a non excluded required doc, if any.
73    * @return true iff there is a non excluded required doc.
74    */

75   private boolean toNonExcluded() throws IOException JavaDoc {
76     int exclDoc = exclScorer.doc();
77     do {
78       int reqDoc = reqScorer.doc(); // may be excluded
79
if (reqDoc < exclDoc) {
80         return true; // reqScorer advanced to before exclScorer, ie. not excluded
81
} else if (reqDoc > exclDoc) {
82         if (! exclScorer.skipTo(reqDoc)) {
83           exclScorer = null; // exhausted, no more exclusions
84
return true;
85         }
86         exclDoc = exclScorer.doc();
87         if (exclDoc > reqDoc) {
88           return true; // not excluded
89
}
90       }
91     } while (reqScorer.next());
92     reqScorer = null; // exhausted, nothing left
93
return false;
94   }
95
96   public int doc() {
97     return reqScorer.doc(); // reqScorer may be null when next() or skipTo() already return false
98
}
99
100   /** Returns the score of the current document matching the query.
101    * Initially invalid, until {@link #next()} is called the first time.
102    * @return The score of the required scorer.
103    */

104   public float score() throws IOException JavaDoc {
105     return reqScorer.score(); // reqScorer may be null when next() or skipTo() already return false
106
}
107   
108   /** Skips to the first match beyond the current whose document number is
109    * greater than or equal to a given target.
110    * <br>When this method is used the {@link #explain(int)} method should not be used.
111    * @param target The target document number.
112    * @return true iff there is such a match.
113    */

114   public boolean skipTo(int target) throws IOException JavaDoc {
115     if (firstTime) {
116       firstTime = false;
117       if (! exclScorer.skipTo(target)) {
118         exclScorer = null; // exhausted
119
}
120     }
121     if (reqScorer == null) {
122       return false;
123     }
124     if (exclScorer == null) {
125       return reqScorer.skipTo(target);
126     }
127     if (! reqScorer.skipTo(target)) {
128       reqScorer = null;
129       return false;
130     }
131     return toNonExcluded();
132   }
133
134   public Explanation explain(int doc) throws IOException JavaDoc {
135     Explanation res = new Explanation();
136     if (exclScorer.skipTo(doc) && (exclScorer.doc() == doc)) {
137       res.setDescription("excluded");
138     } else {
139       res.setDescription("not excluded");
140       res.addDetail(reqScorer.explain(doc));
141     }
142     return res;
143   }
144 }
145
Popular Tags