Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart8.png 55% of files have more coverage
29   90   12   4.83
8   57   0.41   6
6     2  
1    
 
  PrefixQuery       Line # 28 29 12 74.4% 0.74418604
 
  (7)
 
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.Term;
22    import org.apache.lucene.index.TermEnum;
23    import org.apache.lucene.index.IndexReader;
24    import org.apache.lucene.util.ToStringUtils;
25   
26    /** A Query that matches documents containing terms with a specified prefix. A PrefixQuery
27    * is built by QueryParser for input like <code>app*</code>. */
 
28    public class PrefixQuery extends Query {
29    private Term prefix;
30   
31    /** Constructs a query for terms starting with <code>prefix</code>. */
 
32  26 toggle public PrefixQuery(Term prefix) {
33  26 this.prefix = prefix;
34    }
35   
36    /** Returns the prefix of this query. */
 
37  0 toggle public Term getPrefix() { return prefix; }
38   
 
39  7 toggle public Query rewrite(IndexReader reader) throws IOException {
40  7 BooleanQuery query = new BooleanQuery(true);
41  7 TermEnum enumerator = reader.terms(prefix);
42  7 try {
43  7 String prefixText = prefix.text();
44  7 String prefixField = prefix.field();
45  7 do {
46  24 Term term = enumerator.term();
47  24 if (term != null &&
48    term.text().startsWith(prefixText) &&
49    term.field() == prefixField) {
50  20 TermQuery tq = new TermQuery(term); // found a match
51  20 tq.setBoost(getBoost()); // set the boost
52  20 query.add(tq, BooleanClause.Occur.SHOULD); // add to query
53    //System.out.println("added " + term);
54    } else {
55  4 break;
56    }
57  20 } while (enumerator.next());
58    } finally {
59  7 enumerator.close();
60    }
61  7 return query;
62    }
63   
64    /** Prints a user-readable version of this query. */
 
65  20 toggle public String toString(String field) {
66  20 StringBuffer buffer = new StringBuffer();
67  20 if (!prefix.field().equals(field)) {
68  11 buffer.append(prefix.field());
69  11 buffer.append(":");
70    }
71  20 buffer.append(prefix.text());
72  20 buffer.append('*');
73  20 buffer.append(ToStringUtils.boost(getBoost()));
74  20 return buffer.toString();
75    }
76   
77    /** Returns true iff <code>o</code> is equal to this. */
 
78  0 toggle public boolean equals(Object o) {
79  0 if (!(o instanceof PrefixQuery))
80  0 return false;
81  0 PrefixQuery other = (PrefixQuery)o;
82  0 return (this.getBoost() == other.getBoost())
83    && this.prefix.equals(other.prefix);
84    }
85   
86    /** Returns a hash code value for this object.*/
 
87  0 toggle public int hashCode() {
88  0 return Float.floatToIntBits(getBoost()) ^ prefix.hashCode() ^ 0x6634D93C;
89    }
90    }