Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart9.png 37% of files have more coverage
66   167   32   7.33
52   105   0.48   9
9     3.56  
1    
 
  RangeQuery       Line # 32 66 32 85% 0.8503937
 
  (13)
 
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    /**
27    * A Query that matches documents within an exclusive range. A RangeQuery
28    * is built by QueryParser for input like <code>[010 TO 120]</code>.
29    *
30    * @version $Id: RangeQuery.java 358693 2005-12-23 03:37:50Z yonik $
31    */
 
32    public class RangeQuery extends Query
33    {
34    private Term lowerTerm;
35    private Term upperTerm;
36    private boolean inclusive;
37   
38    /** Constructs a query selecting all terms greater than
39    * <code>lowerTerm</code> but less than <code>upperTerm</code>.
40    * There must be at least one term and either term may be null,
41    * in which case there is no bound on that side, but if there are
42    * two terms, both terms <b>must</b> be for the same field.
43    */
 
44  47 toggle public RangeQuery(Term lowerTerm, Term upperTerm, boolean inclusive)
45    {
46  47 if (lowerTerm == null && upperTerm == null)
47    {
48  0 throw new IllegalArgumentException("At least one term must be non-null");
49    }
50  47 if (lowerTerm != null && upperTerm != null && lowerTerm.field() != upperTerm.field())
51    {
52  0 throw new IllegalArgumentException("Both terms must be for the same field");
53    }
54   
55    // if we have a lowerTerm, start there. otherwise, start at beginning
56  47 if (lowerTerm != null) {
57  44 this.lowerTerm = lowerTerm;
58    }
59    else {
60  3 this.lowerTerm = new Term(upperTerm.field(), "");
61    }
62   
63  47 this.upperTerm = upperTerm;
64  47 this.inclusive = inclusive;
65    }
66   
 
67  20 toggle public Query rewrite(IndexReader reader) throws IOException {
68   
69  20 BooleanQuery query = new BooleanQuery(true);
70  20 TermEnum enumerator = reader.terms(lowerTerm);
71   
72  20 try {
73   
74  20 boolean checkLower = false;
75  20 if (!inclusive) // make adjustments to set to exclusive
76  9 checkLower = true;
77   
78  20 String testField = getField();
79   
80  20 do {
81  56 Term term = enumerator.term();
82  56 if (term != null && term.field() == testField) {
83  54 if (!checkLower || term.text().compareTo(lowerTerm.text()) > 0) {
84  51 checkLower = false;
85  51 if (upperTerm != null) {
86  51 int compare = upperTerm.text().compareTo(term.text());
87    /* if beyond the upper term, or is exclusive and
88    * this is equal to the upper term, break out */
89  51 if ((compare < 0) || (!inclusive && compare == 0))
90  15 break;
91    }
92  36 TermQuery tq = new TermQuery(term); // found a match
93  36 tq.setBoost(getBoost()); // set the boost
94  36 query.add(tq, BooleanClause.Occur.SHOULD); // add to query
95    }
96    }
97    else {
98  2 break;
99    }
100    }
101  39 while (enumerator.next());
102    }
103    finally {
104  20 enumerator.close();
105    }
106  20 return query;
107    }
108   
109    /** Returns the field name for this query */
 
110  47 toggle public String getField() {
111  47 return (lowerTerm != null ? lowerTerm.field() : upperTerm.field());
112    }
113   
114    /** Returns the lower term of this range query */
 
115  0 toggle public Term getLowerTerm() { return lowerTerm; }
116   
117    /** Returns the upper term of this range query */
 
118  0 toggle public Term getUpperTerm() { return upperTerm; }
119   
120    /** Returns <code>true</code> if the range query is inclusive */
 
121  0 toggle public boolean isInclusive() { return inclusive; }
122   
123   
124    /** Prints a user-readable version of this query. */
 
125  22 toggle public String toString(String field)
126    {
127  22 StringBuffer buffer = new StringBuffer();
128  22 if (!getField().equals(field))
129    {
130  5 buffer.append(getField());
131  5 buffer.append(":");
132    }
133  22 buffer.append(inclusive ? "[" : "{");
134  22 buffer.append(lowerTerm != null ? lowerTerm.text() : "null");
135  22 buffer.append(" TO ");
136  22 buffer.append(upperTerm != null ? upperTerm.text() : "null");
137  22 buffer.append(inclusive ? "]" : "}");
138  22 buffer.append(ToStringUtils.boost(getBoost()));
139  22 return buffer.toString();
140    }
141   
142    /** Returns true iff <code>o</code> is equal to this. */