add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / WildcardQuery.java
1 package org.apache.lucene.search;
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.index.IndexReader;
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.util.ToStringUtils;
23
24 import java.io.IOException;
25
26 /** Implements the wildcard search query. Supported wildcards are <code>*</code>, which
27  * matches any character sequence (including the empty one), and <code>?</code>,
28  * which matches any single character. Note this query can be slow, as it
29  * needs to iterate over many terms. In order to prevent extremely slow WildcardQueries,
30  * a Wildcard term should not start with one of the wildcards <code>*</code> or
31  * <code>?</code>.
32  * 
33  * <p>This query uses the {@link
34  * MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT}
35  * rewrite method.
36  *
37  * @see WildcardTermEnum */
38 public class WildcardQuery extends MultiTermQuery {
39   private boolean termContainsWildcard;
40   private boolean termIsPrefix;
41   protected Term term;
42     
43   public WildcardQuery(Term term) {
44     this.term = term;
45     String text = term.text();
46     this.termContainsWildcard = (text.indexOf('*') != -1)
47         || (text.indexOf('?') != -1);
48     this.termIsPrefix = termContainsWildcard 
49         && (text.indexOf('?') == -1) 
50         && (text.indexOf('*') == text.length() - 1);
51   }
52
53   @Override
54   protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
55     if (termIsPrefix) {
56       return new PrefixTermEnum(reader, term.createTerm(term.text()
57           .substring(0, term.text().indexOf('*')))); 
58     } else if (termContainsWildcard) {
59       return new WildcardTermEnum(reader, getTerm());
60     } else {
61       return new SingleTermEnum(reader, getTerm());
62     }
63   }
64   
65   /**
66    * Returns the pattern term.
67    */
68   public Term getTerm() {
69     return term;
70   }
71   
72   /** Prints a user-readable version of this query. */
73   @Override
74   public String toString(String field) {
75     StringBuilder buffer = new StringBuilder();
76     if (!term.field().equals(field)) {
77       buffer.append(term.field());
78       buffer.append(":");
79     }
80     buffer.append(term.text());
81     buffer.append(ToStringUtils.boost(getBoost()));
82     return buffer.toString();
83   }
84
85   @Override
86   public int hashCode() {
87     final int prime = 31;
88     int result = super.hashCode();
89     result = prime * result + ((term == null) ? 0 : term.hashCode());
90     return result;
91   }
92
93   @Override
94   public boolean equals(Object obj) {
95     if (this == obj)
96       return true;
97     if (!super.equals(obj))
98       return false;
99     if (getClass() != obj.getClass())
100       return false;
101     WildcardQuery other = (WildcardQuery) obj;
102     if (term == null) {
103       if (other.term != null)
104         return false;
105     } else if (!term.equals(other.term))
106       return false;
107     return true;
108   }
109
110 }