KickJava   Java API By Example, From Geeks To Geeks.

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


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 /** Removes matches which overlap with another SpanQuery. */
28 public class SpanNotQuery extends SpanQuery {
29   private SpanQuery include;
30   private SpanQuery exclude;
31
32   /** Construct a SpanNotQuery matching spans from <code>include</code> which
33    * have no overlap with spans from <code>exclude</code>.*/

34   public SpanNotQuery(SpanQuery include, SpanQuery exclude) {
35     this.include = include;
36     this.exclude = exclude;
37
38     if (!include.getField().equals(exclude.getField()))
39       throw new IllegalArgumentException JavaDoc("Clauses must have same field.");
40   }
41
42   /** Return the SpanQuery whose matches are filtered. */
43   public SpanQuery getInclude() { return include; }
44
45   /** Return the SpanQuery whose matches must not overlap those returned. */
46   public SpanQuery getExclude() { return exclude; }
47
48   public String JavaDoc getField() { return include.getField(); }
49
50   public Collection JavaDoc getTerms() { return include.getTerms(); }
51
52   public String JavaDoc toString(String JavaDoc field) {
53     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
54     buffer.append("spanNot(");
55     buffer.append(include.toString(field));
56     buffer.append(", ");
57     buffer.append(exclude.toString(field));
58     buffer.append(")");
59     buffer.append(ToStringUtils.boost(getBoost()));
60     return buffer.toString();
61   }
62
63
64   public Spans getSpans(final IndexReader reader) throws IOException JavaDoc {
65     return new Spans() {
66         private Spans includeSpans = include.getSpans(reader);
67         private boolean moreInclude = true;
68
69         private Spans excludeSpans = exclude.getSpans(reader);
70         private boolean moreExclude = true;
71
72         public boolean next() throws IOException JavaDoc {
73           if (moreInclude) // move to next include
74
moreInclude = includeSpans.next();
75
76           while (moreInclude && moreExclude) {
77
78             if (includeSpans.doc() > excludeSpans.doc()) // skip exclude
79
moreExclude = excludeSpans.skipTo(includeSpans.doc());
80
81             while (moreExclude // while exclude is before
82
&& includeSpans.doc() == excludeSpans.doc()
83                    && excludeSpans.end() <= includeSpans.start()) {
84               moreExclude = excludeSpans.next(); // increment exclude
85
}
86
87             if (!moreExclude // if no intersection
88
|| includeSpans.doc() != excludeSpans.doc()
89                 || includeSpans.end() <= excludeSpans.start())
90               break; // we found a match
91

92             moreInclude = includeSpans.next(); // intersected: keep scanning
93
}
94           return moreInclude;
95         }
96
97         public boolean skipTo(int target) throws IOException JavaDoc {
98           if (moreInclude) // skip include
99
moreInclude = includeSpans.skipTo(target);
100
101           if (!moreInclude)
102             return false;
103
104           if (moreExclude // skip exclude
105
&& includeSpans.doc() > excludeSpans.doc())
106             moreExclude = excludeSpans.skipTo(includeSpans.doc());
107
108           while (moreExclude // while exclude is before
109
&& includeSpans.doc() == excludeSpans.doc()
110                  && excludeSpans.end() <= includeSpans.start()) {
111             moreExclude = excludeSpans.next(); // increment exclude
112
}
113
114           if (!moreExclude // if no intersection
115
|| includeSpans.doc() != excludeSpans.doc()
116                 || includeSpans.end() <= excludeSpans.start())
117             return true; // we found a match
118

119           return next(); // scan to next match
120
}
121
122         public int doc() { return includeSpans.doc(); }
123         public int start() { return includeSpans.start(); }
124         public int end() { return includeSpans.end(); }
125
126         public String JavaDoc toString() {
127           return "spans(" + SpanNotQuery.this.toString() + ")";
128         }
129
130       };
131   }
132
133   public Query rewrite(IndexReader reader) throws IOException JavaDoc {
134     SpanNotQuery clone = null;
135
136     SpanQuery rewrittenInclude = (SpanQuery) include.rewrite(reader);
137     if (rewrittenInclude != include) {
138       clone = (SpanNotQuery) this.clone();
139       clone.include = rewrittenInclude;
140     }
141     SpanQuery rewrittenExclude = (SpanQuery) exclude.rewrite(reader);
142     if (rewrittenExclude != exclude) {
143       if (clone == null) clone = (SpanNotQuery) this.clone();
144       clone.exclude = rewrittenExclude;
145     }
146
147     if (clone != null) {
148       return clone; // some clauses rewrote
149
} else {
150       return this; // no clauses rewrote
151
}
152   }
153
154     /** Returns true iff <code>o</code> is equal to this. */
155   public boolean equals(Object JavaDoc o) {
156     if (this == o) return true;
157     if (!(o instanceof SpanNotQuery)) return false;
158
159     SpanNotQuery other = (SpanNotQuery)o;
160     return this.include.equals(other.include)
161             && this.exclude.equals(other.exclude)
162             && this.getBoost() == other.getBoost();
163   }
164
165   public int hashCode() {
166     int h = include.hashCode();
167     h = (h<<1) | (h >>> 31); // rotate left
168
h ^= include.hashCode();
169     h = (h<<1) | (h >>> 31); // rotate left
170
h ^= Float.floatToRawIntBits(getBoost());
171     return h;
172   }
173
174 }
175
Popular Tags