KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > BooleanClause


1 package org.apache.lucene.search;
2
3 import org.apache.lucene.util.Parameter;
4
5 /**
6  * Copyright 2004 The Apache Software Foundation
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 /** A clause in a BooleanQuery. */
22 public class BooleanClause implements java.io.Serializable JavaDoc {
23   
24   /** Specifies how terms may occur in matching documents. */
25   public static final class Occur extends Parameter implements java.io.Serializable JavaDoc {
26     
27     private Occur(String JavaDoc name) {
28       // typesafe enum pattern, no public constructor
29
super(name);
30     }
31
32     public String JavaDoc toString() {
33       if (this == MUST) return "+";
34       if (this == MUST_NOT) return "-";
35       return "";
36     }
37
38     /** Use this operator for terms that <i>must</i> appear in the matching documents. */
39     public static final Occur MUST = new Occur("MUST");
40     /** Use this operator for terms that <i>should</i> appear in the
41      * matching documents. For a BooleanQuery with two <code>SHOULD</code>
42      * subqueries, at least one of the queries must appear in the matching documents. */

43     public static final Occur SHOULD = new Occur("SHOULD");
44     /** Use this operator for terms that <i>must not</i> appear in the matching documents.
45      * Note that it is not possible to search for queries that only consist
46      * of a <code>MUST_NOT</code> query. */

47     public static final Occur MUST_NOT = new Occur("MUST_NOT");
48     
49   }
50
51   /** The query whose matching documents are combined by the boolean query.
52    * @deprecated use {@link #setQuery(Query)} instead */

53   public Query query; // TODO: decrease visibility for Lucene 2.0
54

55   /** If true, documents documents which <i>do not</i>
56     match this sub-query will <i>not</i> match the boolean query.
57     @deprecated use {@link #setOccur(BooleanClause.Occur)} instead */

58   public boolean required = false; // TODO: decrease visibility for Lucene 2.0
59

60   /** If true, documents documents which <i>do</i>
61     match this sub-query will <i>not</i> match the boolean query.
62     @deprecated use {@link #setOccur(BooleanClause.Occur)} instead */

63   public boolean prohibited = false; // TODO: decrease visibility for Lucene 2.0
64

65   private Occur occur = Occur.SHOULD;
66
67   /** Constructs a BooleanClause with query <code>q</code>, required
68    * <code>r</code> and prohibited <code>p</code>.
69    * @deprecated use BooleanClause(Query, Occur) instead
70    * <ul>
71    * <li>For BooleanClause(query, true, false) use BooleanClause(query, BooleanClause.Occur.MUST)
72    * <li>For BooleanClause(query, false, false) use BooleanClause(query, BooleanClause.Occur.SHOULD)
73    * <li>For BooleanClause(query, false, true) use BooleanClause(query, BooleanClause.Occur.MUST_NOT)
74    * </ul>
75    */

76   public BooleanClause(Query q, boolean r, boolean p) {
77     // TODO: remove for Lucene 2.0
78
query = q;
79     required = r;
80     prohibited = p;
81     if (required) {
82       if (prohibited) {
83         // prohibited && required doesn't make sense, but we want the old behaviour:
84
occur = Occur.MUST_NOT;
85       } else {
86          occur = Occur.MUST;
87       }
88     } else {
89       if (prohibited) {
90          occur = Occur.MUST_NOT;
91       } else {
92          occur = Occur.SHOULD;
93       }
94     }
95   }
96
97   /** Constructs a BooleanClause.
98   */

99   public BooleanClause(Query query, Occur occur) {
100     this.query = query;
101     this.occur = occur;
102     setFields(occur);
103   }
104
105   public Occur getOccur() {
106     return occur;
107   }
108
109   public void setOccur(Occur occur) {
110     this.occur = occur;
111     setFields(occur);
112   }
113
114   public Query getQuery() {
115     return query;
116   }
117
118   public void setQuery(Query query) {
119     this.query = query;
120   }
121   
122   public boolean isProhibited() {
123     return prohibited;
124   }
125
126   public boolean isRequired() {
127     return required;
128   }
129
130   private void setFields(Occur occur) {
131     if (occur == Occur.MUST) {
132       required = true;
133       prohibited = false;
134     } else if (occur == Occur.SHOULD) {
135       required = false;
136       prohibited = false;
137     } else if (occur == Occur.MUST_NOT) {
138       required = false;
139       prohibited = true;
140     } else {
141       throw new IllegalArgumentException JavaDoc("Unknown operator " + occur);
142     }
143   }
144
145   /** Returns true iff <code>o</code> is equal to this. */
146   public boolean equals(Object JavaDoc o) {
147     if (!(o instanceof BooleanClause))
148       return false;
149     BooleanClause other = (BooleanClause)o;
150     return this.query.equals(other.query)
151       && (this.required == other.required)
152       && (this.prohibited == other.prohibited);
153   }
154
155   /** Returns a hash code value for this object.*/
156   public int hashCode() {
157     return query.hashCode() ^ (this.required?1:0) ^ (this.prohibited?2:0);
158   }
159
160
161   public String JavaDoc toString() {
162     return occur.toString() + query.toString();
163   }
164 }
165
Popular Tags