Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 288   Methods: 18
NCLOC: 132   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IndexQuery.java 75% 60.9% 44.4% 58.9%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: IndexQuery.java 564424 2007-08-09 23:40:03Z natalia $
 18    */
 19   
 20    package org.apache.xindice.core.indexer;
 21   
 22    import org.apache.commons.logging.Log;
 23    import org.apache.commons.logging.LogFactory;
 24    import org.apache.xindice.core.data.Value;
 25   
 26    import java.util.Arrays;
 27   
 28    /**
 29    * IndexQuery represents the most primitive form of index querying.
 30    * Instances of this object should be created by QueryResolvers and
 31    * cached in Query instances.
 32    *
 33    * @version $Revision: 564424 $, $Date: 2007-08-09 16:40:03 -0700 (Thu, 09 Aug 2007) $
 34    */
 35    public class IndexQuery {
 36   
 37    /**
 38    * Logger used by this class
 39    */
 40    private static final Log log = LogFactory.getLog(IndexQuery.class);
 41   
 42    // No Operator
 43    public static final int ANY = 0; // Any And All Matches
 44   
 45    // Singleton Operators
 46    public static final int EQ = 1; // Equal To
 47    public static final int NEQ = -1; // Not Equal To
 48    public static final int GT = 2; // Greater Than
 49    public static final int LEQ = -2; // Less Than Or Equal To
 50    public static final int LT = 3; // Less Than
 51    public static final int GEQ = -3; // Greater Than Or Equal To
 52   
 53    // Range Operators
 54    public static final int BW = 4; // Between (Inclusive)
 55    public static final int NBW = -4; // Not Between (Inclusive)
 56    public static final int BWX = 5; // Between (Exclusive)
 57    public static final int NBWX = -5; // Not Between (Exclusive)
 58   
 59    // Set Operators
 60    public static final int IN = 6; // In The Set
 61    public static final int NIN = -6; // Not In The Set
 62   
 63    // Other operators
 64    public static final int SW = 7; // Starts-with
 65    public static final int NSW = -7; // Not Starts-with
 66   
 67    public static final int TQ = 8; // Text query
 68   
 69   
 70    protected final IndexPattern pattern;
 71    protected final int op;
 72    protected final Value[] vals;
 73   
 74   
 75    /**
 76    * ANY operator index query
 77    */
 78  0 public IndexQuery(IndexPattern pattern) {
 79  0 this.pattern = pattern;
 80  0 this.op = ANY;
 81  0 this.vals = null;
 82    }
 83   
 84    /**
 85    * IN operator index query
 86    */
 87  0 public IndexQuery(IndexPattern pattern, Value[] vals) {
 88  0 this(pattern, IN, vals);
 89    }
 90   
 91    /**
 92    * Binary operator index query
 93    */
 94  82 public IndexQuery(IndexPattern pattern, int op, Value[] vals) {
 95  82 this.pattern = pattern;
 96  82 this.op = op;
 97  82 this.vals = vals;
 98    }
 99   
 100    /**
 101    * Unary operator index query
 102    */
 103  275 public IndexQuery(IndexPattern pattern, int op, Value val) {
 104  275 this.pattern = pattern;
 105  275 this.op = op;
 106   
 107  275 if (op == SW || op == NSW) {
 108    // SW and NSW operators are treated as "between" and "not between"
 109    // where second value is same as first value plus max byte
 110  102 byte[] b = new byte[val.getLength() + 1];
 111  102 val.copyTo(b, 0);
 112  102 b[b.length - 1] = Byte.MAX_VALUE;
 113  102 Value val2 = new Value(b);
 114  102 this.vals = new Value[]{ val, val2 };
 115    } else {
 116  173 this.vals = new Value[]{ val };
 117    }
 118    }
 119   
 120    /**
 121    * EQ operator index query
 122    */
 123  0 public IndexQuery(IndexPattern pattern, Value val1) {
 124  0 this(pattern, EQ, val1);
 125    }
 126   
 127    /**
 128    * Binary operator index query
 129    */
 130  0 public IndexQuery(IndexPattern pattern, int op, Value val1, Value val2) {
 131  0 this.pattern = pattern;
 132  0 this.op = op;
 133  0 this.vals = new Value[]{val1, val2};
 134    }
 135   
 136    /**
 137    * IN operator index query
 138    */
 139  0 public IndexQuery(IndexPattern pattern, Value val1, Value val2) {
 140  0 this(pattern, IN, val1, val2);
 141    }
 142   
 143    /**
 144    * Unary operator index query
 145    */
 146  181 public IndexQuery(IndexPattern pattern, int op, String val1) {
 147  181 this(pattern, op, new Value(val1));
 148    }
 149   
 150    /**
 151    * EQ operator index query
 152    */
 153  0 public IndexQuery(IndexPattern pattern, String val1) {
 154  0 this(pattern, new Value(val1));
 155    }
 156   
 157    /**
 158    * Binary operator index query
 159    */
 160  0 public IndexQuery(IndexPattern pattern, int op, String val1, String val2) {
 161  0 this(pattern, op, new Value(val1), new Value(val2));
 162    }
 163   
 164    /**
 165    * IN operator index query
 166    */
 167  0 public IndexQuery(IndexPattern pattern, String val1, String val2) {
 168  0 this(pattern, new Value(val1), new Value(val2));
 169    }
 170   
 171   
 172    /**
 173    * getPattern returns the IndexPattern associated with this query.
 174    *
 175    * @return the IndexPattern
 176    */
 177  392 public IndexPattern getPattern() {
 178  392 return this.pattern;
 179    }
 180   
 181    /**
 182    * getOperator returns the operator associated with this query.
 183    *
 184    * @return The operator
 185    */
 186  111 public int getOperator() {
 187  111 return this.op;
 188    }
 189   
 190    /**
 191    * getValue returns one of the Values associated with this query.
 192    *
 193    * @param index The Value index
 194    * @return The request Value
 195    */
 196  6 public final Value getValue(int index) {
 197  6 return this.vals[index];
 198    }
 199   
 200    /**
 201    * getValues returns the Values associated with this query.
 202    *
 203    * @return The Value set
 204    */
 205  162 public Value[] getValues() {
 206  162 return this.vals;
 207    }
 208   
 209    /**
 210    * getLength returns the length of the Value set associated with
 211    * this query.
 212    *
 213    * @return The Value set length
 214    */
 215  0 public final int getLength() {
 216  0 return this.vals.length;
 217    }
 218   
 219   
 220    /**
 221    * testValue tests the specified value for validity against this
 222    * IndexQuery. The helper classes in org.apache.xindice.core.indexer.helpers
 223    * should be used for optimized performance.
 224    *
 225    * @param value The Value to compare
 226    * @return Whether or not the value matches
 227    */
 228  64 public boolean testValue(Value value) {
 229  64 switch (this.op) {
 230    // No Comparison (Any)
 231  0 case ANY:
 232  0 return true;
 233   
 234    // Singleton Comparisons
 235  0 case EQ:
 236  0 return value.equals(vals[0]);
 237  0 case NEQ:
 238  0 return !value.equals(vals[0]);
 239  9 case GT:
 240  9 return value.compareTo(vals[0]) > 0;
 241  4 case LEQ:
 242  4 return value.compareTo(vals[0]) <= 0;
 243  14 case LT:
 244  14 return value.compareTo(vals[0]) < 0;
 245  3 case GEQ:
 246  3 return value.compareTo(vals[0]) >= 0;
 247   
 248    // Range Comparisons
 249  5 case BW:
 250  5 return value.compareTo(vals[0]) >= 0 && value.compareTo(vals[1]) <= 0;
 251  3 case NBW:
 252  3 return value.compareTo(vals[0]) <= 0 || value.compareTo(vals[1]) >= 0;
 253  3 case BWX:
 254  3 return value.compareTo(vals[0]) > 0 && value.compareTo(vals[1]) < 0;
 255  4 case NBWX:
 256  4 return value.compareTo(vals[0]) < 0 || value.compareTo(vals[1]) > 0;
 257   
 258    // Set Comparisons
 259  3 case IN:
 260  4 case NIN:
 261  7 return Arrays.binarySearch(vals, value) >= 0 ? op == IN
 262    : op == NIN;
 263   
 264    // Other comparisons
 265  12 case SW:
 266  0 case NSW:
 267  12 return value.startsWith(vals[0]) ? op == SW : op == NSW;
 268  0 default:
 269  0 if (log.isWarnEnabled()) {
 270  0 log.warn("invalid operation : " + op);
 271    }
 272    }
 273   
 274  0 return false;
 275    }
 276   
 277    /**
 278    * testValue tests the specified value for validity against this
 279    * IndexQuery. The helper classes in org.apache.xindice.core.indexer.helpers
 280    * should be used for optimized performance.
 281    *
 282    * @param value The Value to compare
 283    * @return Whether or not the value matches
 284    */
 285  0 public final boolean testValue(String value) {
 286  0 return testValue(new Value(value));
 287    }
 288    }