LUCENE-1984: Generify Disjunction* - add tons of @Override

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@825881 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2009-10-16 13:20:34 +00:00
parent b714198d17
commit 59534306ee
3 changed files with 40 additions and 24 deletions

View File

@ -14,8 +14,8 @@ Changes in runtime behavior
API Changes
* LUCENE-1257, ...: Port to Java 1.5 [not yet finished].
(Uwe Schindler, Robert Muir, Karl Wettin, Paul Elschot)
* LUCENE-1257, LUCENE-1984,...: Port to Java 1.5 [not yet finished].
(Uwe Schindler, Robert Muir, Karl Wettin, Paul Elschot, Kay Kay)
* LUCENE-1944: Remove (all) deprecated methods/constructors taking
String/File directory pathes in IndexReader / IndexWriter and others.

View File

@ -39,10 +39,10 @@ import org.apache.lucene.index.Term;
* include this term in only the best of those multiple fields, without confusing this with the better case of two different terms
* in the multiple fields.
*/
public class DisjunctionMaxQuery extends Query {
public class DisjunctionMaxQuery extends Query implements Iterable<Query> {
/* The subqueries */
private ArrayList disjuncts = new ArrayList();
private ArrayList<Query> disjuncts = new ArrayList<Query>();
/* Multiple of the non-max disjunct scores added into our final score. Non-zero values support tie-breaking. */
private float tieBreakerMultiplier = 0.0f;
@ -62,7 +62,7 @@ public class DisjunctionMaxQuery extends Query {
* @param disjuncts a Collection<Query> of all the disjuncts to add
* @param tieBreakerMultiplier the weight to give to each matching non-maximum disjunct
*/
public DisjunctionMaxQuery(Collection disjuncts, float tieBreakerMultiplier) {
public DisjunctionMaxQuery(Collection<Query> disjuncts, float tieBreakerMultiplier) {
this.tieBreakerMultiplier = tieBreakerMultiplier;
add(disjuncts);
}
@ -77,12 +77,12 @@ public class DisjunctionMaxQuery extends Query {
/** Add a collection of disjuncts to this disjunction
* via Iterable<Query>
*/
public void add(Collection disjuncts) {
public void add(Collection<Query> disjuncts) {
this.disjuncts.addAll(disjuncts);
}
/** An Iterator<Query> over the disjuncts */
public Iterator iterator() {
public Iterator<Query> iterator() {
return disjuncts.iterator();
}
@ -98,26 +98,29 @@ public class DisjunctionMaxQuery extends Query {
protected Similarity similarity;
/** The Weights for our subqueries, in 1-1 correspondence with disjuncts */
protected ArrayList weights = new ArrayList(); // The Weight's for our subqueries, in 1-1 correspondence with disjuncts
protected ArrayList<Weight> weights = new ArrayList<Weight>(); // The Weight's for our subqueries, in 1-1 correspondence with disjuncts
/* Construct the Weight for this Query searched by searcher. Recursively construct subquery weights. */
public DisjunctionMaxWeight(Searcher searcher) throws IOException {
this.similarity = searcher.getSimilarity();
for (Iterator iter = disjuncts.iterator(); iter.hasNext();) {
for (Iterator<Query> iter = disjuncts.iterator(); iter.hasNext();) {
weights.add(((Query) iter.next()).createWeight(searcher));
}
}
/* Return our associated DisjunctionMaxQuery */
@Override
public Query getQuery() { return DisjunctionMaxQuery.this; }
/* Return our boost */
@Override
public float getValue() { return getBoost(); }
/* Compute the sub of squared weights of us applied to our subqueries. Used for normalization. */
@Override
public float sumOfSquaredWeights() throws IOException {
float max = 0.0f, sum = 0.0f;
for (Iterator iter = weights.iterator(); iter.hasNext();) {
for (Iterator<Weight> iter = weights.iterator(); iter.hasNext();) {
float sub = ((Weight) iter.next()).sumOfSquaredWeights();
sum += sub;
max = Math.max(max, sub);
@ -128,19 +131,21 @@ public class DisjunctionMaxQuery extends Query {
}
/* Apply the computed normalization factor to our subqueries */
@Override
public void normalize(float norm) {
norm *= getBoost(); // Incorporate our boost
for (Iterator iter = weights.iterator(); iter.hasNext();) {
for (Iterator<Weight> iter = weights.iterator(); iter.hasNext();) {
((Weight) iter.next()).normalize(norm);
}
}
/* Create the scorer used to score our associated DisjunctionMaxQuery */
@Override
public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder,
boolean topScorer) throws IOException {
Scorer[] scorers = new Scorer[weights.size()];
int idx = 0;
for (Iterator iter = weights.iterator(); iter.hasNext();) {
for (Iterator<Weight> iter = weights.iterator(); iter.hasNext();) {
Weight w = (Weight) iter.next();
Scorer subScorer = w.scorer(reader, true, false);
if (subScorer != null && subScorer.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
@ -153,12 +158,13 @@ public class DisjunctionMaxQuery extends Query {
}
/* Explain the score we computed for doc */
@Override
public Explanation explain(IndexReader reader, int doc) throws IOException {
if (disjuncts.size() == 1) return ((Weight) weights.get(0)).explain(reader,doc);
ComplexExplanation result = new ComplexExplanation();
float max = 0.0f, sum = 0.0f;
result.setDescription(tieBreakerMultiplier == 0.0f ? "max of:" : "max plus " + tieBreakerMultiplier + " times others of:");
for (Iterator iter = weights.iterator(); iter.hasNext();) {
for (Iterator<Weight> iter = weights.iterator(); iter.hasNext();) {
Explanation e = ((Weight) iter.next()).explain(reader, doc);
if (e.isMatch()) {
result.setMatch(Boolean.TRUE);
@ -174,6 +180,7 @@ public class DisjunctionMaxQuery extends Query {
} // end of DisjunctionMaxWeight inner class
/* Create the Weight used to score us */
@Override
public Weight createWeight(Searcher searcher) throws IOException {
return new DisjunctionMaxWeight(searcher);
}
@ -181,6 +188,7 @@ public class DisjunctionMaxQuery extends Query {
/** Optimize our representation and our subqueries representations
* @param reader the IndexReader we query
* @return an optimized copy of us (which may not be a copy if there is nothing to optimize) */
@Override
public Query rewrite(IndexReader reader) throws IOException {
int numDisjunctions = disjuncts.size();
if (numDisjunctions == 1) {
@ -207,6 +215,7 @@ public class DisjunctionMaxQuery extends Query {
/** Create a shallow copy of us -- used in rewriting if necessary
* @return a copy of us (but reuse, don't copy, our subqueries) */
@Override
public Object clone() {
DisjunctionMaxQuery clone = (DisjunctionMaxQuery)super.clone();
clone.disjuncts = (ArrayList)this.disjuncts.clone();
@ -214,6 +223,7 @@ public class DisjunctionMaxQuery extends Query {
}
// inherit javadoc
@Override
public void extractTerms(Set<Term> terms) {
for (Iterator iter = disjuncts.iterator(); iter.hasNext();) {
((Query) iter.next()).extractTerms(terms);
@ -224,6 +234,7 @@ public class DisjunctionMaxQuery extends Query {
* @param field the field to which we are applied
* @return a string that shows what we do, of the form "(disjunct1 | disjunct2 | ... | disjunctn)^boost"
*/
@Override
public String toString(String field) {
StringBuilder buffer = new StringBuilder();
buffer.append("(");
@ -254,6 +265,7 @@ public class DisjunctionMaxQuery extends Query {
* @param o another object
* @return true iff o is a DisjunctionMaxQuery with the same boost and the same subqueries, in the same order, as us
*/
@Override
public boolean equals(Object o) {
if (! (o instanceof DisjunctionMaxQuery) ) return false;
DisjunctionMaxQuery other = (DisjunctionMaxQuery)o;
@ -265,6 +277,7 @@ public class DisjunctionMaxQuery extends Query {
/** Compute a hash code for hashing us
* @return the hash code
*/
@Override
public int hashCode() {
return Float.floatToIntBits(getBoost())
+ Float.floatToIntBits(tieBreakerMultiplier)

View File

@ -18,7 +18,6 @@ package org.apache.lucene.search;
*/
import java.util.List;
import java.util.Iterator;
import java.io.IOException;
import org.apache.lucene.util.ScorerDocQueue;
@ -31,7 +30,7 @@ class DisjunctionSumScorer extends Scorer {
private final int nrScorers;
/** The subscorers. */
protected final List subScorers;
protected final List<Scorer> subScorers;
/** The minimum number of scorers that should match. */
private final int minimumNrMatchers;
@ -68,7 +67,7 @@ class DisjunctionSumScorer extends Scorer {
* <br>When minimumNrMatchers equals the number of subScorers,
* it more efficient to use <code>ConjunctionScorer</code>.
*/
public DisjunctionSumScorer( List subScorers, int minimumNrMatchers) throws IOException {
public DisjunctionSumScorer( List<Scorer> subScorers, int minimumNrMatchers) throws IOException {
super(null);
nrScorers = subScorers.size();
@ -89,7 +88,7 @@ class DisjunctionSumScorer extends Scorer {
/** Construct a <code>DisjunctionScorer</code>, using one as the minimum number
* of matching subscorers.
*/
public DisjunctionSumScorer(List subScorers) throws IOException {
public DisjunctionSumScorer(List<Scorer> subScorers) throws IOException {
this(subScorers, 1);
}
@ -97,11 +96,9 @@ class DisjunctionSumScorer extends Scorer {
* initialize <code>scorerDocQueue</code>.
*/
private void initScorerDocQueue() throws IOException {
Iterator si = subScorers.iterator();
scorerDocQueue = new ScorerDocQueue(nrScorers);
while (si.hasNext()) {
Scorer se = (Scorer) si.next();
if (se.nextDoc() != NO_MORE_DOCS) { // doc() method will be used in scorerDocQueue.
for (Scorer se : subScorers) {
if (se.nextDoc() != NO_MORE_DOCS) {
scorerDocQueue.insert(se);
}
}
@ -111,6 +108,7 @@ class DisjunctionSumScorer extends Scorer {
* @param collector The collector to which all matching documents are passed through.
* <br>When this method is used the {@link #explain(int)} method should not be used.
*/
@Override
public void score(Collector collector) throws IOException {
collector.setScorer(this);
while (nextDoc() != NO_MORE_DOCS) {
@ -125,6 +123,7 @@ class DisjunctionSumScorer extends Scorer {
* @param max Do not score documents past this.
* @return true if more matching documents may remain.
*/
@Override
protected boolean score(Collector collector, int max, int firstDocID) throws IOException {
// firstDocID is ignored since nextDoc() sets 'currentDoc'
collector.setScorer(this);
@ -137,6 +136,7 @@ class DisjunctionSumScorer extends Scorer {
return true;
}
@Override
public int nextDoc() throws IOException {
if (scorerDocQueue.size() < minimumNrMatchers || !advanceAfterCurrent()) {
currentDoc = NO_MORE_DOCS;
@ -191,8 +191,10 @@ class DisjunctionSumScorer extends Scorer {
/** Returns the score of the current document matching the query.
* Initially invalid, until {@link #next()} is called the first time.
*/
@Override
public float score() throws IOException { return currentScore; }
@Override
public int docID() {
return currentDoc;
}
@ -216,6 +218,7 @@ class DisjunctionSumScorer extends Scorer {
* @return the document whose number is greater than or equal to the given
* target, or -1 if none exist.
*/
@Override
public int advance(int target) throws IOException {
if (scorerDocQueue.size() < minimumNrMatchers) {
return currentDoc = NO_MORE_DOCS;
@ -235,13 +238,13 @@ class DisjunctionSumScorer extends Scorer {
}
/** @return An explanation for the score of a given document. */
@Override
public Explanation explain(int doc) throws IOException {
Explanation res = new Explanation();
Iterator ssi = subScorers.iterator();
float sumScore = 0.0f;
int nrMatches = 0;
while (ssi.hasNext()) {
Explanation es = ((Scorer) ssi.next()).explain(doc);
for (Scorer se : subScorers) {
Explanation es = se.explain(doc);
if (es.getValue() > 0.0f) { // indicates match
sumScore += es.getValue();
nrMatches++;