pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / spans / SpanPositionRangeQuery.java
1 package org.apache.lucene.search.spans;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  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
20 import org.apache.lucene.util.ToStringUtils;
21
22 import java.io.IOException;
23
24
25 /**
26  * Checks to see if the {@link #getMatch()} lies between a start and end position
27  *
28  * @see org.apache.lucene.search.spans.SpanFirstQuery for a derivation that is optimized for the case where start position is 0
29  */
30 public class SpanPositionRangeQuery extends SpanPositionCheckQuery {
31   protected int start = 0;
32   protected int end;
33
34   public SpanPositionRangeQuery(SpanQuery match, int start, int end) {
35     super(match);
36     this.start = start;
37     this.end = end;
38   }
39
40
41   @Override
42   protected AcceptStatus acceptPosition(Spans spans) throws IOException {
43     assert spans.start() != spans.end();
44     if (spans.start() >= end)
45       return AcceptStatus.NO_AND_ADVANCE;
46     else if (spans.start() >= start && spans.end() <= end)
47       return AcceptStatus.YES;
48     else
49       return AcceptStatus.NO;
50   }
51
52
53   /**
54    * @return The minimum position permitted in a match
55    */
56   public int getStart() {
57     return start;
58   }
59
60   /**
61    * @return the maximum end position permitted in a match.
62    */
63   public int getEnd() {
64     return end;
65   }
66
67   @Override
68   public String toString(String field) {
69     StringBuilder buffer = new StringBuilder();
70     buffer.append("spanPosRange(");
71     buffer.append(match.toString(field));
72     buffer.append(", ").append(start).append(", ");
73     buffer.append(end);
74     buffer.append(")");
75     buffer.append(ToStringUtils.boost(getBoost()));
76     return buffer.toString();
77   }
78
79   @Override
80   public Object clone() {
81     SpanPositionRangeQuery result = new SpanPositionRangeQuery((SpanQuery) match.clone(), start, end);
82     result.setBoost(getBoost());
83     return result;
84   }
85
86   @Override
87   public boolean equals(Object o) {
88     if (this == o) return true;
89     if (!(o instanceof SpanPositionRangeQuery)) return false;
90
91     SpanPositionRangeQuery other = (SpanPositionRangeQuery)o;
92     return this.end == other.end && this.start == other.start
93          && this.match.equals(other.match)
94          && this.getBoost() == other.getBoost();
95   }
96
97   @Override
98   public int hashCode() {
99     int h = match.hashCode();
100     h ^= (h << 8) | (h >>> 25);  // reversible
101     h ^= Float.floatToRawIntBits(getBoost()) ^ end ^ start;
102     return h;
103   }
104
105 }