add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / spans / SpanFirstQuery.java
1 package org.apache.lucene.search.spans;
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.util.ToStringUtils;
21
22 import java.io.IOException;
23
24 /** Matches spans near the beginning of a field.
25  * <p/> 
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.
28  *
29  *
30  *  */
31 public class SpanFirstQuery extends SpanPositionRangeQuery {
32
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) {
36     super(match, 0, end);
37   }
38
39   @Override
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;
46     else
47       return AcceptStatus.NO;
48   }
49
50
51   @Override
52   public String toString(String field) {
53     StringBuilder buffer = new StringBuilder();
54     buffer.append("spanFirst(");
55     buffer.append(match.toString(field));
56     buffer.append(", ");
57     buffer.append(end);
58     buffer.append(")");
59     buffer.append(ToStringUtils.boost(getBoost()));
60     return buffer.toString();
61   }
62
63   @Override
64   public Object clone() {
65     SpanFirstQuery spanFirstQuery = new SpanFirstQuery((SpanQuery) match.clone(), end);
66     spanFirstQuery.setBoost(getBoost());
67     return spanFirstQuery;
68   }
69
70   @Override
71   public boolean equals(Object o) {
72     if (this == o) return true;
73     if (!(o instanceof SpanFirstQuery)) return false;
74
75     SpanFirstQuery other = (SpanFirstQuery)o;
76     return this.end == other.end
77          && this.match.equals(other.match)
78          && this.getBoost() == other.getBoost();
79   }
80
81   @Override
82   public int hashCode() {
83     int h = match.hashCode();
84     h ^= (h << 8) | (h >>> 25);  // reversible
85     h ^= Float.floatToRawIntBits(getBoost()) ^ end;
86     return h;
87   }
88
89
90 }