add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / PrefixQuery.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 java.io.IOException;
21
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.util.ToStringUtils;
25
26 /** A Query that matches documents containing terms with a specified prefix. A PrefixQuery
27  * is built by QueryParser for input like <code>app*</code>.
28  *
29  * <p>This query uses the {@link
30  * MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT}
31  * rewrite method. */
32 public class PrefixQuery extends MultiTermQuery {
33   private Term prefix;
34
35   /** Constructs a query for terms starting with <code>prefix</code>. */
36   public PrefixQuery(Term prefix) {
37     this.prefix = prefix;
38   }
39
40   /** Returns the prefix of this query. */
41   public Term getPrefix() { return prefix; }
42   
43   @Override
44   protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
45     return new PrefixTermEnum(reader, prefix);
46   }
47
48   /** Prints a user-readable version of this query. */
49   @Override
50   public String toString(String field) {
51     StringBuilder buffer = new StringBuilder();
52     if (!prefix.field().equals(field)) {
53       buffer.append(prefix.field());
54       buffer.append(":");
55     }
56     buffer.append(prefix.text());
57     buffer.append('*');
58     buffer.append(ToStringUtils.boost(getBoost()));
59     return buffer.toString();
60   }
61
62   @Override
63   public int hashCode() {
64     final int prime = 31;
65     int result = super.hashCode();
66     result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
67     return result;
68   }
69
70   @Override
71   public boolean equals(Object obj) {
72     if (this == obj)
73       return true;
74     if (!super.equals(obj))
75       return false;
76     if (getClass() != obj.getClass())
77       return false;
78     PrefixQuery other = (PrefixQuery) obj;
79     if (prefix == null) {
80       if (other.prefix != null)
81         return false;
82     } else if (!prefix.equals(other.prefix))
83       return false;
84     return true;
85   }
86
87 }