01: package org.apache.lucene.search.spans;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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: import org.apache.lucene.index.Term;
22: import org.apache.lucene.util.ToStringUtils;
23:
24: import java.io.IOException;
25: import java.util.ArrayList;
26: import java.util.Collection;
27: import java.util.Set;
28:
29: /** Matches spans containing a term. */
30: public class SpanTermQuery extends SpanQuery {
31: protected Term term;
32:
33: /** Construct a SpanTermQuery matching the named term's spans. */
34: public SpanTermQuery(Term term) {
35: this .term = term;
36: }
37:
38: /** Return the term whose spans are matched. */
39: public Term getTerm() {
40: return term;
41: }
42:
43: public String getField() {
44: return term.field();
45: }
46:
47: /** Returns a collection of all terms matched by this query.
48: * @deprecated use extractTerms instead
49: * @see #extractTerms(Set)
50: */
51: public Collection getTerms() {
52: Collection terms = new ArrayList();
53: terms.add(term);
54: return terms;
55: }
56:
57: public void extractTerms(Set terms) {
58: terms.add(term);
59: }
60:
61: public String toString(String field) {
62: StringBuffer buffer = new StringBuffer();
63: if (term.field().equals(field))
64: buffer.append(term.text());
65: else
66: buffer.append(term.toString());
67: buffer.append(ToStringUtils.boost(getBoost()));
68: return buffer.toString();
69: }
70:
71: /** Returns true iff <code>o</code> is equal to this. */
72: public boolean equals(Object o) {
73: if (!(o instanceof SpanTermQuery))
74: return false;
75: SpanTermQuery other = (SpanTermQuery) o;
76: return (this .getBoost() == other.getBoost())
77: && this .term.equals(other.term);
78: }
79:
80: /** Returns a hash code value for this object.*/
81: public int hashCode() {
82: return Float.floatToIntBits(getBoost()) ^ term.hashCode()
83: ^ 0xD23FE494;
84: }
85:
86: public Spans getSpans(final IndexReader reader) throws IOException {
87: return new TermSpans(reader.termPositions(term), term);
88: }
89:
90: }
|