Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
RegexQuery.cs
Go to the documentation of this file.
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 using System.Text;
20 using Lucene.Net.Index;
21 using Lucene.Net.Search;
22 using Lucene.Net.Util;
23 
24 namespace Contrib.Regex
25 {
26  /// <summary>
27  /// Regular expression based query.
28  /// </summary>
29  /// <remarks>http://www.java2s.com/Open-Source/Java-Document/Net/lucene-connector/org/apache/lucene/search/regex/RegexQuery.java.htm</remarks>
30  public class RegexQuery : MultiTermQuery, IRegexQueryCapable, IEquatable<RegexQuery>
31  {
32  private IRegexCapabilities _regexImpl = new CSharpRegexCapabilities();
33  public Term Term { get; private set; }
34 
35  public RegexQuery(Term term)
36  {
37  Term = term;
38  }
39 
40  /// <summary>Construct the enumeration to be used, expanding the pattern term. </summary>
41  protected override FilteredTermEnum GetEnum(IndexReader reader)
42  {
43  return new RegexTermEnum(reader, Term, _regexImpl);
44  }
45 
46  public IRegexCapabilities RegexImplementation
47  {
48  set { _regexImpl = value; }
49  get { return _regexImpl; }
50  }
51 
52 
53  public override String ToString(String field)
54  {
55  StringBuilder buffer = new StringBuilder();
56  if (!Term.Field.Equals(field))
57  {
58  buffer.Append(Term.Field);
59  buffer.Append(":");
60  }
61  buffer.Append(Term.Text);
62  buffer.Append(ToStringUtils.Boost(Boost));
63  return buffer.ToString();
64  }
65 
66  /// <summary>
67  /// Indicates whether the current object is equal to another object of the same type.
68  /// </summary>
69  /// <returns>
70  /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
71  /// </returns>
72  /// <param name="other">An object to compare with this object</param>
73  public bool Equals(RegexQuery other)
74  {
75  if (other == null) return false;
76  if (this == other) return true;
77 
78  if (!base.Equals(other)) return false;
79  return _regexImpl.Equals(other._regexImpl);
80  }
81 
82  public override bool Equals(object obj)
83  {
84  if ((obj == null) || (obj as RegexQuery == null)) return false;
85  if (this == obj) return true;
86 
87  return Equals((RegexQuery) obj);
88  }
89 
90  public override int GetHashCode()
91  {
92  return 29 * base.GetHashCode() + _regexImpl.GetHashCode();
93  }
94  }
95 }