Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart5.png 77% of files have more coverage
68   195   35   9.71
54   121   0.51   7
7     5  
1    
 
  RangeFilter       Line # 36 68 35 44.2% 0.44186047
 
  (9)
 
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 org.apache.lucene.index.IndexReader;
20    import org.apache.lucene.index.Term;
21    import org.apache.lucene.index.TermDocs;
22    import org.apache.lucene.index.TermEnum;
23   
24    import java.io.IOException;
25    import java.util.BitSet;
26   
27    /**
28    * A Filter that restricts search results to a range of values in a given
29    * field.
30    *
31    * <p>
32    * This code borrows heavily from {@link RangeQuery}, but is implemented as a Filter
33    *
34    * </p>
35    */
 
36    public class RangeFilter extends Filter {
37   
38    private String fieldName;
39    private String lowerTerm;
40    private String upperTerm;
41    private boolean includeLower;
42    private boolean includeUpper;
43   
44    /**
45    * @param fieldName The field this range applies to
46    * @param lowerTerm The lower bound on this range
47    * @param upperTerm The upper bound on this range
48    * @param includeLower Does this range include the lower bound?
49    * @param includeUpper Does this range include the upper bound?
50    * @throws IllegalArgumentException if both terms are null or if
51    * lowerTerm is null and includeLower is true (similar for upperTerm
52    * and includeUpper)
53    */
 
54  79 toggle public RangeFilter(String fieldName, String lowerTerm, String upperTerm,
55    boolean includeLower, boolean includeUpper) {
56  79 this.fieldName = fieldName;
57  79 this.lowerTerm = lowerTerm;
58  79 this.upperTerm = upperTerm;
59  79 this.includeLower = includeLower;
60  79 this.includeUpper = includeUpper;
61   
62  79 if (null == lowerTerm && null == upperTerm) {
63  0 throw new IllegalArgumentException
64    ("At least one value must be non-null");
65    }
66  79 if (includeLower && null == lowerTerm) {
67  0 throw new IllegalArgumentException
68    ("The lower bound must be non-null to be inclusive");
69    }
70  79 if (includeUpper && null == upperTerm) {
71  0 throw new IllegalArgumentException
72    ("The upper bound must be non-null to be inclusive");
73    }
74    }
75   
76    /**
77    * Constructs a filter for field <code>fieldName</code> matching
78    * less than or equal to <code>upperTerm</code>.
79    */
 
80  0 toggle public static RangeFilter Less(String fieldName, String upperTerm) {
81  0 return new RangeFilter(fieldName, null, upperTerm, false, true);
82    }
83   
84    /**
85    * Constructs a filter for field <code>fieldName</code> matching
86    * greater than or equal to <code>lowerTerm</code>.
87    */
 
88  0 toggle public static RangeFilter More(String fieldName, String lowerTerm) {
89  0 return new RangeFilter(fieldName, lowerTerm, null, true, false);
90    }
91   
92    /**
93    * Returns a BitSet with true for documents which should be
94    * permitted in search results, and false for those that should
95    * not.
96    */
 
97  83 toggle public BitSet bits(IndexReader reader) throws IOException {
98  83 BitSet bits = new BitSet(reader.maxDoc());
99  83 TermEnum enumerator =
100  83 (null != lowerTerm
101    ? reader.terms(new Term(fieldName, lowerTerm))
102    : reader.terms(new Term(fieldName,"")));
103   
104  83 try {
105   
106  83 if (enumerator.term() == null) {
107  2 return bits;
108    }
109   
110  81 boolean checkLower = false;
111  81 if (!includeLower) // make adjustments to set to exclusive
112  32 checkLower = true;
113   
114  81 TermDocs termDocs = reader.termDocs();
115  81 try {
116   
117  81 do {
118  360141 Term term = enumerator.term();
119  360141 if (term != null && term.field().equals(fieldName)) {
120  360121 if (!checkLower || null==lowerTerm || term.text().compareTo(lowerTerm) > 0) {
121  360097 checkLower = false;
122  360097 if (upperTerm != null) {
123  280089 int compare = upperTerm.compareTo(term.text());
124    /* if beyond the upper term, or is exclusive and
125    * this is equal to the upper term, break out */
126  280089 if ((compare < 0) ||
127    (!includeUpper && compare==0)) {
128  41 break;
129    }
130    }
131    /* we have a good term, find the docs */
132   
133  360056 termDocs.seek(enumerator.term());
134  720157 while (termDocs.next()) {
135  360101 bits.set(termDocs.doc());
136    }
137    }
138    } else {
139  20 break;
140    }
141    }
142  360080 while (enumerator.next());
143   
144    } finally {
145  81 termDocs.close();
146    }
147    } finally {
148  83 enumerator.close();
149    }
150   
151  81 return bits;
152    }
153   
 
154  0 toggle public String toString() {
155  0 StringBuffer buffer = new StringBuffer();
156  0 buffer.append(fieldName);
157  0 buffer.append(":");
158  0 buffer.append(includeLower ? "[" : "{");
159  0 if (null != lowerTerm) {
160  0 buffer.append(lowerTerm);
161    }
162  0 buffer.append("-");
163  0 if (null != upperTerm) {
164  0 buffer.append(upperTerm);
165    }
166  0 buffer.append(includeUpper ? "]" : "}");
167  0 return buffer.toString();
168    }
169   
170    /** Returns true if <code>o</code> is equal to this. */