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 java.util.regex.Pattern;
23 * An implementation tying Java's built-in java.util.regex to RegexQuery.
25 * Note that because this implementation currently only returns null from
26 * {@link #prefix} that queries using this implementation will enumerate and
27 * attempt to {@link #match} each term for the specified field in the index.
29 public class JavaUtilRegexCapabilities implements RegexCapabilities {
30 private Pattern pattern;
31 private int flags = 0;
33 // Define the optional flags from Pattern that can be used.
34 // Do this here to keep Pattern contained within this class.
36 public static final int FLAG_CANON_EQ = Pattern.CANON_EQ;
37 public static final int FLAG_CASE_INSENSITIVE = Pattern.CASE_INSENSITIVE;
38 public static final int FLAG_COMMENTS = Pattern.COMMENTS;
39 public static final int FLAG_DOTALL = Pattern.DOTALL;
40 public static final int FLAG_LITERAL = Pattern.LITERAL;
41 public static final int FLAG_MULTILINE = Pattern.MULTILINE;
42 public static final int FLAG_UNICODE_CASE = Pattern.UNICODE_CASE;
43 public static final int FLAG_UNIX_LINES = Pattern.UNIX_LINES;
46 * Default constructor that uses java.util.regex.Pattern
47 * with its default flags.
49 public JavaUtilRegexCapabilities() {
54 * Constructor that allows for the modification of the flags that
55 * the java.util.regex.Pattern will use to compile the regular expression.
56 * This gives the user the ability to fine-tune how the regular expression
57 * to match the functionality that they need.
58 * The {@link java.util.regex.Pattern Pattern} class supports specifying
59 * these fields via the regular expression text itself, but this gives the caller
60 * another option to modify the behavior. Useful in cases where the regular expression text
61 * cannot be modified, or if doing so is undesired.
63 * @param flags The flags that are ORed together.
65 public JavaUtilRegexCapabilities(int flags) {
69 public void compile(String pattern) {
70 this.pattern = Pattern.compile(pattern, this.flags);
73 public boolean match(String string) {
74 return pattern.matcher(string).matches();
77 public String prefix() {
82 public boolean equals(Object o) {
83 if (this == o) return true;
84 if (o == null || getClass() != o.getClass()) return false;
86 final JavaUtilRegexCapabilities that = (JavaUtilRegexCapabilities) o;
88 if (pattern != null ? !pattern.equals(that.pattern) : that.pattern != null) return false;
94 public int hashCode() {
95 return (pattern != null ? pattern.hashCode() : 0);