1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 using System; 19 20 namespace Lucene.Net.Search 21 { 22 23 /// <summary>A clause in a BooleanQuery. </summary> 24 [Serializable] 25 public class BooleanClause 26 { 27 private Occur occur; 28 29 /// <summary>Constructs a BooleanClause.</summary> BooleanClause(Query query, Occur occur)30 public BooleanClause(Query query, Occur occur) 31 { 32 this._query = query; 33 this.occur = occur; 34 } 35 36 public virtual Occur Occur 37 { 38 get { return occur; } 39 set { this.occur = value; } 40 } 41 42 private Query _query; 43 44 /// <summary>The query whose matching documents are combined by the boolean query.</summary> 45 public virtual Query Query 46 { 47 get { return _query; } 48 set { this._query = value; } 49 } 50 51 public virtual bool IsProhibited 52 { 53 get { return Occur.MUST_NOT.Equals(occur); } 54 } 55 56 public virtual bool IsRequired 57 { 58 get { return Occur.MUST.Equals(occur); } 59 } 60 61 62 /// <summary>Returns true if <c>o</c> is equal to this. </summary> Equals(System.Object o)63 public override bool Equals(System.Object o) 64 { 65 if (o == null || !(o is BooleanClause)) 66 return false; 67 BooleanClause other = (BooleanClause) o; 68 return this.Query.Equals(other.Query) && this.occur.Equals(other.occur); 69 } 70 71 /// <summary>Returns a hash code value for this object.</summary> GetHashCode()72 public override int GetHashCode() 73 { 74 return Query.GetHashCode() ^ (Occur.MUST.Equals(occur)?1:0) ^ (Occur.MUST_NOT.Equals(occur)?2:0); 75 } 76 77 ToString()78 public override System.String ToString() 79 { 80 return OccurExtensions.ToString(occur) + Query; 81 } 82 } 83 84 public enum Occur 85 { 86 MUST, 87 SHOULD, 88 MUST_NOT 89 } 90 91 public static class OccurExtensions 92 { ToString(this Occur occur)93 public static System.String ToString(this Occur occur) 94 { 95 if (occur == Occur.MUST) 96 return "+"; 97 if (occur == Occur.MUST_NOT) 98 return "-"; 99 return ""; 100 } 101 } 102 }