KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > spans > SpanFirstQuery


1 package org.apache.lucene.search.spans;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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 import java.io.IOException JavaDoc;
20
21 import java.util.Collection JavaDoc;
22
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.search.Query;
25 import org.apache.lucene.util.ToStringUtils;
26
27 /** Matches spans near the beginning of a field. */
28 public class SpanFirstQuery extends SpanQuery {
29   private SpanQuery match;
30   private int end;
31
32   /** Construct a SpanFirstQuery matching spans in <code>match</code> whose end
33    * position is less than or equal to <code>end</code>. */

34   public SpanFirstQuery(SpanQuery match, int end) {
35     this.match = match;
36     this.end = end;
37   }
38
39   /** Return the SpanQuery whose matches are filtered. */
40   public SpanQuery getMatch() { return match; }
41
42   /** Return the maximum end position permitted in a match. */
43   public int getEnd() { return end; }
44
45   public String JavaDoc getField() { return match.getField(); }
46
47   public Collection JavaDoc getTerms() { return match.getTerms(); }
48
49   public String JavaDoc toString(String JavaDoc field) {
50     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
51     buffer.append("spanFirst(");
52     buffer.append(match.toString(field));
53     buffer.append(", ");
54     buffer.append(end);
55     buffer.append(")");
56     buffer.append(ToStringUtils.boost(getBoost()));
57     return buffer.toString();
58   }
59
60   public Spans getSpans(final IndexReader reader) throws IOException JavaDoc {
61     return new Spans() {
62         private Spans spans = match.getSpans(reader);
63
64         public boolean next() throws IOException JavaDoc {
65           while (spans.next()) { // scan to next match
66
if (end() <= end)
67               return true;
68           }
69           return false;
70         }
71
72         public boolean skipTo(int target) throws IOException JavaDoc {
73           if (!spans.skipTo(target))
74             return false;
75
76           if (spans.end() <= end) // there is a match
77
return true;
78
79           return next(); // scan to next match
80
}
81
82         public int doc() { return spans.doc(); }
83         public int start() { return spans.start(); }
84         public int end() { return spans.end(); }
85
86         public String JavaDoc toString() {
87           return "spans(" + SpanFirstQuery.this.toString() + ")";
88         }
89
90       };
91   }
92
93   public Query rewrite(IndexReader reader) throws IOException JavaDoc {
94     SpanFirstQuery clone = null;
95
96     SpanQuery rewritten = (SpanQuery) match.rewrite(reader);
97     if (rewritten != match) {
98       clone = (SpanFirstQuery) this.clone();
99       clone.match = rewritten;
100     }
101
102     if (clone != null) {
103       return clone; // some clauses rewrote
104
} else {
105       return this; // no clauses rewrote
106
}
107   }
108
109   public boolean equals(Object JavaDoc o) {
110     if (this == o) return true;
111     if (!(o instanceof SpanFirstQuery)) return false;
112
113     SpanFirstQuery other = (SpanFirstQuery)o;
114     return this.end == other.end
115          && this.match.equals(other.match)
116          && this.getBoost() == other.getBoost();
117   }
118
119   public int hashCode() {
120     int h = match.hashCode();
121     h ^= (h << 8) | (h >>> 25); // reversible
122
h ^= Float.floatToRawIntBits(getBoost()) ^ end;
123     return h;
124   }
125
126
127 }
128
Popular Tags