1 package org.apache.lucene.search.spans; 2 3 18 19 import java.io.IOException ; 20 21 import java.util.Collection ; 22 23 import org.apache.lucene.index.IndexReader; 24 import org.apache.lucene.search.Query; 25 import org.apache.lucene.util.ToStringUtils; 26 27 28 public class SpanNotQuery extends SpanQuery { 29 private SpanQuery include; 30 private SpanQuery exclude; 31 32 34 public SpanNotQuery(SpanQuery include, SpanQuery exclude) { 35 this.include = include; 36 this.exclude = exclude; 37 38 if (!include.getField().equals(exclude.getField())) 39 throw new IllegalArgumentException ("Clauses must have same field."); 40 } 41 42 43 public SpanQuery getInclude() { return include; } 44 45 46 public SpanQuery getExclude() { return exclude; } 47 48 public String getField() { return include.getField(); } 49 50 public Collection getTerms() { return include.getTerms(); } 51 52 public String toString(String field) { 53 StringBuffer buffer = new StringBuffer (); 54 buffer.append("spanNot("); 55 buffer.append(include.toString(field)); 56 buffer.append(", "); 57 buffer.append(exclude.toString(field)); 58 buffer.append(")"); 59 buffer.append(ToStringUtils.boost(getBoost())); 60 return buffer.toString(); 61 } 62 63 64 public Spans getSpans(final IndexReader reader) throws IOException { 65 return new Spans() { 66 private Spans includeSpans = include.getSpans(reader); 67 private boolean moreInclude = true; 68 69 private Spans excludeSpans = exclude.getSpans(reader); 70 private boolean moreExclude = true; 71 72 public boolean next() throws IOException { 73 if (moreInclude) moreInclude = includeSpans.next(); 75 76 while (moreInclude && moreExclude) { 77 78 if (includeSpans.doc() > excludeSpans.doc()) moreExclude = excludeSpans.skipTo(includeSpans.doc()); 80 81 while (moreExclude && includeSpans.doc() == excludeSpans.doc() 83 && excludeSpans.end() <= includeSpans.start()) { 84 moreExclude = excludeSpans.next(); } 86 87 if (!moreExclude || includeSpans.doc() != excludeSpans.doc() 89 || includeSpans.end() <= excludeSpans.start()) 90 break; 92 moreInclude = includeSpans.next(); } 94 return moreInclude; 95 } 96 97 public boolean skipTo(int target) throws IOException { 98 if (moreInclude) moreInclude = includeSpans.skipTo(target); 100 101 if (!moreInclude) 102 return false; 103 104 if (moreExclude && includeSpans.doc() > excludeSpans.doc()) 106 moreExclude = excludeSpans.skipTo(includeSpans.doc()); 107 108 while (moreExclude && includeSpans.doc() == excludeSpans.doc() 110 && excludeSpans.end() <= includeSpans.start()) { 111 moreExclude = excludeSpans.next(); } 113 114 if (!moreExclude || includeSpans.doc() != excludeSpans.doc() 116 || includeSpans.end() <= excludeSpans.start()) 117 return true; 119 return next(); } 121 122 public int doc() { return includeSpans.doc(); } 123 public int start() { return includeSpans.start(); } 124 public int end() { return includeSpans.end(); } 125 126 public String toString() { 127 return "spans(" + SpanNotQuery.this.toString() + ")"; 128 } 129 130 }; 131 } 132 133 public Query rewrite(IndexReader reader) throws IOException { 134 SpanNotQuery clone = null; 135 136 SpanQuery rewrittenInclude = (SpanQuery) include.rewrite(reader); 137 if (rewrittenInclude != include) { 138 clone = (SpanNotQuery) this.clone(); 139 clone.include = rewrittenInclude; 140 } 141 SpanQuery rewrittenExclude = (SpanQuery) exclude.rewrite(reader); 142 if (rewrittenExclude != exclude) { 143 if (clone == null) clone = (SpanNotQuery) this.clone(); 144 clone.exclude = rewrittenExclude; 145 } 146 147 if (clone != null) { 148 return clone; } else { 150 return this; } 152 } 153 154 155 public boolean equals(Object o) { 156 if (this == o) return true; 157 if (!(o instanceof SpanNotQuery)) return false; 158 159 SpanNotQuery other = (SpanNotQuery)o; 160 return this.include.equals(other.include) 161 && this.exclude.equals(other.exclude) 162 && this.getBoost() == other.getBoost(); 163 } 164 165 public int hashCode() { 166 int h = include.hashCode(); 167 h = (h<<1) | (h >>> 31); h ^= include.hashCode(); 169 h = (h<<1) | (h >>> 31); h ^= Float.floatToRawIntBits(getBoost()); 171 return h; 172 } 173 174 } 175 | Popular Tags |