Clover coverage report - lucene-core-2.2.0
Coverage timestamp: Mon Jun 18 2007 18:51:12 PDT
file stats: LOC: 139   Methods: 10
NCLOC: 81   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConstantScoreRangeQuery.java 71.7% 84.3% 60% 76.6%
coverage coverage
 1    package org.apache.lucene.search;
 2   
 3    /**
 4    * Licensed to the Apache Software Foundation (ASF) under one or more
 5    * contributor license agreements. See the NOTICE file distributed with
 6    * this work for additional information regarding copyright ownership.
 7    * The ASF licenses this file to You under the Apache License, Version 2.0
 8    * (the "License"); you may not use this file except in compliance with
 9    * the License. You may obtain a copy of the License at
 10    *
 11    * http://www.apache.org/licenses/LICENSE-2.0
 12    *
 13    * Unless required by applicable law or agreed to in writing, software
 14    * distributed under the License is distributed on an "AS IS" BASIS,
 15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16    * See the License for the specific language governing permissions and
 17    * limitations under the License.
 18    */
 19   
 20    import org.apache.lucene.index.IndexReader;
 21   
 22    import java.io.IOException;
 23   
 24    /**
 25    * A range query that returns a constant score equal to its boost for
 26    * all documents in the range.
 27    * <p>
 28    * It does not have an upper bound on the number of clauses covered in the range.
 29    * <p>
 30    * If an endpoint is null, it is said to be "open".
 31    * Either or both endpoints may be open. Open endpoints may not be exclusive
 32    * (you can't select all but the first or last term without explicitly specifying the term to exclude.)
 33    *
 34    * @author yonik
 35    * @version $Id: ConstantScoreRangeQuery.java 472959 2006-11-09 16:21:50Z yonik $
 36    */
 37   
 38    public class ConstantScoreRangeQuery extends Query
 39    {
 40    private final String fieldName;
 41    private final String lowerVal;
 42    private final String upperVal;
 43    private final boolean includeLower;
 44    private final boolean includeUpper;
 45   
 46   
 47  86 public ConstantScoreRangeQuery(String fieldName, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper)
 48    {
 49    // do a little bit of normalization...
 50    // open ended range queries should always be inclusive.
 51  86 if (lowerVal==null) {
 52  6 includeLower=true;
 53  80 } else if (includeLower && lowerVal.equals("")) {
 54  0 lowerVal=null;
 55    }
 56  86 if (upperVal==null) {
 57  6 includeUpper=true;
 58    }
 59   
 60   
 61  86 this.fieldName = fieldName.intern(); // intern it, just like terms...
 62  86 this.lowerVal = lowerVal;
 63  86 this.upperVal = upperVal;
 64  86 this.includeLower = includeLower;
 65  86 this.includeUpper = includeUpper;
 66    }
 67   
 68    /** Returns the field name for this query */
 69  38 public String getField() { return fieldName; }
 70    /** Returns the value of the lower endpoint of this range query, null if open ended */
 71  0 public String getLowerVal() { return lowerVal; }
 72    /** Returns the value of the upper endpoint of this range query, null if open ended */
 73  0 public String getUpperVal() { return upperVal; }
 74    /** Returns <code>true</code> if the lower endpoint is inclusive */
 75  0 public boolean includesLower() { return includeLower; }
 76    /** Returns <code>true</code> if the upper endpoint is inclusive */
 77  0 public boolean includesUpper() { return includeUpper; }
 78   
 79  50 public Query rewrite(IndexReader reader) throws IOException {
 80    // Map to RangeFilter semantics which are slightly different...
 81  50 RangeFilter rangeFilt = new RangeFilter(fieldName,
 82  50 lowerVal!=null?lowerVal:"",
 83  50 upperVal, lowerVal==""?false:includeLower, upperVal==null?false:includeUpper);
 84  50 Query q = new ConstantScoreQuery(rangeFilt);
 85  50 q.setBoost(getBoost());
 86  50 return q;
 87    }
 88   
 89    /** Prints a user-readable version of this query. */
 90  33 public String toString(String field)
 91    {
 92  33 StringBuffer buffer = new StringBuffer();
 93  33 if (!getField().equals(field))
 94    {
 95  5 buffer.append(getField());
 96  5 buffer.append(":");
 97    }
 98  33 buffer.append(includeLower ? '[' : '{');
 99  33 buffer.append(lowerVal != null ? lowerVal : "*");
 100  33 buffer.append(" TO ");
 101  33 buffer.append(upperVal != null ? upperVal : "*");
 102  33 buffer.append(includeUpper ? ']' : '}');
 103  33 if (getBoost() != 1.0f)
 104    {
 105  1 buffer.append("^");
 106  1 buffer.append(Float.toString(getBoost()));
 107    }
 108  33 return buffer.toString();
 109    }
 110   
 111    /** Returns true if <code>o</code> is equal to this. */
 112  10 public boolean equals(Object o) {
 113  0 if (this == o) return true;
 114  2 if (!(o instanceof ConstantScoreRangeQuery)) return false;
 115  8 ConstantScoreRangeQuery other = (ConstantScoreRangeQuery) o;
 116   
 117  8 if (this.fieldName != other.fieldName // interned comparison
 118    || this.includeLower != other.includeLower
 119    || this.includeUpper != other.includeUpper
 120  0 ) { return false; }
 121  2 if (this.lowerVal != null ? !this.lowerVal.equals(other.lowerVal) : other.lowerVal != null) return false;
 122  0 if (this.upperVal != null ? !this.upperVal.equals(other.upperVal) : other.upperVal != null) return false;
 123  6 return this.getBoost() == other.getBoost();
 124    }
 125   
 126    /** Returns a hash code value for this object.*/
 127  12 public int hashCode() {
 128  12 int h = Float.floatToIntBits(getBoost()) ^ fieldName.hashCode();
 129    // hashCode of "" is 0, so don't use that for null...
 130  12 h ^= lowerVal != null ? lowerVal.hashCode() : 0x965a965a;
 131    // don't just XOR upperVal with out mixing either it or h, as it will cancel
 132    // out lowerVal if they are equal.
 133  12 h ^= (h << 17) | (h >>> 16); // a reversible (one to one) 32 bit mapping mix
 134  12 h ^= (upperVal != null ? (upperVal.hashCode()) : 0x5a695a69);
 135  12 h ^= (includeLower ? 0x665599aa : 0)
 136  12 ^ (includeUpper ? 0x99aa5566 : 0);
 137  12 return h;
 138    }
 139    }