cvsimport
[beagle.git] / beagled / Lucene.Net / Search / Spans / SpanTermQuery.cs
blob5d663848916ae0cd2fb35f913f7f508f291c6d43
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 Term = Lucene.Net.Index.Term;
20 using TermPositions = Lucene.Net.Index.TermPositions;
21 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
23 namespace Lucene.Net.Search.Spans
26 /// <summary>Matches spans containing a term. </summary>
27 [Serializable]
28 public class SpanTermQuery:SpanQuery
30 private class AnonymousClassSpans : Spans
32 public AnonymousClassSpans(Lucene.Net.Index.IndexReader reader, SpanTermQuery enclosingInstance)
34 InitBlock(reader, enclosingInstance);
36 private void InitBlock(Lucene.Net.Index.IndexReader reader, SpanTermQuery enclosingInstance)
38 this.reader = reader;
39 this.enclosingInstance = enclosingInstance;
40 positions = reader.TermPositions(Enclosing_Instance.term);
42 private Lucene.Net.Index.IndexReader reader;
43 private SpanTermQuery enclosingInstance;
44 public SpanTermQuery Enclosing_Instance
46 get
48 return enclosingInstance;
52 private TermPositions positions;
54 private int doc = - 1;
55 private int freq;
56 private int count;
57 private int position;
59 public virtual bool Next()
61 if (count == freq)
63 if (!positions.Next())
65 doc = System.Int32.MaxValue;
66 return false;
68 doc = positions.Doc();
69 freq = positions.Freq();
70 count = 0;
72 position = positions.NextPosition();
73 count++;
74 return true;
77 public virtual bool SkipTo(int target)
79 // are we already at the correct position?
80 if (doc >= target)
82 return true;
85 if (!positions.SkipTo(target))
87 doc = System.Int32.MaxValue;
88 return false;
91 doc = positions.Doc();
92 freq = positions.Freq();
93 count = 0;
95 position = positions.NextPosition();
96 count++;
98 return true;
101 public virtual int Doc()
103 return doc;
105 public virtual int Start()
107 return position;
109 public virtual int End()
111 return position + 1;
114 public override System.String ToString()
116 return "spans(" + Enclosing_Instance.ToString() + ")@" + (doc == - 1?"START":((doc == System.Int32.MaxValue)?"END":doc + "-" + position));
119 private Term term;
121 /// <summary>Construct a SpanTermQuery matching the named term's spans. </summary>
122 public SpanTermQuery(Term term)
124 this.term = term;
127 /// <summary>Return the term whose spans are matched. </summary>
128 public virtual Term GetTerm()
130 return term;
133 public override System.String GetField()
135 return term.Field();
138 public override System.Collections.ICollection GetTerms()
140 System.Collections.ArrayList terms = new System.Collections.ArrayList();
141 terms.Add(term);
142 return terms;
145 public override System.String ToString(System.String field)
147 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
148 if (term.Field().Equals(field))
149 buffer.Append(term.Text());
150 else
152 buffer.Append(term.ToString());
154 buffer.Append(ToStringUtils.Boost(GetBoost()));
155 return buffer.ToString();
158 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
159 public override bool Equals(System.Object o)
161 if (!(o is SpanTermQuery))
162 return false;
163 SpanTermQuery other = (SpanTermQuery) o;
164 return (this.GetBoost() == other.GetBoost()) && this.term.Equals(other.term);
167 /// <summary>Returns a hash code value for this object.</summary>
168 public override int GetHashCode()
170 return GetBoost().ToString().GetHashCode() ^ term.GetHashCode(); // {{Aroush-1.9}} Is this OK?
173 public override Spans GetSpans(IndexReader reader)
175 return new AnonymousClassSpans(reader, this);