001: package org.apache.lucene.search;
002:
003: import org.apache.lucene.util.Parameter;
004:
005: /**
006: * Licensed to the Apache Software Foundation (ASF) under one or more
007: * contributor license agreements. See the NOTICE file distributed with
008: * this work for additional information regarding copyright ownership.
009: * The ASF licenses this file to You under the Apache License, Version 2.0
010: * (the "License"); you may not use this file except in compliance with
011: * the License. You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: /** A clause in a BooleanQuery. */
023: public class BooleanClause implements java.io.Serializable {
024:
025: /** Specifies how clauses are to occur in matching documents. */
026: public static final class Occur extends Parameter implements
027: java.io.Serializable {
028:
029: private Occur(String name) {
030: // typesafe enum pattern, no public constructor
031: super (name);
032: }
033:
034: public String toString() {
035: if (this == MUST)
036: return "+";
037: if (this == MUST_NOT)
038: return "-";
039: return "";
040: }
041:
042: /** Use this operator for clauses that <i>must</i> appear in the matching documents. */
043: public static final Occur MUST = new Occur("MUST");
044: /** Use this operator for clauses that <i>should</i> appear in the
045: * matching documents. For a BooleanQuery with no <code>MUST</code>
046: * clauses one or more <code>SHOULD</code> clauses must match a document
047: * for the BooleanQuery to match.
048: * @see BooleanQuery#setMinimumNumberShouldMatch
049: */
050: public static final Occur SHOULD = new Occur("SHOULD");
051: /** Use this operator for clauses that <i>must not</i> appear in the matching documents.
052: * Note that it is not possible to search for queries that only consist
053: * of a <code>MUST_NOT</code> clause. */
054: public static final Occur MUST_NOT = new Occur("MUST_NOT");
055:
056: }
057:
058: /** The query whose matching documents are combined by the boolean query.
059: */
060: private Query query;
061:
062: private Occur occur;
063:
064: /** Constructs a BooleanClause.
065: */
066: public BooleanClause(Query query, Occur occur) {
067: this .query = query;
068: this .occur = occur;
069:
070: }
071:
072: public Occur getOccur() {
073: return occur;
074: }
075:
076: public void setOccur(Occur occur) {
077: this .occur = occur;
078:
079: }
080:
081: public Query getQuery() {
082: return query;
083: }
084:
085: public void setQuery(Query query) {
086: this .query = query;
087: }
088:
089: public boolean isProhibited() {
090: return Occur.MUST_NOT.equals(occur);
091: }
092:
093: public boolean isRequired() {
094: return Occur.MUST.equals(occur);
095: }
096:
097: /** Returns true iff <code>o</code> is equal to this. */
098: public boolean equals(Object o) {
099: if (!(o instanceof BooleanClause))
100: return false;
101: BooleanClause other = (BooleanClause) o;
102: return this .query.equals(other.query)
103: && this .occur.equals(other.occur);
104: }
105:
106: /** Returns a hash code value for this object.*/
107: public int hashCode() {
108: return query.hashCode() ^ (Occur.MUST.equals(occur) ? 1 : 0)
109: ^ (Occur.MUST_NOT.equals(occur) ? 2 : 0);
110: }
111:
112: public String toString() {
113: return occur.toString() + query.toString();
114: }
115: }
|