add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queries / src / java / org / apache / lucene / search / regex / RegexQuery.java
1 package org.apache.lucene.search.regex;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.search.MultiTermQuery;
21 import org.apache.lucene.search.FilteredTermEnum;
22 import org.apache.lucene.index.Term;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.util.ToStringUtils;
25
26 import java.io.IOException;
27
28 /** Implements the regular expression term search query.
29  * The expressions supported depend on the regular expression implementation
30  * used by way of the {@link RegexCapabilities} interface.
31  *
32  * @see RegexTermEnum
33  */
34 public class RegexQuery extends MultiTermQuery implements RegexQueryCapable {
35   private RegexCapabilities regexImpl = new JavaUtilRegexCapabilities();
36   private Term term;
37
38   /** Constructs a query for terms matching <code>term</code>. */
39   public RegexQuery(Term term) {
40     this.term = term;
41   }
42   
43   public Term getTerm() { return term; }
44
45   /**
46    * Defines which {@link RegexCapabilities} implementation is used by this instance.
47    *
48    * @param impl
49    */
50   public void setRegexImplementation(RegexCapabilities impl) {
51     this.regexImpl = impl;
52   }
53
54   /**
55    * @return The implementation used by this instance.
56    */
57   public RegexCapabilities getRegexImplementation() {
58     return regexImpl;
59   }
60
61   @Override
62   protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
63     return new RegexTermEnum(reader, term, regexImpl);
64   }
65
66   @Override
67   public String toString(String field) {
68     StringBuilder buffer = new StringBuilder();
69     if (!term.field().equals(field)) {
70       buffer.append(term.field());
71       buffer.append(":");
72     }
73     buffer.append(term.text());
74     buffer.append(ToStringUtils.boost(getBoost()));
75     return buffer.toString();
76   }
77
78   /* generated by IntelliJ IDEA */
79   @Override
80   public boolean equals(Object o) {
81     if (this == o) return true;
82     if (o == null || getClass() != o.getClass()) return false;
83     if (!super.equals(o)) return false;
84
85     final RegexQuery that = (RegexQuery) o;
86
87     return regexImpl.equals(that.regexImpl);
88   }
89
90   /* generated by IntelliJ IDEA */
91   @Override
92   public int hashCode() {
93     int result = super.hashCode();
94     result = 29 * result + regexImpl.hashCode();
95     return result;
96   }
97 }