1 package org.apache.lucene.search; 2 3 import org.apache.lucene.util.Parameter; 4 5 20 21 22 public class BooleanClause implements java.io.Serializable { 23 24 25 public static final class Occur extends Parameter implements java.io.Serializable { 26 27 private Occur(String name) { 28 super(name); 30 } 31 32 public String toString() { 33 if (this == MUST) return "+"; 34 if (this == MUST_NOT) return "-"; 35 return ""; 36 } 37 38 39 public static final Occur MUST = new Occur("MUST"); 40 43 public static final Occur SHOULD = new Occur("SHOULD"); 44 47 public static final Occur MUST_NOT = new Occur("MUST_NOT"); 48 49 } 50 51 53 public Query query; 55 58 public boolean required = false; 60 63 public boolean prohibited = false; 65 private Occur occur = Occur.SHOULD; 66 67 76 public BooleanClause(Query q, boolean r, boolean p) { 77 query = q; 79 required = r; 80 prohibited = p; 81 if (required) { 82 if (prohibited) { 83 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 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 ("Unknown operator " + occur); 142 } 143 } 144 145 146 public boolean equals(Object 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 156 public int hashCode() { 157 return query.hashCode() ^ (this.required?1:0) ^ (this.prohibited?2:0); 158 } 159 160 161 public String toString() { 162 return occur.toString() + query.toString(); 163 } 164 } 165 | Popular Tags |