1 package org.apache.lucene.search.regex;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
26 import java.io.IOException;
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.
34 public class RegexQuery extends MultiTermQuery implements RegexQueryCapable {
35 private RegexCapabilities regexImpl = new JavaUtilRegexCapabilities();
38 /** Constructs a query for terms matching <code>term</code>. */
39 public RegexQuery(Term term) {
43 public Term getTerm() { return term; }
46 * Defines which {@link RegexCapabilities} implementation is used by this instance.
50 public void setRegexImplementation(RegexCapabilities impl) {
51 this.regexImpl = impl;
55 * @return The implementation used by this instance.
57 public RegexCapabilities getRegexImplementation() {
62 protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
63 return new RegexTermEnum(reader, term, regexImpl);
67 public String toString(String field) {
68 StringBuilder buffer = new StringBuilder();
69 if (!term.field().equals(field)) {
70 buffer.append(term.field());
73 buffer.append(term.text());
74 buffer.append(ToStringUtils.boost(getBoost()));
75 return buffer.toString();
78 /* generated by IntelliJ IDEA */
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;
85 final RegexQuery that = (RegexQuery) o;
87 return regexImpl.equals(that.regexImpl);
90 /* generated by IntelliJ IDEA */
92 public int hashCode() {
93 int result = super.hashCode();
94 result = 29 * result + regexImpl.hashCode();