Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart9.png 37% of files have more coverage
29   97   12   4.83
12   51   0.41   6
6     2  
1    
 
  MultiTermQuery       Line # 38 29 12 83% 0.82978725
 
  (10)
 
1    package org.apache.lucene.search;
2   
3    /**
4    * Copyright 2004 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;
20   
21    import org.apache.lucene.index.IndexReader;
22    import org.apache.lucene.index.Term;
23    import org.apache.lucene.util.ToStringUtils;
24   
25    /**
26    * A {@link Query} that matches documents containing a subset of terms provided
27    * by a {@link FilteredTermEnum} enumeration.
28    * <P>
29    * <code>MultiTermQuery</code> is not designed to be used by itself.
30    * <BR>
31    * The reason being that it is not intialized with a {@link FilteredTermEnum}
32    * enumeration. A {@link FilteredTermEnum} enumeration needs to be provided.
33    * <P>
34    * For example, {@link WildcardQuery} and {@link FuzzyQuery} extend
35    * <code>MultiTermQuery</code> to provide {@link WildcardTermEnum} and
36    * {@link FuzzyTermEnum}, respectively.
37    */
 
38    public abstract class MultiTermQuery extends Query {
39    private Term term;
40   
41    /** Constructs a query for terms matching <code>term</code>. */
 
42  99 toggle public MultiTermQuery(Term term) {
43  99 this.term = term;
44    }
45   
46    /** Returns the pattern term. */
 
47  75 toggle public Term getTerm() { return term; }
48   
49    /** Construct the enumeration to be used, expanding the pattern term. */
50    protected abstract FilteredTermEnum getEnum(IndexReader reader)
51    throws IOException;
52   
 
53  17 toggle public Query rewrite(IndexReader reader) throws IOException {
54  17 FilteredTermEnum enumerator = getEnum(reader);
55  17 BooleanQuery query = new BooleanQuery(true);
56  17 try {
57  17 do {
58  22 Term t = enumerator.term();
59  22 if (t != null) {
60  18 TermQuery tq = new TermQuery(t); // found a match
61  18 tq.setBoost(getBoost() * enumerator.difference()); // set the boost
62  18 query.add(tq, BooleanClause.Occur.SHOULD); // add to query
63    }
64  22 } while (enumerator.next());
65    } finally {
66  17 enumerator.close();
67    }
68  17 return query;
69    }
70   
71    /** Prints a user-readable version of this query. */
 
72  18 toggle public String toString(String field) {
73  18 StringBuffer buffer = new StringBuffer();
74  18 if (!term.field().equals(field)) {
75  5 buffer.append(term.field());
76  5 buffer.append(":");
77    }
78  18 buffer.append(term.text());
79  18 buffer.append(ToStringUtils.boost(getBoost()));
80  18 return buffer.toString();
81    }
82   
 
83  4 toggle public boolean equals(Object o) {
84  0 if (this == o) return true;
85  0 if (!(o instanceof MultiTermQuery)) return false;
86   
87  4 final MultiTermQuery multiTermQuery = (MultiTermQuery) o;
88   
89  0 if (!term.equals(multiTermQuery.term)) return false;
90   
91  4 return getBoost() == multiTermQuery.getBoost();
92    }
93   
 
94  0 toggle public int hashCode() {
95  0 return term.hashCode() + Float.floatToRawIntBits(getBoost());
96    }
97    }