cvsimport
[beagle.git] / beagled / Lucene.Net / Search / Spans / SpanNotQuery.cs
blobf2bcd56b384939488c26aa30a5605d6ee2762197
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 using Query = Lucene.Net.Search.Query;
20 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
22 namespace Lucene.Net.Search.Spans
25 /// <summary>Removes matches which overlap with another SpanQuery. </summary>
26 [Serializable]
27 public class SpanNotQuery : SpanQuery
29 private class AnonymousClassSpans : Spans
31 public AnonymousClassSpans(Lucene.Net.Index.IndexReader reader, SpanNotQuery enclosingInstance)
33 InitBlock(reader, enclosingInstance);
35 private void InitBlock(Lucene.Net.Index.IndexReader reader, SpanNotQuery enclosingInstance)
37 this.reader = reader;
38 this.enclosingInstance = enclosingInstance;
39 includeSpans = Enclosing_Instance.include.GetSpans(reader);
40 excludeSpans = Enclosing_Instance.exclude.GetSpans(reader);
42 private Lucene.Net.Index.IndexReader reader;
43 private SpanNotQuery enclosingInstance;
44 public SpanNotQuery Enclosing_Instance
46 get
48 return enclosingInstance;
52 private Spans includeSpans;
53 private bool moreInclude = true;
55 private Spans excludeSpans;
56 private bool moreExclude = true;
58 public virtual bool Next()
60 if (moreInclude)
61 // move to next include
62 moreInclude = includeSpans.Next();
64 while (moreInclude && moreExclude)
67 if (includeSpans.Doc() > excludeSpans.Doc())
68 // skip exclude
69 moreExclude = excludeSpans.SkipTo(includeSpans.Doc());
71 while (moreExclude && includeSpans.Doc() == excludeSpans.Doc() && excludeSpans.End() <= includeSpans.Start())
73 moreExclude = excludeSpans.Next(); // increment exclude
76 if (!moreExclude || includeSpans.Doc() != excludeSpans.Doc() || includeSpans.End() <= excludeSpans.Start())
77 break; // we found a match
79 moreInclude = includeSpans.Next(); // intersected: keep scanning
81 return moreInclude;
84 public virtual bool SkipTo(int target)
86 if (moreInclude)
87 // skip include
88 moreInclude = includeSpans.SkipTo(target);
90 if (!moreInclude)
91 return false;
93 if (moreExclude && includeSpans.Doc() > excludeSpans.Doc())
94 moreExclude = excludeSpans.SkipTo(includeSpans.Doc());
96 while (moreExclude && includeSpans.Doc() == excludeSpans.Doc() && excludeSpans.End() <= includeSpans.Start())
98 moreExclude = excludeSpans.Next(); // increment exclude
101 if (!moreExclude || includeSpans.Doc() != excludeSpans.Doc() || includeSpans.End() <= excludeSpans.Start())
102 return true; // we found a match
104 return Next(); // scan to next match
107 public virtual int Doc()
109 return includeSpans.Doc();
111 public virtual int Start()
113 return includeSpans.Start();
115 public virtual int End()
117 return includeSpans.End();
120 public override System.String ToString()
122 return "spans(" + Enclosing_Instance.ToString() + ")";
125 private SpanQuery include;
126 private SpanQuery exclude;
128 /// <summary>Construct a SpanNotQuery matching spans from <code>include</code> which
129 /// have no overlap with spans from <code>exclude</code>.
130 /// </summary>
131 public SpanNotQuery(SpanQuery include, SpanQuery exclude)
133 this.include = include;
134 this.exclude = exclude;
136 if (!include.GetField().Equals(exclude.GetField()))
137 throw new System.ArgumentException("Clauses must have same field.");
140 /// <summary>Return the SpanQuery whose matches are filtered. </summary>
141 public virtual SpanQuery GetInclude()
143 return include;
146 /// <summary>Return the SpanQuery whose matches must not overlap those returned. </summary>
147 public virtual SpanQuery GetExclude()
149 return exclude;
152 public override System.String GetField()
154 return include.GetField();
157 public override System.Collections.ICollection GetTerms()
159 return include.GetTerms();
162 public override System.String ToString(System.String field)
164 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
165 buffer.Append("spanNot(");
166 buffer.Append(include.ToString(field));
167 buffer.Append(", ");
168 buffer.Append(exclude.ToString(field));
169 buffer.Append(")");
170 buffer.Append(ToStringUtils.Boost(GetBoost()));
171 return buffer.ToString();
175 public override Spans GetSpans(IndexReader reader)
177 return new AnonymousClassSpans(reader, this);
180 public override Query Rewrite(IndexReader reader)
182 SpanNotQuery clone = null;
184 SpanQuery rewrittenInclude = (SpanQuery) include.Rewrite(reader);
185 if (rewrittenInclude != include)
187 clone = (SpanNotQuery) this.Clone();
188 clone.include = rewrittenInclude;
190 SpanQuery rewrittenExclude = (SpanQuery) exclude.Rewrite(reader);
191 if (rewrittenExclude != exclude)
193 if (clone == null)
194 clone = (SpanNotQuery) this.Clone();
195 clone.exclude = rewrittenExclude;
198 if (clone != null)
200 return clone; // some clauses rewrote
202 else
204 return this; // no clauses rewrote