pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queries / src / java / org / apache / lucene / search / regex / JavaUtilRegexCapabilities.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 java.util.regex.Pattern;
21
22 /**
23  * An implementation tying Java's built-in java.util.regex to RegexQuery.
24  *
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.
28  */
29 public class JavaUtilRegexCapabilities implements RegexCapabilities {
30   private Pattern pattern;
31   private int flags = 0;
32   
33   // Define the optional flags from Pattern that can be used.
34   // Do this here to keep Pattern contained within this class.
35   
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;
44   
45   /**
46    * Default constructor that uses java.util.regex.Pattern 
47    * with its default flags.
48    */
49   public JavaUtilRegexCapabilities()  {
50     this.flags = 0;
51   }
52   
53   /**
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.
62    * 
63    * @param flags The flags that are ORed together.
64    */
65   public JavaUtilRegexCapabilities(int flags) {
66     this.flags = flags;
67   }
68   
69   public void compile(String pattern) {
70     this.pattern = Pattern.compile(pattern, this.flags);
71   }
72
73   public boolean match(String string) {
74     return pattern.matcher(string).matches();
75   }
76
77   public String prefix() {
78     return null;
79   }
80
81   @Override
82   public boolean equals(Object o) {
83     if (this == o) return true;
84     if (o == null || getClass() != o.getClass()) return false;
85
86     final JavaUtilRegexCapabilities that = (JavaUtilRegexCapabilities) o;
87
88     if (pattern != null ? !pattern.equals(that.pattern) : that.pattern != null) return false;
89
90     return true;
91   }
92
93   @Override
94   public int hashCode() {
95     return (pattern != null ? pattern.hashCode() : 0);
96   }
97 }