Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart6.png 74% of files have more coverage
51   138   23   5.1
46   81   0.45   10
10     2.3  
1    
 
  ConstantScoreRangeQuery       Line # 37 51 23 52.3% 0.5233645
 
  (6)
 
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   
21    import java.io.IOException;
22   
23    /**
24    * A range query that returns a constant score equal to its boost for
25    * all documents in the range.
26    * <p>
27    * It does not have an upper bound on the number of clauses covered in the range.
28    * <p>
29    * If an endpoint is null, it is said to be "open".
30    * Either or both endpoints may be open. Open endpoints may not be exclusive
31    * (you can't select all but the first or last term without explicitly specifying the term to exclude.)
32    *
33    * @author yonik
34    * @version $Id: ConstantScoreRangeQuery.java 386735 2006-03-17 21:55:13Z dnaber $
35    */
36   
 
37    public class ConstantScoreRangeQuery extends Query
38    {
39    private final String fieldName;
40    private final String lowerVal;
41    private final String upperVal;
42    private final boolean includeLower;
43    private final boolean includeUpper;
44   
45   
 
46  45 toggle public ConstantScoreRangeQuery(String fieldName, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper)
47    {
48    // do a little bit of normalization...
49    // open ended range queries should always be inclusive.
50  45 if (lowerVal==null) {
51  6 includeLower=true;
52  39 } else if (includeLower && lowerVal.equals("")) {
53  0 lowerVal=null;
54    }
55  45 if (upperVal==null) {
56  6 includeUpper=true;
57    }
58   
59   
60  45 this.fieldName = fieldName.intern(); // intern it, just like terms...
61  45 this.lowerVal = lowerVal;
62  45 this.upperVal = upperVal;
63  45 this.includeLower = includeLower;
64  45 this.includeUpper = includeUpper;
65    }
66   
67    /** Returns the field name for this query */
 
68  0 toggle public String getField() { return fieldName; }
69    /** Returns the value of the lower endpoint of this range query, null if open ended */
 
70  0 toggle public String getLowerVal() { return lowerVal; }
71    /** Returns the value of the upper endpoint of this range query, null if open ended */
 
72  0 toggle public String getUpperVal() { return upperVal; }
73    /** Returns <code>true</code> if the lower endpoint is inclusive */
 
74  0 toggle public boolean includesLower() { return includeLower; }
75    /** Returns <code>true</code> if the upper endpoint is inclusive */
 
76  0 toggle public boolean includesUpper() { return includeUpper; }
77   
 
78  41 toggle public Query rewrite(IndexReader reader) throws IOException {
79    // Map to RangeFilter semantics which are slightly different...
80  41 RangeFilter rangeFilt = new RangeFilter(fieldName,
81  41 lowerVal!=null?lowerVal:"",
82  41 upperVal, lowerVal==""?false:includeLower, upperVal==null?false:includeUpper);
83  41 Query q = new ConstantScoreQuery(rangeFilt);
84  41 q.setBoost(getBoost());
85  41 return q;
86    }
87   
88    /** Prints a user-readable version of this query. */
 
89  0 toggle public String toString(String field)
90    {
91  0 StringBuffer buffer = new StringBuffer();
92  0 if (!getField().equals(field))
93    {
94  0 buffer.append(getField());
95  0 buffer.append(":");
96    }
97  0 buffer.append(includeLower ? '[' : '{');
98  0 buffer.append(lowerVal != null ? lowerVal : "*");
99  0 buffer.append(" TO ");
100  0 buffer.append(upperVal != null ? upperVal : "*");
101  0 buffer.append(includeUpper ? ']' : '}');
102  0 if (getBoost() != 1.0f)
103    {
104  0 buffer.append("^");
105  0 buffer.append(Float.toString(getBoost()));
106    }
107  0 return buffer.toString();
108    }
109   
110    /** Returns true if <code>o</code> is equal to this. */
 
111  10 toggle public boolean equals(Object o) {
112  0 if (this == o) return true;
113  10 if (!(o instanceof ConstantScoreRangeQuery)) return false;
114  8 ConstantScoreRangeQuery other = (ConstantScoreRangeQuery) o;
115   
116  8 if (this.fieldName != other.fieldName // interned comparison
117    || this.includeLower != other.includeLower
118    || this.includeUpper != other.includeUpper
119  0 ) { return false; }
120  8 if (this.lowerVal != null ? !this.lowerVal.equals(other.lowerVal) : other.lowerVal != null) return false;
121  0 if (this.upperVal != null ? !this.upperVal.equals(other.upperVal) : other.upperVal != null) return false;
122  6 return this.getBoost() == other.getBoost();
123    }
124   
125    /** Returns a hash code value for this object.*/
 
126  12 toggle public int hashCode() {
127  12 int h = Float.floatToIntBits(getBoost()) ^ fieldName.hashCode();
128    // hashCode of "" is 0, so don't use that for null...
129  12 h ^= lowerVal != null ? lowerVal.hashCode() : 0x965a965a;
130    // don't just XOR upperVal with out mixing either it or h, as it will cancel
131    // out lowerVal if they are equal.
132  12 h ^= (h << 17) | (h >>> 16); // a reversible (one to one) 32 bit mapping mix
133  12 h ^= (upperVal != null ? (upperVal.hashCode()) : 0x5a695a69);
134  12 h ^= (includeLower ? 0x665599aa : 0)
135  12 ^ (includeUpper ? 0x99aa5566 : 0);
136  12 return h;
137    }
138    }