1 package org.apache.lucene.search.spans;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import org.apache.lucene.util.ToStringUtils;
22 import java.io.IOException;
24 /** Matches spans near the beginning of a field.
26 * This class is a simple extension of {@link SpanPositionRangeQuery} in that it assumes the
27 * start to be zero and only checks the end boundary.
31 public class SpanFirstQuery extends SpanPositionRangeQuery {
33 /** Construct a SpanFirstQuery matching spans in <code>match</code> whose end
34 * position is less than or equal to <code>end</code>. */
35 public SpanFirstQuery(SpanQuery match, int end) {
40 protected AcceptStatus acceptPosition(Spans spans) throws IOException {
41 assert spans.start() != spans.end();
42 if (spans.start() >= end)
43 return AcceptStatus.NO_AND_ADVANCE;
44 else if (spans.end() <= end)
45 return AcceptStatus.YES;
47 return AcceptStatus.NO;
52 public String toString(String field) {
53 StringBuilder buffer = new StringBuilder();
54 buffer.append("spanFirst(");
55 buffer.append(match.toString(field));
59 buffer.append(ToStringUtils.boost(getBoost()));
60 return buffer.toString();
64 public Object clone() {
65 SpanFirstQuery spanFirstQuery = new SpanFirstQuery((SpanQuery) match.clone(), end);
66 spanFirstQuery.setBoost(getBoost());
67 return spanFirstQuery;
71 public boolean equals(Object o) {
72 if (this == o) return true;
73 if (!(o instanceof SpanFirstQuery)) return false;
75 SpanFirstQuery other = (SpanFirstQuery)o;
76 return this.end == other.end
77 && this.match.equals(other.match)
78 && this.getBoost() == other.getBoost();
82 public int hashCode() {
83 int h = match.hashCode();
84 h ^= (h << 8) | (h >>> 25); // reversible
85 h ^= Float.floatToRawIntBits(getBoost()) ^ end;