[monkeydoc] Merge/add monkeydoc to master.
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Search / Spans / SpanNotQuery.cs
1 /* 
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 using System;
19
20 using IndexReader = Mono.Lucene.Net.Index.IndexReader;
21 using ToStringUtils = Mono.Lucene.Net.Util.ToStringUtils;
22 using Query = Mono.Lucene.Net.Search.Query;
23
24 namespace Mono.Lucene.Net.Search.Spans
25 {
26         
27         /// <summary>Removes matches which overlap with another SpanQuery. </summary>
28         [Serializable]
29         public class SpanNotQuery:SpanQuery, System.ICloneable
30         {
31                 private class AnonymousClassSpans : Spans
32                 {
33                         public AnonymousClassSpans(Mono.Lucene.Net.Index.IndexReader reader, SpanNotQuery enclosingInstance)
34                         {
35                                 InitBlock(reader, enclosingInstance);
36                         }
37                         private void  InitBlock(Mono.Lucene.Net.Index.IndexReader reader, SpanNotQuery enclosingInstance)
38                         {
39                                 this.reader = reader;
40                                 this.enclosingInstance = enclosingInstance;
41                                 includeSpans = Enclosing_Instance.include.GetSpans(reader);
42                                 excludeSpans = Enclosing_Instance.exclude.GetSpans(reader);
43                                 moreExclude = excludeSpans.Next();
44                         }
45                         private Mono.Lucene.Net.Index.IndexReader reader;
46                         private SpanNotQuery enclosingInstance;
47                         public SpanNotQuery Enclosing_Instance
48                         {
49                                 get
50                                 {
51                                         return enclosingInstance;
52                                 }
53                                 
54                         }
55                         private Spans includeSpans;
56                         private bool moreInclude = true;
57                         
58                         private Spans excludeSpans;
59                         private bool moreExclude;
60                         
61                         public override bool Next()
62                         {
63                                 if (moreInclude)
64                                 // move to next include
65                                         moreInclude = includeSpans.Next();
66                                 
67                                 while (moreInclude && moreExclude)
68                                 {
69                                         
70                                         if (includeSpans.Doc() > excludeSpans.Doc())
71                                         // skip exclude
72                                                 moreExclude = excludeSpans.SkipTo(includeSpans.Doc());
73                                         
74                                         while (moreExclude && includeSpans.Doc() == excludeSpans.Doc() && excludeSpans.End() <= includeSpans.Start())
75                                         {
76                                                 moreExclude = excludeSpans.Next(); // increment exclude
77                                         }
78                                         
79                                         if (!moreExclude || includeSpans.Doc() != excludeSpans.Doc() || includeSpans.End() <= excludeSpans.Start())
80                                                 break; // we found a match
81                                         
82                                         moreInclude = includeSpans.Next(); // intersected: keep scanning
83                                 }
84                                 return moreInclude;
85                         }
86                         
87                         public override bool SkipTo(int target)
88                         {
89                                 if (moreInclude)
90                                 // skip include
91                                         moreInclude = includeSpans.SkipTo(target);
92                                 
93                                 if (!moreInclude)
94                                         return false;
95                                 
96                                 if (moreExclude && includeSpans.Doc() > excludeSpans.Doc())
97                                         moreExclude = excludeSpans.SkipTo(includeSpans.Doc());
98                                 
99                                 while (moreExclude && includeSpans.Doc() == excludeSpans.Doc() && excludeSpans.End() <= includeSpans.Start())
100                                 {
101                                         moreExclude = excludeSpans.Next(); // increment exclude
102                                 }
103                                 
104                                 if (!moreExclude || includeSpans.Doc() != excludeSpans.Doc() || includeSpans.End() <= excludeSpans.Start())
105                                         return true; // we found a match
106                                 
107                                 return Next(); // scan to next match
108                         }
109                         
110                         public override int Doc()
111                         {
112                                 return includeSpans.Doc();
113                         }
114                         public override int Start()
115                         {
116                                 return includeSpans.Start();
117                         }
118                         public override int End()
119                         {
120                                 return includeSpans.End();
121                         }
122                         
123                         // TODO: Remove warning after API has been finalizedb
124                         public override System.Collections.Generic.ICollection<byte[]> GetPayload()
125                         {
126                                 System.Collections.Generic.ICollection<byte[]> result = null;
127                                 if (includeSpans.IsPayloadAvailable())
128                                 {
129                                         result = includeSpans.GetPayload();
130                                 }
131                                 return result;
132                         }
133                         
134                         // TODO: Remove warning after API has been finalized
135                         public override bool IsPayloadAvailable()
136                         {
137                                 return includeSpans.IsPayloadAvailable();
138                         }
139                         
140                         public override System.String ToString()
141                         {
142                                 return "spans(" + Enclosing_Instance.ToString() + ")";
143                         }
144                 }
145                 private SpanQuery include;
146                 private SpanQuery exclude;
147                 
148                 /// <summary>Construct a SpanNotQuery matching spans from <code>include</code> which
149                 /// have no overlap with spans from <code>exclude</code>.
150                 /// </summary>
151                 public SpanNotQuery(SpanQuery include, SpanQuery exclude)
152                 {
153                         this.include = include;
154                         this.exclude = exclude;
155                         
156                         if (!include.GetField().Equals(exclude.GetField()))
157                                 throw new System.ArgumentException("Clauses must have same field.");
158                 }
159                 
160                 /// <summary>Return the SpanQuery whose matches are filtered. </summary>
161                 public virtual SpanQuery GetInclude()
162                 {
163                         return include;
164                 }
165                 
166                 /// <summary>Return the SpanQuery whose matches must not overlap those returned. </summary>
167                 public virtual SpanQuery GetExclude()
168                 {
169                         return exclude;
170                 }
171                 
172                 public override System.String GetField()
173                 {
174                         return include.GetField();
175                 }
176                 
177                 /// <summary>Returns a collection of all terms matched by this query.</summary>
178                 /// <deprecated> use extractTerms instead
179                 /// </deprecated>
180                 /// <seealso cref="ExtractTerms(Set)">
181                 /// </seealso>
182         [Obsolete("use ExtractTerms instead")]
183                 public override System.Collections.ICollection GetTerms()
184                 {
185                         return include.GetTerms();
186                 }
187                 
188                 public override void  ExtractTerms(System.Collections.Hashtable terms)
189                 {
190                         include.ExtractTerms(terms);
191                 }
192                 
193                 public override System.String ToString(System.String field)
194                 {
195                         System.Text.StringBuilder buffer = new System.Text.StringBuilder();
196                         buffer.Append("spanNot(");
197                         buffer.Append(include.ToString(field));
198                         buffer.Append(", ");
199                         buffer.Append(exclude.ToString(field));
200                         buffer.Append(")");
201                         buffer.Append(ToStringUtils.Boost(GetBoost()));
202                         return buffer.ToString();
203                 }
204                 
205                 public override System.Object Clone()
206                 {
207                         SpanNotQuery spanNotQuery = new SpanNotQuery((SpanQuery) include.Clone(), (SpanQuery) exclude.Clone());
208                         spanNotQuery.SetBoost(GetBoost());
209                         return spanNotQuery;
210                 }
211                 
212                 public override Spans GetSpans(IndexReader reader)
213                 {
214                         return new AnonymousClassSpans(reader, this);
215                 }
216                 
217                 public override Query Rewrite(IndexReader reader)
218                 {
219                         SpanNotQuery clone = null;
220                         
221                         SpanQuery rewrittenInclude = (SpanQuery) include.Rewrite(reader);
222                         if (rewrittenInclude != include)
223                         {
224                                 clone = (SpanNotQuery) this.Clone();
225                                 clone.include = rewrittenInclude;
226                         }
227                         SpanQuery rewrittenExclude = (SpanQuery) exclude.Rewrite(reader);
228                         if (rewrittenExclude != exclude)
229                         {
230                                 if (clone == null)
231                                         clone = (SpanNotQuery) this.Clone();
232                                 clone.exclude = rewrittenExclude;
233                         }
234                         
235                         if (clone != null)
236                         {
237                                 return clone; // some clauses rewrote
238                         }
239                         else
240                         {
241                                 return this; // no clauses rewrote
242                         }
243                 }
244                 
245                 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
246                 public  override bool Equals(System.Object o)
247                 {
248                         if (this == o)
249                                 return true;
250                         if (!(o is SpanNotQuery))
251                                 return false;
252                         
253                         SpanNotQuery other = (SpanNotQuery) o;
254                         return this.include.Equals(other.include) && this.exclude.Equals(other.exclude) && this.GetBoost() == other.GetBoost();
255                 }
256                 
257                 public override int GetHashCode()
258                 {
259                         int h = include.GetHashCode();
260                         h = (h << 1) | (SupportClass.Number.URShift(h, 31)); // rotate left
261                         h ^= exclude.GetHashCode();
262                         h = (h << 1) | (SupportClass.Number.URShift(h, 31)); // rotate left
263                         h ^= System.Convert.ToInt32(GetBoost());
264                         return h;
265                 }
266         }
267 }